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, 2012, 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>
41 #include <liblustre.h>
42 #include <libcfs/list.h>
44 #include <linux/crypto.h>
45 #include <linux/key.h>
49 #include <obd_class.h>
50 #include <obd_support.h>
51 #include <lustre_net.h>
52 #include <lustre_import.h>
53 #include <lustre_log.h>
54 #include <lustre_disk.h>
55 #include <lustre_dlm.h>
56 #include <lustre_param.h>
57 #include <lustre_sec.h>
59 #include "ptlrpc_internal.h"
61 const char *sptlrpc_part2name(enum lustre_sec_part part)
80 EXPORT_SYMBOL(sptlrpc_part2name);
82 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
84 const char *type = obd->obd_type->typ_name;
86 if (!strcmp(type, LUSTRE_MDT_NAME))
88 if (!strcmp(type, LUSTRE_OST_NAME))
90 if (!strcmp(type, LUSTRE_MGS_NAME))
93 CERROR("unknown target %p(%s)\n", obd, type);
96 EXPORT_SYMBOL(sptlrpc_target_sec_part);
98 /****************************************
99 * user supplied flavor string parsing *
100 ****************************************/
103 * format: <base_flavor>[-<bulk_type:alg_spec>]
105 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
110 memset(flvr, 0, sizeof(*flvr));
112 if (str == NULL || str[0] == '\0') {
113 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
117 strlcpy(buf, str, sizeof(buf));
119 bulk = strchr(buf, '-');
123 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
124 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
128 * currently only base flavor "plain" can have bulk specification.
130 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
131 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
134 * format: plain-hash:<hash_alg>
136 alg = strchr(bulk, ':');
141 if (strcmp(bulk, "hash"))
144 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
145 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
149 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
150 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
152 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
162 CERROR("invalid flavor string: %s\n", str);
165 EXPORT_SYMBOL(sptlrpc_parse_flavor);
167 /****************************************
169 ****************************************/
171 static void get_default_flavor(struct sptlrpc_flavor *sf)
173 memset(sf, 0, sizeof(*sf));
175 sf->sf_rpc = SPTLRPC_FLVR_NULL;
179 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
181 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
182 rule->sr_from = LUSTRE_SP_ANY;
183 rule->sr_to = LUSTRE_SP_ANY;
184 rule->sr_padding = 0;
186 get_default_flavor(&rule->sr_flvr);
190 * format: network[.direction]=flavor
192 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
197 sptlrpc_rule_init(rule);
199 flavor = strchr(param, '=');
200 if (flavor == NULL) {
201 CERROR("invalid param, no '='\n");
206 dir = strchr(param, '.');
211 if (strcmp(param, "default")) {
212 rule->sr_netid = libcfs_str2net(param);
213 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
214 CERROR("invalid network name: %s\n", param);
221 if (!strcmp(dir, "mdt2ost")) {
222 rule->sr_from = LUSTRE_SP_MDT;
223 rule->sr_to = LUSTRE_SP_OST;
224 } else if (!strcmp(dir, "mdt2mdt")) {
225 rule->sr_from = LUSTRE_SP_MDT;
226 rule->sr_to = LUSTRE_SP_MDT;
227 } else if (!strcmp(dir, "cli2ost")) {
228 rule->sr_from = LUSTRE_SP_CLI;
229 rule->sr_to = LUSTRE_SP_OST;
230 } else if (!strcmp(dir, "cli2mdt")) {
231 rule->sr_from = LUSTRE_SP_CLI;
232 rule->sr_to = LUSTRE_SP_MDT;
234 CERROR("invalid rule dir segment: %s\n", dir);
240 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
246 EXPORT_SYMBOL(sptlrpc_parse_rule);
248 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
250 LASSERT(rset->srs_nslot ||
251 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
253 if (rset->srs_nslot) {
254 OBD_FREE(rset->srs_rules,
255 rset->srs_nslot * sizeof(*rset->srs_rules));
256 sptlrpc_rule_set_init(rset);
259 EXPORT_SYMBOL(sptlrpc_rule_set_free);
262 * return 0 if the rule set could accomodate one more rule.
264 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
266 struct sptlrpc_rule *rules;
271 if (rset->srs_nrule < rset->srs_nslot)
274 nslot = rset->srs_nslot + 8;
276 /* better use realloc() if available */
277 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
281 if (rset->srs_nrule) {
282 LASSERT(rset->srs_nslot && rset->srs_rules);
283 memcpy(rules, rset->srs_rules,
284 rset->srs_nrule * sizeof(*rset->srs_rules));
286 OBD_FREE(rset->srs_rules,
287 rset->srs_nslot * sizeof(*rset->srs_rules));
290 rset->srs_rules = rules;
291 rset->srs_nslot = nslot;
294 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
296 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
298 return (rule->sr_from != LUSTRE_SP_ANY ||
299 rule->sr_to != LUSTRE_SP_ANY);
301 static inline int rule_spec_net(struct sptlrpc_rule *rule)
303 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
305 static inline int rule_match_dir(struct sptlrpc_rule *r1,
306 struct sptlrpc_rule *r2)
308 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
310 static inline int rule_match_net(struct sptlrpc_rule *r1,
311 struct sptlrpc_rule *r2)
313 return (r1->sr_netid == r2->sr_netid);
317 * merge @rule into @rset.
318 * the @rset slots might be expanded.
320 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
321 struct sptlrpc_rule *rule)
323 struct sptlrpc_rule *p = rset->srs_rules;
324 int spec_dir, spec_net;
325 int rc, n, match = 0;
329 spec_net = rule_spec_net(rule);
330 spec_dir = rule_spec_dir(rule);
332 for (n = 0; n < rset->srs_nrule; n++) {
333 p = &rset->srs_rules[n];
335 /* test network match, if failed:
336 * - spec rule: skip rules which is also spec rule match, until
337 * we hit a wild rule, which means no more chance
338 * - wild rule: skip until reach the one which is also wild
341 if (!rule_match_net(p, rule)) {
343 if (rule_spec_net(p))
352 /* test dir match, same logic as net matching */
353 if (!rule_match_dir(p, rule)) {
355 if (rule_spec_dir(p))
370 LASSERT(n >= 0 && n < rset->srs_nrule);
372 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
373 /* remove this rule */
374 if (n < rset->srs_nrule - 1)
375 memmove(&rset->srs_rules[n],
376 &rset->srs_rules[n + 1],
377 (rset->srs_nrule - n - 1) *
381 /* override the rule */
382 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
385 LASSERT(n >= 0 && n <= rset->srs_nrule);
387 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
388 rc = sptlrpc_rule_set_expand(rset);
392 if (n < rset->srs_nrule)
393 memmove(&rset->srs_rules[n + 1],
395 (rset->srs_nrule - n) * sizeof(*rule));
396 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
399 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
405 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
408 * given from/to/nid, determine a matching flavor in ruleset.
409 * return 1 if a match found, otherwise return 0.
411 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
412 enum lustre_sec_part from,
413 enum lustre_sec_part to,
415 struct sptlrpc_flavor *sf)
417 struct sptlrpc_rule *r;
420 for (n = 0; n < rset->srs_nrule; n++) {
421 r = &rset->srs_rules[n];
423 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
424 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
425 LNET_NIDNET(nid) != r->sr_netid)
428 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
432 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
442 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
444 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
446 struct sptlrpc_rule *r;
449 for (n = 0; n < rset->srs_nrule; n++) {
450 r = &rset->srs_rules[n];
451 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
452 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
455 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
457 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
458 struct sptlrpc_rule_set *tgt,
459 enum lustre_sec_part from,
460 enum lustre_sec_part to,
461 struct sptlrpc_rule_set *rset)
463 struct sptlrpc_rule_set *src[2] = { gen, tgt };
464 struct sptlrpc_rule *rule;
469 /* merge general rules firstly, then target-specific rules */
470 for (i = 0; i < 2; i++) {
474 for (n = 0; n < src[i]->srs_nrule; n++) {
475 rule = &src[i]->srs_rules[n];
477 if (from != LUSTRE_SP_ANY &&
478 rule->sr_from != LUSTRE_SP_ANY &&
479 rule->sr_from != from)
481 if (to != LUSTRE_SP_ANY &&
482 rule->sr_to != LUSTRE_SP_ANY &&
486 rc = sptlrpc_rule_set_merge(rset, rule);
488 CERROR("can't merge: %d\n", rc);
497 /**********************************
498 * sptlrpc configuration support *
499 **********************************/
501 struct sptlrpc_conf_tgt {
502 struct list_head sct_list;
503 char sct_name[MAX_OBD_NAME];
504 struct sptlrpc_rule_set sct_rset;
507 struct sptlrpc_conf {
508 struct list_head sc_list;
509 char sc_fsname[MTI_NAME_MAXLEN];
510 unsigned int sc_modified; /* modified during updating */
511 unsigned int sc_updated:1, /* updated copy from MGS */
512 sc_local:1; /* local copy from target */
513 struct sptlrpc_rule_set sc_rset; /* fs general rules */
514 struct list_head sc_tgts; /* target-specific rules */
517 static struct mutex sptlrpc_conf_lock;
518 static struct list_head sptlrpc_confs;
520 static inline int is_hex(char c)
522 return ((c >= '0' && c <= '9') ||
523 (c >= 'a' && c <= 'f'));
526 static void target2fsname(const char *tgt, char *fsname, int buflen)
531 ptr = strrchr(tgt, '-');
533 if ((strncmp(ptr, "-MDT", 4) != 0 &&
534 strncmp(ptr, "-OST", 4) != 0) ||
535 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
536 !is_hex(ptr[6]) || !is_hex(ptr[7]))
540 /* if we didn't find the pattern, treat the whole string as fsname */
546 len = min(len, buflen - 1);
547 memcpy(fsname, tgt, len);
551 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
553 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
555 sptlrpc_rule_set_free(&conf->sc_rset);
557 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
558 &conf->sc_tgts, sct_list) {
559 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
560 list_del(&conf_tgt->sct_list);
561 OBD_FREE_PTR(conf_tgt);
563 LASSERT(list_empty(&conf->sc_tgts));
565 conf->sc_updated = 0;
569 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
571 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
573 sptlrpc_conf_free_rsets(conf);
574 list_del(&conf->sc_list);
579 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
583 struct sptlrpc_conf_tgt *conf_tgt;
585 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
586 if (strcmp(conf_tgt->sct_name, name) == 0)
593 OBD_ALLOC_PTR(conf_tgt);
595 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
596 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
597 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
604 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
607 struct sptlrpc_conf *conf;
609 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
610 if (strcmp(conf->sc_fsname, fsname) == 0)
621 if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
622 sizeof(conf->sc_fsname)) {
626 sptlrpc_rule_set_init(&conf->sc_rset);
627 INIT_LIST_HEAD(&conf->sc_tgts);
628 list_add(&conf->sc_list, &sptlrpc_confs);
630 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
635 * caller must hold conf_lock already.
637 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
639 struct sptlrpc_rule *rule)
641 struct sptlrpc_conf_tgt *conf_tgt;
642 struct sptlrpc_rule_set *rule_set;
644 /* fsname == target means general rules for the whole fs */
645 if (strcmp(conf->sc_fsname, target) == 0) {
646 rule_set = &conf->sc_rset;
648 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
650 rule_set = &conf_tgt->sct_rset;
652 CERROR("out of memory, can't merge rule!\n");
657 return sptlrpc_rule_set_merge(rule_set, rule);
661 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
662 * find one through the target name in the record inside conf_lock;
663 * otherwise means caller already hold conf_lock.
665 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
666 struct sptlrpc_conf *conf)
668 char *target, *param;
669 char fsname[MTI_NAME_MAXLEN];
670 struct sptlrpc_rule rule;
674 target = lustre_cfg_string(lcfg, 1);
675 if (target == NULL) {
676 CERROR("missing target name\n");
680 param = lustre_cfg_string(lcfg, 2);
682 CERROR("missing parameter\n");
686 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
688 /* parse rule to make sure the format is correct */
689 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
690 CERROR("Invalid sptlrpc parameter: %s\n", param);
693 param += sizeof(PARAM_SRPC_FLVR) - 1;
695 rc = sptlrpc_parse_rule(param, &rule);
700 target2fsname(target, fsname, sizeof(fsname));
702 mutex_lock(&sptlrpc_conf_lock);
703 conf = sptlrpc_conf_get(fsname, 0);
705 CERROR("can't find conf\n");
708 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
710 mutex_unlock(&sptlrpc_conf_lock);
712 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
713 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
722 int sptlrpc_process_config(struct lustre_cfg *lcfg)
724 return __sptlrpc_process_config(lcfg, NULL);
726 EXPORT_SYMBOL(sptlrpc_process_config);
728 static int logname2fsname(const char *logname, char *buf, int buflen)
733 ptr = strrchr(logname, '-');
734 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
735 CERROR("%s is not a sptlrpc config log\n", logname);
739 len = min((int) (ptr - logname), buflen - 1);
741 memcpy(buf, logname, len);
746 void sptlrpc_conf_log_update_begin(const char *logname)
748 struct sptlrpc_conf *conf;
751 if (logname2fsname(logname, fsname, sizeof(fsname)))
754 mutex_lock(&sptlrpc_conf_lock);
756 conf = sptlrpc_conf_get(fsname, 0);
758 if (conf->sc_local) {
759 LASSERT(conf->sc_updated == 0);
760 sptlrpc_conf_free_rsets(conf);
762 conf->sc_modified = 0;
765 mutex_unlock(&sptlrpc_conf_lock);
767 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
770 * mark a config log has been updated
772 void sptlrpc_conf_log_update_end(const char *logname)
774 struct sptlrpc_conf *conf;
777 if (logname2fsname(logname, fsname, sizeof(fsname)))
780 mutex_lock(&sptlrpc_conf_lock);
782 conf = sptlrpc_conf_get(fsname, 0);
785 * if original state is not updated, make sure the
786 * modified counter > 0 to enforce updating local copy.
788 if (conf->sc_updated == 0)
791 conf->sc_updated = 1;
794 mutex_unlock(&sptlrpc_conf_lock);
796 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
798 void sptlrpc_conf_log_start(const char *logname)
802 if (logname2fsname(logname, fsname, sizeof(fsname)))
805 mutex_lock(&sptlrpc_conf_lock);
806 sptlrpc_conf_get(fsname, 1);
807 mutex_unlock(&sptlrpc_conf_lock);
809 EXPORT_SYMBOL(sptlrpc_conf_log_start);
811 void sptlrpc_conf_log_stop(const char *logname)
813 struct sptlrpc_conf *conf;
816 if (logname2fsname(logname, fsname, sizeof(fsname)))
819 mutex_lock(&sptlrpc_conf_lock);
820 conf = sptlrpc_conf_get(fsname, 0);
822 sptlrpc_conf_free(conf);
823 mutex_unlock(&sptlrpc_conf_lock);
825 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
827 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
828 enum lustre_sec_part from,
829 enum lustre_sec_part to,
830 unsigned int fl_udesc)
833 * null flavor doesn't need to set any flavor, and in fact
834 * we'd better not do that because everybody share a single sec.
836 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
839 if (from == LUSTRE_SP_MDT) {
840 /* MDT->MDT; MDT->OST */
841 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
842 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
844 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
845 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
847 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
848 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
852 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
853 enum lustre_sec_part to,
854 struct obd_uuid *target,
856 struct sptlrpc_flavor *sf)
858 struct sptlrpc_conf *conf;
859 struct sptlrpc_conf_tgt *conf_tgt;
860 char name[MTI_NAME_MAXLEN];
863 target2fsname(target->uuid, name, sizeof(name));
865 mutex_lock(&sptlrpc_conf_lock);
867 conf = sptlrpc_conf_get(name, 0);
871 /* convert uuid name (supposed end with _UUID) to target name */
872 len = strlen(target->uuid);
874 memcpy(name, target->uuid, len - 5);
875 name[len - 5] = '\0';
877 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
879 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
885 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
887 mutex_unlock(&sptlrpc_conf_lock);
890 get_default_flavor(sf);
892 flavor_set_flags(sf, from, to, 1);
896 * called by target devices, determine the expected flavor from
897 * certain peer (from, nid).
899 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
900 enum lustre_sec_part from,
902 struct sptlrpc_flavor *sf)
904 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
905 get_default_flavor(sf);
907 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
909 #define SEC_ADAPT_DELAY (10)
912 * called by client devices, notify the sptlrpc config has changed and
913 * do import_sec_adapt later.
915 void sptlrpc_conf_client_adapt(struct obd_device *obd)
917 struct obd_import *imp;
920 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
921 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_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(&obd->u.cli.cl_sem);
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);
943 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
949 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
952 net = libcfs_net2str(r->sr_netid);
954 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
957 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
958 sptlrpc_part2name(r->sr_from),
959 sptlrpc_part2name(r->sr_to));
961 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
963 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
964 buf[buflen - 1] = '\0';
967 static int sptlrpc_record_rule_set(struct llog_handle *llh,
969 struct sptlrpc_rule_set *rset)
971 struct llog_cfg_rec *lcr;
972 struct lustre_cfg_bufs bufs;
976 for (i = 0; i < rset->srs_nrule; i++) {
977 rule2string(&rset->srs_rules[i], param, sizeof(param));
979 lustre_cfg_bufs_reset(&bufs, NULL);
980 lustre_cfg_bufs_set_string(&bufs, 1, target);
981 lustre_cfg_bufs_set_string(&bufs, 2, param);
982 lcr = lustre_cfg_rec_new(LCFG_SPTLRPC_CONF, &bufs);
985 rc = llog_write(NULL, llh, &lcr->lcr_hdr, LLOG_NEXT_IDX);
986 lustre_cfg_rec_free(lcr);
993 static int sptlrpc_record_rules(struct llog_handle *llh,
994 struct sptlrpc_conf *conf)
996 struct sptlrpc_conf_tgt *conf_tgt;
998 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
1000 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
1001 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
1002 &conf_tgt->sct_rset);
1007 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1008 #define LOG_SPTLRPC "sptlrpc"
1011 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1012 struct sptlrpc_conf *conf)
1014 struct llog_handle *llh = NULL;
1015 struct llog_ctxt *ctxt;
1016 struct lvfs_run_ctxt saved;
1017 struct dentry *dentry;
1021 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1025 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1027 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd.dentry,
1028 strlen(MOUNT_CONFIGS_DIR));
1029 if (IS_ERR(dentry)) {
1030 rc = PTR_ERR(dentry);
1031 CERROR("cannot lookup %s directory: rc = %d\n",
1032 MOUNT_CONFIGS_DIR, rc);
1036 /* erase the old tmp log */
1037 rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1038 if (rc < 0 && rc != -ENOENT) {
1039 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1044 /* write temporary log */
1045 rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1048 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1050 GOTO(out_close, rc);
1052 rc = sptlrpc_record_rules(llh, conf);
1055 llog_close(NULL, llh);
1057 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1058 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1062 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1063 llog_ctxt_put(ctxt);
1064 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1069 static int local_read_handler(const struct lu_env *env,
1070 struct llog_handle *llh,
1071 struct llog_rec_hdr *rec, void *data)
1073 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1074 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1078 if (rec->lrh_type != OBD_CFG_REC) {
1079 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1083 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1084 sizeof(struct llog_rec_tail);
1086 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1088 CERROR("Insane cfg\n");
1092 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1093 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1097 RETURN(__sptlrpc_process_config(lcfg, conf));
1101 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1102 struct sptlrpc_conf *conf)
1104 struct llog_handle *llh = NULL;
1105 struct llog_ctxt *ctxt;
1106 struct lvfs_run_ctxt saved;
1110 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1112 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1114 CERROR("missing llog context\n");
1118 push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1120 rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1127 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1129 GOTO(out_close, rc);
1131 if (llog_get_size(llh) <= 1) {
1132 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1133 GOTO(out_close, rc = 0);
1136 rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1141 sptlrpc_conf_free_rsets(conf);
1145 llog_close(NULL, llh);
1147 pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1148 llog_ctxt_put(ctxt);
1149 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1154 #endif /* __KRENEL__ */
1157 * called by target devices, extract sptlrpc rules which applies to
1158 * this target, to be used for future rpc flavor checking.
1160 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1161 struct sptlrpc_rule_set *rset,
1164 struct sptlrpc_conf *conf;
1165 struct sptlrpc_conf_tgt *conf_tgt;
1166 enum lustre_sec_part sp_dst;
1167 char fsname[MTI_NAME_MAXLEN];
1171 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1172 sp_dst = LUSTRE_SP_MDT;
1173 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1174 sp_dst = LUSTRE_SP_OST;
1176 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1179 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1181 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1183 mutex_lock(&sptlrpc_conf_lock);
1185 conf = sptlrpc_conf_get(fsname, 0);
1187 CERROR("missing sptlrpc config log\n");
1192 if (conf->sc_updated == 0) {
1194 * always read from local copy. here another option is
1195 * if we already have a local copy (read from another
1196 * target device hosted on the same node) we simply use that.
1199 sptlrpc_conf_free_rsets(conf);
1201 sptlrpc_target_local_read_conf(obd, conf);
1203 LASSERT(conf->sc_local == 0);
1205 /* write a local copy */
1206 if (initial || conf->sc_modified)
1207 sptlrpc_target_local_copy_conf(obd, conf);
1209 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1213 /* extract rule set for this target */
1214 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1216 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1217 conf_tgt ? &conf_tgt->sct_rset: NULL,
1218 LUSTRE_SP_ANY, sp_dst, rset);
1220 mutex_unlock(&sptlrpc_conf_lock);
1223 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1225 int sptlrpc_conf_init(void)
1227 INIT_LIST_HEAD(&sptlrpc_confs);
1228 mutex_init(&sptlrpc_conf_lock);
1232 void sptlrpc_conf_fini(void)
1234 struct sptlrpc_conf *conf, *conf_next;
1236 mutex_lock(&sptlrpc_conf_lock);
1237 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
1238 sptlrpc_conf_free(conf);
1239 LASSERT(list_empty(&sptlrpc_confs));
1240 mutex_unlock(&sptlrpc_conf_lock);