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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_SEC
39 #include <libcfs/libcfs.h>
40 #include <linux/crypto.h>
41 #include <linux/key.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre_net.h>
47 #include <lustre_import.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <lustre_dlm.h>
51 #include <lustre_param.h>
52 #include <lustre_sec.h>
54 #include "ptlrpc_internal.h"
56 const char *sptlrpc_part2name(enum lustre_sec_part part)
75 EXPORT_SYMBOL(sptlrpc_part2name);
77 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
79 const char *type = obd->obd_type->typ_name;
81 if (!strcmp(type, LUSTRE_MDT_NAME))
83 if (!strcmp(type, LUSTRE_OST_NAME))
85 if (!strcmp(type, LUSTRE_MGS_NAME))
88 CERROR("unknown target %p(%s)\n", obd, type);
92 /****************************************
93 * user supplied flavor string parsing *
94 ****************************************/
97 * format: <base_flavor>[-<bulk_type:alg_spec>]
99 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
104 memset(flvr, 0, sizeof(*flvr));
106 if (str == NULL || str[0] == '\0') {
107 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
111 strlcpy(buf, str, sizeof(buf));
113 bulk = strchr(buf, '-');
117 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
118 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
122 * currently only base flavor "plain" can have bulk specification.
124 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
125 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
128 * format: plain-hash:<hash_alg>
130 alg = strchr(bulk, ':');
135 if (strcmp(bulk, "hash"))
138 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
139 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
143 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
144 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
146 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
156 CERROR("invalid flavor string: %s\n", str);
159 EXPORT_SYMBOL(sptlrpc_parse_flavor);
161 /****************************************
163 ****************************************/
165 static void get_default_flavor(struct sptlrpc_flavor *sf)
167 memset(sf, 0, sizeof(*sf));
169 sf->sf_rpc = SPTLRPC_FLVR_NULL;
173 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
175 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
176 rule->sr_from = LUSTRE_SP_ANY;
177 rule->sr_to = LUSTRE_SP_ANY;
178 rule->sr_padding = 0;
180 get_default_flavor(&rule->sr_flvr);
184 * format: network[.direction]=flavor
186 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
191 sptlrpc_rule_init(rule);
193 flavor = strchr(param, '=');
194 if (flavor == NULL) {
195 CERROR("invalid param, no '='\n");
200 dir = strchr(param, '.');
205 if (strcmp(param, "default")) {
206 rule->sr_netid = libcfs_str2net(param);
207 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
208 CERROR("invalid network name: %s\n", param);
215 if (!strcmp(dir, "mdt2ost")) {
216 rule->sr_from = LUSTRE_SP_MDT;
217 rule->sr_to = LUSTRE_SP_OST;
218 } else if (!strcmp(dir, "mdt2mdt")) {
219 rule->sr_from = LUSTRE_SP_MDT;
220 rule->sr_to = LUSTRE_SP_MDT;
221 } else if (!strcmp(dir, "cli2ost")) {
222 rule->sr_from = LUSTRE_SP_CLI;
223 rule->sr_to = LUSTRE_SP_OST;
224 } else if (!strcmp(dir, "cli2mdt")) {
225 rule->sr_from = LUSTRE_SP_CLI;
226 rule->sr_to = LUSTRE_SP_MDT;
228 CERROR("invalid rule dir segment: %s\n", dir);
234 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
240 EXPORT_SYMBOL(sptlrpc_parse_rule);
242 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
244 LASSERT(rset->srs_nslot ||
245 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
247 if (rset->srs_nslot) {
248 OBD_FREE(rset->srs_rules,
249 rset->srs_nslot * sizeof(*rset->srs_rules));
250 sptlrpc_rule_set_init(rset);
253 EXPORT_SYMBOL(sptlrpc_rule_set_free);
256 * return 0 if the rule set could accomodate one more rule.
258 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
260 struct sptlrpc_rule *rules;
265 if (rset->srs_nrule < rset->srs_nslot)
268 nslot = rset->srs_nslot + 8;
270 /* better use realloc() if available */
271 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
275 if (rset->srs_nrule) {
276 LASSERT(rset->srs_nslot && rset->srs_rules);
277 memcpy(rules, rset->srs_rules,
278 rset->srs_nrule * sizeof(*rset->srs_rules));
280 OBD_FREE(rset->srs_rules,
281 rset->srs_nslot * sizeof(*rset->srs_rules));
284 rset->srs_rules = rules;
285 rset->srs_nslot = nslot;
289 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
291 return (rule->sr_from != LUSTRE_SP_ANY ||
292 rule->sr_to != LUSTRE_SP_ANY);
294 static inline int rule_spec_net(struct sptlrpc_rule *rule)
296 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
298 static inline int rule_match_dir(struct sptlrpc_rule *r1,
299 struct sptlrpc_rule *r2)
301 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
303 static inline int rule_match_net(struct sptlrpc_rule *r1,
304 struct sptlrpc_rule *r2)
306 return (r1->sr_netid == r2->sr_netid);
310 * merge @rule into @rset.
311 * the @rset slots might be expanded.
313 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
314 struct sptlrpc_rule *rule)
316 struct sptlrpc_rule *p = rset->srs_rules;
317 int spec_dir, spec_net;
318 int rc, n, match = 0;
322 spec_net = rule_spec_net(rule);
323 spec_dir = rule_spec_dir(rule);
325 for (n = 0; n < rset->srs_nrule; n++) {
326 p = &rset->srs_rules[n];
328 /* test network match, if failed:
329 * - spec rule: skip rules which is also spec rule match, until
330 * we hit a wild rule, which means no more chance
331 * - wild rule: skip until reach the one which is also wild
334 if (!rule_match_net(p, rule)) {
336 if (rule_spec_net(p))
345 /* test dir match, same logic as net matching */
346 if (!rule_match_dir(p, rule)) {
348 if (rule_spec_dir(p))
363 LASSERT(n >= 0 && n < rset->srs_nrule);
365 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
366 /* remove this rule */
367 if (n < rset->srs_nrule - 1)
368 memmove(&rset->srs_rules[n],
369 &rset->srs_rules[n + 1],
370 (rset->srs_nrule - n - 1) *
374 /* override the rule */
375 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
378 LASSERT(n >= 0 && n <= rset->srs_nrule);
380 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
381 rc = sptlrpc_rule_set_expand(rset);
385 if (n < rset->srs_nrule)
386 memmove(&rset->srs_rules[n + 1],
388 (rset->srs_nrule - n) * sizeof(*rule));
389 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
392 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
398 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
401 * given from/to/nid, determine a matching flavor in ruleset.
402 * return 1 if a match found, otherwise return 0.
404 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
405 enum lustre_sec_part from,
406 enum lustre_sec_part to,
408 struct sptlrpc_flavor *sf)
410 struct sptlrpc_rule *r;
413 for (n = 0; n < rset->srs_nrule; n++) {
414 r = &rset->srs_rules[n];
416 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
417 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
418 LNET_NIDNET(nid) != r->sr_netid)
421 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
425 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
436 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
438 struct sptlrpc_rule *r;
441 for (n = 0; n < rset->srs_nrule; n++) {
442 r = &rset->srs_rules[n];
443 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
444 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
448 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
449 struct sptlrpc_rule_set *tgt,
450 enum lustre_sec_part from,
451 enum lustre_sec_part to,
452 struct sptlrpc_rule_set *rset)
454 struct sptlrpc_rule_set *src[2] = { gen, tgt };
455 struct sptlrpc_rule *rule;
460 /* merge general rules firstly, then target-specific rules */
461 for (i = 0; i < 2; i++) {
465 for (n = 0; n < src[i]->srs_nrule; n++) {
466 rule = &src[i]->srs_rules[n];
468 if (from != LUSTRE_SP_ANY &&
469 rule->sr_from != LUSTRE_SP_ANY &&
470 rule->sr_from != from)
472 if (to != LUSTRE_SP_ANY &&
473 rule->sr_to != LUSTRE_SP_ANY &&
477 rc = sptlrpc_rule_set_merge(rset, rule);
479 CERROR("can't merge: %d\n", rc);
488 /**********************************
489 * sptlrpc configuration support *
490 **********************************/
492 struct sptlrpc_conf_tgt {
493 struct list_head sct_list;
494 char sct_name[MAX_OBD_NAME];
495 struct sptlrpc_rule_set sct_rset;
498 struct sptlrpc_conf {
499 struct list_head sc_list;
500 char sc_fsname[MTI_NAME_MAXLEN];
501 unsigned int sc_modified; /* modified during updating */
502 unsigned int sc_updated:1, /* updated copy from MGS */
503 sc_local:1; /* local copy from target */
504 struct sptlrpc_rule_set sc_rset; /* fs general rules */
505 struct list_head sc_tgts; /* target-specific rules */
508 static struct mutex sptlrpc_conf_lock;
509 static struct list_head sptlrpc_confs;
511 static inline int is_hex(char c)
513 return ((c >= '0' && c <= '9') ||
514 (c >= 'a' && c <= 'f'));
517 static void target2fsname(const char *tgt, char *fsname, int buflen)
522 ptr = strrchr(tgt, '-');
524 if ((strncmp(ptr, "-MDT", 4) != 0 &&
525 strncmp(ptr, "-OST", 4) != 0) ||
526 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
527 !is_hex(ptr[6]) || !is_hex(ptr[7]))
531 /* if we didn't find the pattern, treat the whole string as fsname */
537 len = min(len, buflen - 1);
538 memcpy(fsname, tgt, len);
542 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
544 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
546 sptlrpc_rule_set_free(&conf->sc_rset);
548 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
549 &conf->sc_tgts, sct_list) {
550 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
551 list_del(&conf_tgt->sct_list);
552 OBD_FREE_PTR(conf_tgt);
554 LASSERT(list_empty(&conf->sc_tgts));
556 conf->sc_updated = 0;
560 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
562 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
564 sptlrpc_conf_free_rsets(conf);
565 list_del(&conf->sc_list);
570 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
574 struct sptlrpc_conf_tgt *conf_tgt;
576 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
577 if (strcmp(conf_tgt->sct_name, name) == 0)
584 OBD_ALLOC_PTR(conf_tgt);
586 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
587 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
588 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
595 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
598 struct sptlrpc_conf *conf;
600 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
601 if (strcmp(conf->sc_fsname, fsname) == 0)
612 if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
613 sizeof(conf->sc_fsname)) {
617 sptlrpc_rule_set_init(&conf->sc_rset);
618 INIT_LIST_HEAD(&conf->sc_tgts);
619 list_add(&conf->sc_list, &sptlrpc_confs);
621 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
626 * caller must hold conf_lock already.
628 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
630 struct sptlrpc_rule *rule)
632 struct sptlrpc_conf_tgt *conf_tgt;
633 struct sptlrpc_rule_set *rule_set;
635 /* fsname == target means general rules for the whole fs */
636 if (strcmp(conf->sc_fsname, target) == 0) {
637 rule_set = &conf->sc_rset;
639 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
641 rule_set = &conf_tgt->sct_rset;
643 CERROR("out of memory, can't merge rule!\n");
648 return sptlrpc_rule_set_merge(rule_set, rule);
652 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
653 * find one through the target name in the record inside conf_lock;
654 * otherwise means caller already hold conf_lock.
656 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
657 struct sptlrpc_conf *conf)
659 char *target, *param;
660 char fsname[MTI_NAME_MAXLEN];
661 struct sptlrpc_rule rule;
665 target = lustre_cfg_string(lcfg, 1);
666 if (target == NULL) {
667 CERROR("missing target name\n");
671 param = lustre_cfg_string(lcfg, 2);
673 CERROR("missing parameter\n");
677 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
679 /* parse rule to make sure the format is correct */
680 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
681 CERROR("Invalid sptlrpc parameter: %s\n", param);
684 param += sizeof(PARAM_SRPC_FLVR) - 1;
686 rc = sptlrpc_parse_rule(param, &rule);
691 target2fsname(target, fsname, sizeof(fsname));
693 mutex_lock(&sptlrpc_conf_lock);
694 conf = sptlrpc_conf_get(fsname, 0);
696 CERROR("can't find conf\n");
699 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
701 mutex_unlock(&sptlrpc_conf_lock);
703 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
704 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
713 int sptlrpc_process_config(struct lustre_cfg *lcfg)
715 return __sptlrpc_process_config(lcfg, NULL);
717 EXPORT_SYMBOL(sptlrpc_process_config);
719 static int logname2fsname(const char *logname, char *buf, int buflen)
724 ptr = strrchr(logname, '-');
725 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
726 CERROR("%s is not a sptlrpc config log\n", logname);
730 len = min((int) (ptr - logname), buflen - 1);
732 memcpy(buf, logname, len);
737 void sptlrpc_conf_log_update_begin(const char *logname)
739 struct sptlrpc_conf *conf;
742 if (logname2fsname(logname, fsname, sizeof(fsname)))
745 mutex_lock(&sptlrpc_conf_lock);
747 conf = sptlrpc_conf_get(fsname, 0);
749 if (conf->sc_local) {
750 LASSERT(conf->sc_updated == 0);
751 sptlrpc_conf_free_rsets(conf);
753 conf->sc_modified = 0;
756 mutex_unlock(&sptlrpc_conf_lock);
758 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
761 * mark a config log has been updated
763 void sptlrpc_conf_log_update_end(const char *logname)
765 struct sptlrpc_conf *conf;
768 if (logname2fsname(logname, fsname, sizeof(fsname)))
771 mutex_lock(&sptlrpc_conf_lock);
773 conf = sptlrpc_conf_get(fsname, 0);
776 * if original state is not updated, make sure the
777 * modified counter > 0 to enforce updating local copy.
779 if (conf->sc_updated == 0)
782 conf->sc_updated = 1;
785 mutex_unlock(&sptlrpc_conf_lock);
787 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
789 void sptlrpc_conf_log_start(const char *logname)
793 if (logname2fsname(logname, fsname, sizeof(fsname)))
796 mutex_lock(&sptlrpc_conf_lock);
797 sptlrpc_conf_get(fsname, 1);
798 mutex_unlock(&sptlrpc_conf_lock);
800 EXPORT_SYMBOL(sptlrpc_conf_log_start);
802 void sptlrpc_conf_log_stop(const char *logname)
804 struct sptlrpc_conf *conf;
807 if (logname2fsname(logname, fsname, sizeof(fsname)))
810 mutex_lock(&sptlrpc_conf_lock);
811 conf = sptlrpc_conf_get(fsname, 0);
813 sptlrpc_conf_free(conf);
814 mutex_unlock(&sptlrpc_conf_lock);
816 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
818 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
819 enum lustre_sec_part from,
820 enum lustre_sec_part to,
821 unsigned int fl_udesc)
824 * null flavor doesn't need to set any flavor, and in fact
825 * we'd better not do that because everybody share a single sec.
827 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
830 if (from == LUSTRE_SP_MDT) {
831 /* MDT->MDT; MDT->OST */
832 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
833 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
835 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
836 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
838 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
839 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
842 /* Some flavors use a single uid (0) context */
843 if (flvr_is_rootonly(sf->sf_rpc))
844 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
846 /* User descriptor might need to be cleared */
847 if (flvr_allows_user_desc(sf->sf_rpc) == 0)
848 sf->sf_flags &= ~PTLRPC_SEC_FL_UDESC;
851 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
852 enum lustre_sec_part to,
853 struct obd_uuid *target,
855 struct sptlrpc_flavor *sf)
857 struct sptlrpc_conf *conf;
858 struct sptlrpc_conf_tgt *conf_tgt;
859 char name[MTI_NAME_MAXLEN];
862 target2fsname(target->uuid, name, sizeof(name));
864 mutex_lock(&sptlrpc_conf_lock);
866 conf = sptlrpc_conf_get(name, 0);
870 /* convert uuid name (supposed end with _UUID) to target name */
871 len = strlen(target->uuid);
873 memcpy(name, target->uuid, len - 5);
874 name[len - 5] = '\0';
876 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
878 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
884 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
886 mutex_unlock(&sptlrpc_conf_lock);
889 get_default_flavor(sf);
891 flavor_set_flags(sf, from, to, 1);
895 * called by target devices, determine the expected flavor from
896 * certain peer (from, nid).
898 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
899 enum lustre_sec_part from,
901 struct sptlrpc_flavor *sf)
903 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
904 get_default_flavor(sf);
907 #define SEC_ADAPT_DELAY (10)
910 * called by client devices, notify the sptlrpc config has changed and
911 * do import_sec_adapt later.
913 void sptlrpc_conf_client_adapt(struct obd_device *obd)
915 struct obd_import *imp;
918 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
919 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
920 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0 ||
921 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0);
922 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
924 /* serialize with connect/disconnect import */
925 down_read_nested(&obd->u.cli.cl_sem, OBD_CLI_SEM_MDCOSC);
927 imp = obd->u.cli.cl_import;
929 spin_lock(&imp->imp_lock);
931 imp->imp_sec_expire = cfs_time_current_sec() +
933 spin_unlock(&imp->imp_lock);
936 up_read(&obd->u.cli.cl_sem);
939 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
942 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
945 char net[LNET_NIDSTR_SIZE] = "default";
948 if (r->sr_netid != LNET_NIDNET(LNET_NID_ANY))
949 libcfs_net2str_r(r->sr_netid, net, sizeof(net));
951 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
954 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
955 sptlrpc_part2name(r->sr_from),
956 sptlrpc_part2name(r->sr_to));
958 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
960 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
961 buf[buflen - 1] = '\0';
964 static int sptlrpc_record_rule_set(struct llog_handle *llh,
966 struct sptlrpc_rule_set *rset)
968 struct llog_cfg_rec *lcr;
969 struct lustre_cfg_bufs bufs;
973 for (i = 0; i < rset->srs_nrule; i++) {
974 rule2string(&rset->srs_rules[i], param, sizeof(param));
976 lustre_cfg_bufs_reset(&bufs, NULL);
977 lustre_cfg_bufs_set_string(&bufs, 1, target);
978 lustre_cfg_bufs_set_string(&bufs, 2, param);
979 lcr = lustre_cfg_rec_new(LCFG_SPTLRPC_CONF, &bufs);
982 rc = llog_write(NULL, llh, &lcr->lcr_hdr, LLOG_NEXT_IDX);
983 lustre_cfg_rec_free(lcr);
990 static int sptlrpc_record_rules(struct llog_handle *llh,
991 struct sptlrpc_conf *conf)
993 struct sptlrpc_conf_tgt *conf_tgt;
995 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
997 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
998 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
999 &conf_tgt->sct_rset);
1004 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1005 #define LOG_SPTLRPC "sptlrpc"
1008 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1009 struct sptlrpc_conf *conf)
1011 struct llog_handle *llh = NULL;
1012 struct llog_ctxt *ctxt;
1013 struct lvfs_run_ctxt saved;
1014 struct dentry *dentry;
1018 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1022 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1024 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd.dentry,
1025 strlen(MOUNT_CONFIGS_DIR));
1026 if (IS_ERR(dentry)) {
1027 rc = PTR_ERR(dentry);
1028 CERROR("cannot lookup %s directory: rc = %d\n",
1029 MOUNT_CONFIGS_DIR, rc);
1033 /* erase the old tmp log */
1034 rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1035 if (rc < 0 && rc != -ENOENT) {
1036 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1041 /* write temporary log */
1042 rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1045 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1047 GOTO(out_close, rc);
1049 rc = sptlrpc_record_rules(llh, conf);
1052 llog_close(NULL, llh);
1054 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1055 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1059 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1060 llog_ctxt_put(ctxt);
1061 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1066 static int local_read_handler(const struct lu_env *env,
1067 struct llog_handle *llh,
1068 struct llog_rec_hdr *rec, void *data)
1070 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1071 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1075 if (rec->lrh_type != OBD_CFG_REC) {
1076 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1080 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1081 sizeof(struct llog_rec_tail);
1083 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1085 CERROR("Insane cfg\n");
1089 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1090 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1094 RETURN(__sptlrpc_process_config(lcfg, conf));
1098 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1099 struct sptlrpc_conf *conf)
1101 struct llog_handle *llh = NULL;
1102 struct llog_ctxt *ctxt;
1103 struct lvfs_run_ctxt saved;
1107 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1109 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1111 CERROR("missing llog context\n");
1115 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1117 rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1124 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1126 GOTO(out_close, rc);
1128 if (llog_get_size(llh) <= 1) {
1129 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1130 GOTO(out_close, rc = 0);
1133 rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1138 sptlrpc_conf_free_rsets(conf);
1142 llog_close(NULL, llh);
1144 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1145 llog_ctxt_put(ctxt);
1146 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1153 * called by target devices, extract sptlrpc rules which applies to
1154 * this target, to be used for future rpc flavor checking.
1156 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1157 struct sptlrpc_rule_set *rset,
1160 struct sptlrpc_conf *conf;
1161 struct sptlrpc_conf_tgt *conf_tgt;
1162 enum lustre_sec_part sp_dst;
1163 char fsname[MTI_NAME_MAXLEN];
1167 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1168 sp_dst = LUSTRE_SP_MDT;
1169 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1170 sp_dst = LUSTRE_SP_OST;
1172 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1175 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1177 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1179 mutex_lock(&sptlrpc_conf_lock);
1181 conf = sptlrpc_conf_get(fsname, 0);
1183 CERROR("missing sptlrpc config log\n");
1187 if (conf->sc_updated == 0) {
1189 * always read from local copy. here another option is
1190 * if we already have a local copy (read from another
1191 * target device hosted on the same node) we simply use that.
1194 sptlrpc_conf_free_rsets(conf);
1196 sptlrpc_target_local_read_conf(obd, conf);
1198 LASSERT(conf->sc_local == 0);
1200 /* write a local copy */
1201 if (initial || conf->sc_modified)
1202 sptlrpc_target_local_copy_conf(obd, conf);
1204 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1207 /* extract rule set for this target */
1208 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1210 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1211 conf_tgt ? &conf_tgt->sct_rset: NULL,
1212 LUSTRE_SP_ANY, sp_dst, rset);
1214 mutex_unlock(&sptlrpc_conf_lock);
1218 int sptlrpc_conf_init(void)
1220 INIT_LIST_HEAD(&sptlrpc_confs);
1221 mutex_init(&sptlrpc_conf_lock);
1225 void sptlrpc_conf_fini(void)
1227 struct sptlrpc_conf *conf, *conf_next;
1229 mutex_lock(&sptlrpc_conf_lock);
1230 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
1231 sptlrpc_conf_free(conf);
1232 LASSERT(list_empty(&sptlrpc_confs));
1233 mutex_unlock(&sptlrpc_conf_lock);