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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_SEC
35 #include <libcfs/libcfs.h>
36 #include <linux/crypto.h>
37 #include <linux/key.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_log.h>
45 #include <lustre_disk.h>
46 #include <lustre_dlm.h>
47 #include <lustre_param.h>
48 #include <lustre_sec.h>
50 #include "ptlrpc_internal.h"
52 const char *sptlrpc_part2name(enum lustre_sec_part part)
71 EXPORT_SYMBOL(sptlrpc_part2name);
73 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
75 const char *type = obd->obd_type->typ_name;
77 if (!strcmp(type, LUSTRE_MDT_NAME))
79 if (!strcmp(type, LUSTRE_OST_NAME))
81 if (!strcmp(type, LUSTRE_MGS_NAME))
84 CERROR("unknown target %p(%s)\n", obd, type);
88 /****************************************
89 * user supplied flavor string parsing *
90 ****************************************/
93 * format: <base_flavor>[-<bulk_type:alg_spec>]
95 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
100 memset(flvr, 0, sizeof(*flvr));
102 if (str == NULL || str[0] == '\0') {
103 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
107 strlcpy(buf, str, sizeof(buf));
109 bulk = strchr(buf, '-');
113 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
114 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
118 * currently only base flavor "plain" can have bulk specification.
120 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
121 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
124 * format: plain-hash:<hash_alg>
126 alg = strchr(bulk, ':');
131 if (strcmp(bulk, "hash"))
134 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
135 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
139 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
140 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
142 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
152 CERROR("invalid flavor string: %s\n", str);
155 EXPORT_SYMBOL(sptlrpc_parse_flavor);
157 /****************************************
159 ****************************************/
161 static void get_default_flavor(struct sptlrpc_flavor *sf)
163 memset(sf, 0, sizeof(*sf));
165 sf->sf_rpc = SPTLRPC_FLVR_NULL;
169 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
171 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
172 rule->sr_from = LUSTRE_SP_ANY;
173 rule->sr_to = LUSTRE_SP_ANY;
174 rule->sr_padding = 0;
176 get_default_flavor(&rule->sr_flvr);
180 * format: network[.direction]=flavor
182 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
187 sptlrpc_rule_init(rule);
189 flavor = strchr(param, '=');
190 if (flavor == NULL) {
191 CERROR("invalid param, no '='\n");
196 dir = strchr(param, '.');
201 if (strcmp(param, "default")) {
202 rule->sr_netid = libcfs_str2net(param);
203 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
204 CERROR("invalid network name: %s\n", param);
211 if (!strcmp(dir, "mdt2ost")) {
212 rule->sr_from = LUSTRE_SP_MDT;
213 rule->sr_to = LUSTRE_SP_OST;
214 } else if (!strcmp(dir, "mdt2mdt")) {
215 rule->sr_from = LUSTRE_SP_MDT;
216 rule->sr_to = LUSTRE_SP_MDT;
217 } else if (!strcmp(dir, "cli2ost")) {
218 rule->sr_from = LUSTRE_SP_CLI;
219 rule->sr_to = LUSTRE_SP_OST;
220 } else if (!strcmp(dir, "cli2mdt")) {
221 rule->sr_from = LUSTRE_SP_CLI;
222 rule->sr_to = LUSTRE_SP_MDT;
224 CERROR("invalid rule dir segment: %s\n", dir);
230 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
236 EXPORT_SYMBOL(sptlrpc_parse_rule);
238 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
240 LASSERT(rset->srs_nslot ||
241 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
243 if (rset->srs_nslot) {
244 OBD_FREE(rset->srs_rules,
245 rset->srs_nslot * sizeof(*rset->srs_rules));
246 sptlrpc_rule_set_init(rset);
249 EXPORT_SYMBOL(sptlrpc_rule_set_free);
252 * return 0 if the rule set could accomodate one more rule.
254 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
256 struct sptlrpc_rule *rules;
261 if (rset->srs_nrule < rset->srs_nslot)
264 nslot = rset->srs_nslot + 8;
266 /* better use realloc() if available */
267 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
271 if (rset->srs_nrule) {
272 LASSERT(rset->srs_nslot && rset->srs_rules);
273 memcpy(rules, rset->srs_rules,
274 rset->srs_nrule * sizeof(*rset->srs_rules));
276 OBD_FREE(rset->srs_rules,
277 rset->srs_nslot * sizeof(*rset->srs_rules));
280 rset->srs_rules = rules;
281 rset->srs_nslot = nslot;
285 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
287 return (rule->sr_from != LUSTRE_SP_ANY ||
288 rule->sr_to != LUSTRE_SP_ANY);
290 static inline int rule_spec_net(struct sptlrpc_rule *rule)
292 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
294 static inline int rule_match_dir(struct sptlrpc_rule *r1,
295 struct sptlrpc_rule *r2)
297 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
299 static inline int rule_match_net(struct sptlrpc_rule *r1,
300 struct sptlrpc_rule *r2)
302 return (r1->sr_netid == r2->sr_netid);
306 * merge @rule into @rset.
307 * the @rset slots might be expanded.
309 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
310 struct sptlrpc_rule *rule)
312 struct sptlrpc_rule *p = rset->srs_rules;
313 int spec_dir, spec_net;
314 int rc, n, match = 0;
318 spec_net = rule_spec_net(rule);
319 spec_dir = rule_spec_dir(rule);
321 for (n = 0; n < rset->srs_nrule; n++) {
322 p = &rset->srs_rules[n];
324 /* test network match, if failed:
325 * - spec rule: skip rules which is also spec rule match, until
326 * we hit a wild rule, which means no more chance
327 * - wild rule: skip until reach the one which is also wild
330 if (!rule_match_net(p, rule)) {
332 if (rule_spec_net(p))
341 /* test dir match, same logic as net matching */
342 if (!rule_match_dir(p, rule)) {
344 if (rule_spec_dir(p))
359 LASSERT(n >= 0 && n < rset->srs_nrule);
361 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
362 /* remove this rule */
363 if (n < rset->srs_nrule - 1)
364 memmove(&rset->srs_rules[n],
365 &rset->srs_rules[n + 1],
366 (rset->srs_nrule - n - 1) *
370 /* override the rule */
371 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
374 LASSERT(n >= 0 && n <= rset->srs_nrule);
376 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
377 rc = sptlrpc_rule_set_expand(rset);
381 if (n < rset->srs_nrule)
382 memmove(&rset->srs_rules[n + 1],
384 (rset->srs_nrule - n) * sizeof(*rule));
385 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
388 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
394 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
397 * given from/to/nid, determine a matching flavor in ruleset.
398 * return 1 if a match found, otherwise return 0.
400 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
401 enum lustre_sec_part from,
402 enum lustre_sec_part to,
404 struct sptlrpc_flavor *sf)
406 struct sptlrpc_rule *r;
409 for (n = 0; n < rset->srs_nrule; n++) {
410 r = &rset->srs_rules[n];
412 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
413 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
414 LNET_NIDNET(nid) != r->sr_netid)
417 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
421 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
432 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
434 struct sptlrpc_rule *r;
437 for (n = 0; n < rset->srs_nrule; n++) {
438 r = &rset->srs_rules[n];
439 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
440 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
444 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
445 struct sptlrpc_rule_set *tgt,
446 enum lustre_sec_part from,
447 enum lustre_sec_part to,
448 struct sptlrpc_rule_set *rset)
450 struct sptlrpc_rule_set *src[2] = { gen, tgt };
451 struct sptlrpc_rule *rule;
456 /* merge general rules firstly, then target-specific rules */
457 for (i = 0; i < 2; i++) {
461 for (n = 0; n < src[i]->srs_nrule; n++) {
462 rule = &src[i]->srs_rules[n];
464 if (from != LUSTRE_SP_ANY &&
465 rule->sr_from != LUSTRE_SP_ANY &&
466 rule->sr_from != from)
468 if (to != LUSTRE_SP_ANY &&
469 rule->sr_to != LUSTRE_SP_ANY &&
473 rc = sptlrpc_rule_set_merge(rset, rule);
475 CERROR("can't merge: %d\n", rc);
484 /**********************************
485 * sptlrpc configuration support *
486 **********************************/
488 struct sptlrpc_conf_tgt {
489 struct list_head sct_list;
490 char sct_name[MAX_OBD_NAME];
491 struct sptlrpc_rule_set sct_rset;
494 struct sptlrpc_conf {
495 struct list_head sc_list;
496 char sc_fsname[MTI_NAME_MAXLEN];
497 unsigned int sc_modified; /* modified during updating */
498 unsigned int sc_updated:1, /* updated copy from MGS */
499 sc_local:1; /* local copy from target */
500 struct sptlrpc_rule_set sc_rset; /* fs general rules */
501 struct list_head sc_tgts; /* target-specific rules */
504 static struct mutex sptlrpc_conf_lock;
505 static struct list_head sptlrpc_confs;
507 static inline int is_hex(char c)
509 return ((c >= '0' && c <= '9') ||
510 (c >= 'a' && c <= 'f'));
513 static void target2fsname(const char *tgt, char *fsname, int buflen)
518 ptr = strrchr(tgt, '-');
520 if ((strncmp(ptr, "-MDT", 4) != 0 &&
521 strncmp(ptr, "-OST", 4) != 0) ||
522 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
523 !is_hex(ptr[6]) || !is_hex(ptr[7]))
527 /* if we didn't find the pattern, treat the whole string as fsname */
533 len = min(len, buflen - 1);
534 memcpy(fsname, tgt, len);
538 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
540 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
542 sptlrpc_rule_set_free(&conf->sc_rset);
544 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
545 &conf->sc_tgts, sct_list) {
546 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
547 list_del(&conf_tgt->sct_list);
548 OBD_FREE_PTR(conf_tgt);
550 LASSERT(list_empty(&conf->sc_tgts));
552 conf->sc_updated = 0;
556 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
558 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
560 sptlrpc_conf_free_rsets(conf);
561 list_del(&conf->sc_list);
566 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
570 struct sptlrpc_conf_tgt *conf_tgt;
572 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
573 if (strcmp(conf_tgt->sct_name, name) == 0)
580 OBD_ALLOC_PTR(conf_tgt);
582 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
583 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
584 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
591 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
594 struct sptlrpc_conf *conf;
596 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
597 if (strcmp(conf->sc_fsname, fsname) == 0)
608 if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
609 sizeof(conf->sc_fsname)) {
613 sptlrpc_rule_set_init(&conf->sc_rset);
614 INIT_LIST_HEAD(&conf->sc_tgts);
615 list_add(&conf->sc_list, &sptlrpc_confs);
617 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
622 * caller must hold conf_lock already.
624 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
626 struct sptlrpc_rule *rule)
628 struct sptlrpc_conf_tgt *conf_tgt;
629 struct sptlrpc_rule_set *rule_set;
631 /* fsname == target means general rules for the whole fs */
632 if (strcmp(conf->sc_fsname, target) == 0) {
633 rule_set = &conf->sc_rset;
635 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
637 rule_set = &conf_tgt->sct_rset;
639 CERROR("out of memory, can't merge rule!\n");
644 return sptlrpc_rule_set_merge(rule_set, rule);
648 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
649 * find one through the target name in the record inside conf_lock;
650 * otherwise means caller already hold conf_lock.
652 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
653 struct sptlrpc_conf *conf)
655 char *target, *param;
656 char fsname[MTI_NAME_MAXLEN];
657 struct sptlrpc_rule rule;
661 target = lustre_cfg_string(lcfg, 1);
662 if (target == NULL) {
663 CERROR("missing target name\n");
667 param = lustre_cfg_string(lcfg, 2);
669 CERROR("missing parameter\n");
673 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
675 /* parse rule to make sure the format is correct */
676 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
677 CERROR("Invalid sptlrpc parameter: %s\n", param);
680 param += sizeof(PARAM_SRPC_FLVR) - 1;
682 rc = sptlrpc_parse_rule(param, &rule);
687 target2fsname(target, fsname, sizeof(fsname));
689 mutex_lock(&sptlrpc_conf_lock);
690 conf = sptlrpc_conf_get(fsname, 0);
692 CERROR("can't find conf\n");
695 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
697 mutex_unlock(&sptlrpc_conf_lock);
699 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
700 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
709 int sptlrpc_process_config(struct lustre_cfg *lcfg)
711 return __sptlrpc_process_config(lcfg, NULL);
713 EXPORT_SYMBOL(sptlrpc_process_config);
715 static int logname2fsname(const char *logname, char *buf, int buflen)
720 ptr = strrchr(logname, '-');
721 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
722 CERROR("%s is not a sptlrpc config log\n", logname);
726 len = min((int) (ptr - logname), buflen - 1);
728 memcpy(buf, logname, len);
733 void sptlrpc_conf_log_update_begin(const char *logname)
735 struct sptlrpc_conf *conf;
738 if (logname2fsname(logname, fsname, sizeof(fsname)))
741 mutex_lock(&sptlrpc_conf_lock);
743 conf = sptlrpc_conf_get(fsname, 0);
745 if (conf->sc_local) {
746 LASSERT(conf->sc_updated == 0);
747 sptlrpc_conf_free_rsets(conf);
749 conf->sc_modified = 0;
752 mutex_unlock(&sptlrpc_conf_lock);
754 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
757 * mark a config log has been updated
759 void sptlrpc_conf_log_update_end(const char *logname)
761 struct sptlrpc_conf *conf;
764 if (logname2fsname(logname, fsname, sizeof(fsname)))
767 mutex_lock(&sptlrpc_conf_lock);
769 conf = sptlrpc_conf_get(fsname, 0);
772 * if original state is not updated, make sure the
773 * modified counter > 0 to enforce updating local copy.
775 if (conf->sc_updated == 0)
778 conf->sc_updated = 1;
781 mutex_unlock(&sptlrpc_conf_lock);
783 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
785 void sptlrpc_conf_log_start(const char *logname)
789 if (logname2fsname(logname, fsname, sizeof(fsname)))
792 mutex_lock(&sptlrpc_conf_lock);
793 sptlrpc_conf_get(fsname, 1);
794 mutex_unlock(&sptlrpc_conf_lock);
796 EXPORT_SYMBOL(sptlrpc_conf_log_start);
798 void sptlrpc_conf_log_stop(const char *logname)
800 struct sptlrpc_conf *conf;
803 if (logname2fsname(logname, fsname, sizeof(fsname)))
806 mutex_lock(&sptlrpc_conf_lock);
807 conf = sptlrpc_conf_get(fsname, 0);
809 sptlrpc_conf_free(conf);
810 mutex_unlock(&sptlrpc_conf_lock);
812 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
814 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
815 enum lustre_sec_part from,
816 enum lustre_sec_part to,
817 unsigned int fl_udesc)
820 * null flavor doesn't need to set any flavor, and in fact
821 * we'd better not do that because everybody share a single sec.
823 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
826 if (from == LUSTRE_SP_MDT) {
827 /* MDT->MDT; MDT->OST */
828 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
829 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
831 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
832 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
834 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
835 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
838 /* Some flavors use a single uid (0) context */
839 if (flvr_is_rootonly(sf->sf_rpc))
840 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
842 /* User descriptor might need to be cleared */
843 if (flvr_allows_user_desc(sf->sf_rpc) == 0)
844 sf->sf_flags &= ~PTLRPC_SEC_FL_UDESC;
847 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
848 enum lustre_sec_part to,
849 struct obd_uuid *target,
851 struct sptlrpc_flavor *sf)
853 struct sptlrpc_conf *conf;
854 struct sptlrpc_conf_tgt *conf_tgt;
855 char name[MTI_NAME_MAXLEN];
858 target2fsname(target->uuid, name, sizeof(name));
860 mutex_lock(&sptlrpc_conf_lock);
862 conf = sptlrpc_conf_get(name, 0);
866 /* convert uuid name (supposed end with _UUID) to target name */
867 len = strlen(target->uuid);
869 memcpy(name, target->uuid, len - 5);
870 name[len - 5] = '\0';
872 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
874 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
880 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
882 mutex_unlock(&sptlrpc_conf_lock);
885 get_default_flavor(sf);
887 flavor_set_flags(sf, from, to, 1);
891 * called by target devices, determine the expected flavor from
892 * certain peer (from, nid).
894 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
895 enum lustre_sec_part from,
897 struct sptlrpc_flavor *sf)
899 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
900 get_default_flavor(sf);
903 #define SEC_ADAPT_DELAY (10)
906 * called by client devices, notify the sptlrpc config has changed and
907 * do import_sec_adapt later.
909 void sptlrpc_conf_client_adapt(struct obd_device *obd)
911 struct obd_import *imp;
914 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
915 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
916 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0 ||
917 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0);
918 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
920 /* serialize with connect/disconnect import */
921 down_read_nested(&obd->u.cli.cl_sem, OBD_CLI_SEM_MDCOSC);
923 imp = obd->u.cli.cl_import;
925 spin_lock(&imp->imp_lock);
927 imp->imp_sec_expire = cfs_time_current_sec() +
929 spin_unlock(&imp->imp_lock);
932 up_read(&obd->u.cli.cl_sem);
935 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
938 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
941 char net[LNET_NIDSTR_SIZE] = "default";
944 if (r->sr_netid != LNET_NIDNET(LNET_NID_ANY))
945 libcfs_net2str_r(r->sr_netid, net, sizeof(net));
947 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
950 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
951 sptlrpc_part2name(r->sr_from),
952 sptlrpc_part2name(r->sr_to));
954 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
956 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
957 buf[buflen - 1] = '\0';
960 static int sptlrpc_record_rule_set(struct llog_handle *llh,
962 struct sptlrpc_rule_set *rset)
964 struct llog_cfg_rec *lcr;
965 struct lustre_cfg_bufs bufs;
969 for (i = 0; i < rset->srs_nrule; i++) {
970 rule2string(&rset->srs_rules[i], param, sizeof(param));
972 lustre_cfg_bufs_reset(&bufs, NULL);
973 lustre_cfg_bufs_set_string(&bufs, 1, target);
974 lustre_cfg_bufs_set_string(&bufs, 2, param);
975 lcr = lustre_cfg_rec_new(LCFG_SPTLRPC_CONF, &bufs);
978 rc = llog_write(NULL, llh, &lcr->lcr_hdr, LLOG_NEXT_IDX);
979 lustre_cfg_rec_free(lcr);
986 static int sptlrpc_record_rules(struct llog_handle *llh,
987 struct sptlrpc_conf *conf)
989 struct sptlrpc_conf_tgt *conf_tgt;
991 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
993 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
994 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
995 &conf_tgt->sct_rset);
1000 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1001 #define LOG_SPTLRPC "sptlrpc"
1004 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1005 struct sptlrpc_conf *conf)
1007 struct llog_handle *llh = NULL;
1008 struct llog_ctxt *ctxt;
1009 struct lvfs_run_ctxt saved;
1010 struct dentry *dentry;
1014 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1018 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1020 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd.dentry,
1021 strlen(MOUNT_CONFIGS_DIR));
1022 if (IS_ERR(dentry)) {
1023 rc = PTR_ERR(dentry);
1024 CERROR("cannot lookup %s directory: rc = %d\n",
1025 MOUNT_CONFIGS_DIR, rc);
1029 /* erase the old tmp log */
1030 rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1031 if (rc < 0 && rc != -ENOENT) {
1032 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1037 /* write temporary log */
1038 rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1041 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1043 GOTO(out_close, rc);
1045 rc = sptlrpc_record_rules(llh, conf);
1048 llog_close(NULL, llh);
1050 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1051 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1055 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1056 llog_ctxt_put(ctxt);
1057 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1062 static int local_read_handler(const struct lu_env *env,
1063 struct llog_handle *llh,
1064 struct llog_rec_hdr *rec, void *data)
1066 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1067 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1071 if (rec->lrh_type != OBD_CFG_REC) {
1072 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1076 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1077 sizeof(struct llog_rec_tail);
1079 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1081 CERROR("Insane cfg\n");
1085 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1086 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1090 RETURN(__sptlrpc_process_config(lcfg, conf));
1094 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1095 struct sptlrpc_conf *conf)
1097 struct llog_handle *llh = NULL;
1098 struct llog_ctxt *ctxt;
1099 struct lvfs_run_ctxt saved;
1103 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1105 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1107 CERROR("missing llog context\n");
1111 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1113 rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1120 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1122 GOTO(out_close, rc);
1124 if (llog_get_size(llh) <= 1) {
1125 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1126 GOTO(out_close, rc = 0);
1129 rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1134 sptlrpc_conf_free_rsets(conf);
1138 llog_close(NULL, llh);
1140 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1141 llog_ctxt_put(ctxt);
1142 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1149 * called by target devices, extract sptlrpc rules which applies to
1150 * this target, to be used for future rpc flavor checking.
1152 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1153 struct sptlrpc_rule_set *rset,
1156 struct sptlrpc_conf *conf;
1157 struct sptlrpc_conf_tgt *conf_tgt;
1158 enum lustre_sec_part sp_dst;
1159 char fsname[MTI_NAME_MAXLEN];
1163 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1164 sp_dst = LUSTRE_SP_MDT;
1165 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1166 sp_dst = LUSTRE_SP_OST;
1168 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1171 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1173 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1175 mutex_lock(&sptlrpc_conf_lock);
1177 conf = sptlrpc_conf_get(fsname, 0);
1179 CERROR("missing sptlrpc config log\n");
1183 if (conf->sc_updated == 0) {
1185 * always read from local copy. here another option is
1186 * if we already have a local copy (read from another
1187 * target device hosted on the same node) we simply use that.
1190 sptlrpc_conf_free_rsets(conf);
1192 sptlrpc_target_local_read_conf(obd, conf);
1194 LASSERT(conf->sc_local == 0);
1196 /* write a local copy */
1197 if (initial || conf->sc_modified)
1198 sptlrpc_target_local_copy_conf(obd, conf);
1200 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1203 /* extract rule set for this target */
1204 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1206 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1207 conf_tgt ? &conf_tgt->sct_rset: NULL,
1208 LUSTRE_SP_ANY, sp_dst, rset);
1210 mutex_unlock(&sptlrpc_conf_lock);
1214 int sptlrpc_conf_init(void)
1216 INIT_LIST_HEAD(&sptlrpc_confs);
1217 mutex_init(&sptlrpc_conf_lock);
1221 void sptlrpc_conf_fini(void)
1223 struct sptlrpc_conf *conf, *conf_next;
1225 mutex_lock(&sptlrpc_conf_lock);
1226 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
1227 sptlrpc_conf_free(conf);
1228 LASSERT(list_empty(&sptlrpc_confs));
1229 mutex_unlock(&sptlrpc_conf_lock);