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 <linux/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);
91 EXPORT_SYMBOL(sptlrpc_target_sec_part);
93 /****************************************
94 * user supplied flavor string parsing *
95 ****************************************/
98 * format: <base_flavor>[-<bulk_type:alg_spec>]
100 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
105 memset(flvr, 0, sizeof(*flvr));
107 if (str == NULL || str[0] == '\0') {
108 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
112 strncpy(buf, str, sizeof(buf));
113 buf[sizeof(buf) - 1] = '\0';
115 bulk = strchr(buf, '-');
119 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
120 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
124 * currently only base flavor "plain" can have bulk specification.
126 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
127 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
130 * format: plain-hash:<hash_alg>
132 alg = strchr(bulk, ':');
137 if (strcmp(bulk, "hash"))
140 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
141 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
145 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
146 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
148 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
158 CERROR("invalid flavor string: %s\n", str);
161 EXPORT_SYMBOL(sptlrpc_parse_flavor);
163 /****************************************
165 ****************************************/
167 static void get_default_flavor(struct sptlrpc_flavor *sf)
169 memset(sf, 0, sizeof(*sf));
171 sf->sf_rpc = SPTLRPC_FLVR_NULL;
175 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
177 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
178 rule->sr_from = LUSTRE_SP_ANY;
179 rule->sr_to = LUSTRE_SP_ANY;
180 rule->sr_padding = 0;
182 get_default_flavor(&rule->sr_flvr);
186 * format: network[.direction]=flavor
188 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
193 sptlrpc_rule_init(rule);
195 flavor = strchr(param, '=');
196 if (flavor == NULL) {
197 CERROR("invalid param, no '='\n");
202 dir = strchr(param, '.');
207 if (strcmp(param, "default")) {
208 rule->sr_netid = libcfs_str2net(param);
209 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
210 CERROR("invalid network name: %s\n", param);
217 if (!strcmp(dir, "mdt2ost")) {
218 rule->sr_from = LUSTRE_SP_MDT;
219 rule->sr_to = LUSTRE_SP_OST;
220 } else if (!strcmp(dir, "mdt2mdt")) {
221 rule->sr_from = LUSTRE_SP_MDT;
222 rule->sr_to = LUSTRE_SP_MDT;
223 } else if (!strcmp(dir, "cli2ost")) {
224 rule->sr_from = LUSTRE_SP_CLI;
225 rule->sr_to = LUSTRE_SP_OST;
226 } else if (!strcmp(dir, "cli2mdt")) {
227 rule->sr_from = LUSTRE_SP_CLI;
228 rule->sr_to = LUSTRE_SP_MDT;
230 CERROR("invalid rule dir segment: %s\n", dir);
236 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
242 EXPORT_SYMBOL(sptlrpc_parse_rule);
244 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
246 LASSERT(rset->srs_nslot ||
247 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
249 if (rset->srs_nslot) {
250 OBD_FREE(rset->srs_rules,
251 rset->srs_nslot * sizeof(*rset->srs_rules));
252 sptlrpc_rule_set_init(rset);
255 EXPORT_SYMBOL(sptlrpc_rule_set_free);
258 * return 0 if the rule set could accomodate one more rule.
260 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
262 struct sptlrpc_rule *rules;
267 if (rset->srs_nrule < rset->srs_nslot)
270 nslot = rset->srs_nslot + 8;
272 /* better use realloc() if available */
273 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
277 if (rset->srs_nrule) {
278 LASSERT(rset->srs_nslot && rset->srs_rules);
279 memcpy(rules, rset->srs_rules,
280 rset->srs_nrule * sizeof(*rset->srs_rules));
282 OBD_FREE(rset->srs_rules,
283 rset->srs_nslot * sizeof(*rset->srs_rules));
286 rset->srs_rules = rules;
287 rset->srs_nslot = nslot;
290 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
292 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
294 return (rule->sr_from != LUSTRE_SP_ANY ||
295 rule->sr_to != LUSTRE_SP_ANY);
297 static inline int rule_spec_net(struct sptlrpc_rule *rule)
299 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
301 static inline int rule_match_dir(struct sptlrpc_rule *r1,
302 struct sptlrpc_rule *r2)
304 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
306 static inline int rule_match_net(struct sptlrpc_rule *r1,
307 struct sptlrpc_rule *r2)
309 return (r1->sr_netid == r2->sr_netid);
313 * merge @rule into @rset.
314 * the @rset slots might be expanded.
316 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
317 struct sptlrpc_rule *rule)
319 struct sptlrpc_rule *p = rset->srs_rules;
320 int spec_dir, spec_net;
321 int rc, n, match = 0;
325 spec_net = rule_spec_net(rule);
326 spec_dir = rule_spec_dir(rule);
328 for (n = 0; n < rset->srs_nrule; n++) {
329 p = &rset->srs_rules[n];
331 /* test network match, if failed:
332 * - spec rule: skip rules which is also spec rule match, until
333 * we hit a wild rule, which means no more chance
334 * - wild rule: skip until reach the one which is also wild
337 if (!rule_match_net(p, rule)) {
339 if (rule_spec_net(p))
348 /* test dir match, same logic as net matching */
349 if (!rule_match_dir(p, rule)) {
351 if (rule_spec_dir(p))
366 LASSERT(n >= 0 && n < rset->srs_nrule);
368 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
369 /* remove this rule */
370 if (n < rset->srs_nrule - 1)
371 memmove(&rset->srs_rules[n],
372 &rset->srs_rules[n + 1],
373 (rset->srs_nrule - n - 1) *
377 /* override the rule */
378 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
381 LASSERT(n >= 0 && n <= rset->srs_nrule);
383 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
384 rc = sptlrpc_rule_set_expand(rset);
388 if (n < rset->srs_nrule)
389 memmove(&rset->srs_rules[n + 1],
391 (rset->srs_nrule - n) * sizeof(*rule));
392 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
395 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
401 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
404 * given from/to/nid, determine a matching flavor in ruleset.
405 * return 1 if a match found, otherwise return 0.
407 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
408 enum lustre_sec_part from,
409 enum lustre_sec_part to,
411 struct sptlrpc_flavor *sf)
413 struct sptlrpc_rule *r;
416 for (n = 0; n < rset->srs_nrule; n++) {
417 r = &rset->srs_rules[n];
419 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
420 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
421 LNET_NIDNET(nid) != r->sr_netid)
424 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
428 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
438 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
440 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
442 struct sptlrpc_rule *r;
445 for (n = 0; n < rset->srs_nrule; n++) {
446 r = &rset->srs_rules[n];
447 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
448 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
451 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
453 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
454 struct sptlrpc_rule_set *tgt,
455 enum lustre_sec_part from,
456 enum lustre_sec_part to,
457 struct sptlrpc_rule_set *rset)
459 struct sptlrpc_rule_set *src[2] = { gen, tgt };
460 struct sptlrpc_rule *rule;
465 /* merge general rules firstly, then target-specific rules */
466 for (i = 0; i < 2; i++) {
470 for (n = 0; n < src[i]->srs_nrule; n++) {
471 rule = &src[i]->srs_rules[n];
473 if (from != LUSTRE_SP_ANY &&
474 rule->sr_from != LUSTRE_SP_ANY &&
475 rule->sr_from != from)
477 if (to != LUSTRE_SP_ANY &&
478 rule->sr_to != LUSTRE_SP_ANY &&
482 rc = sptlrpc_rule_set_merge(rset, rule);
484 CERROR("can't merge: %d\n", rc);
493 /**********************************
494 * sptlrpc configuration support *
495 **********************************/
497 struct sptlrpc_conf_tgt {
498 struct list_head sct_list;
499 char sct_name[MAX_OBD_NAME];
500 struct sptlrpc_rule_set sct_rset;
503 struct sptlrpc_conf {
504 struct list_head sc_list;
505 char sc_fsname[MTI_NAME_MAXLEN];
506 unsigned int sc_modified; /* modified during updating */
507 unsigned int sc_updated:1, /* updated copy from MGS */
508 sc_local:1; /* local copy from target */
509 struct sptlrpc_rule_set sc_rset; /* fs general rules */
510 struct list_head sc_tgts; /* target-specific rules */
513 static struct mutex sptlrpc_conf_lock;
514 static LIST_HEAD(sptlrpc_confs);
516 static inline int is_hex(char c)
518 return ((c >= '0' && c <= '9') ||
519 (c >= 'a' && c <= 'f'));
522 static void target2fsname(const char *tgt, char *fsname, int buflen)
527 ptr = strrchr(tgt, '-');
529 if ((strncmp(ptr, "-MDT", 4) != 0 &&
530 strncmp(ptr, "-OST", 4) != 0) ||
531 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
532 !is_hex(ptr[6]) || !is_hex(ptr[7]))
536 /* if we didn't find the pattern, treat the whole string as fsname */
542 len = min(len, buflen - 1);
543 memcpy(fsname, tgt, len);
547 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
549 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
551 sptlrpc_rule_set_free(&conf->sc_rset);
553 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
554 &conf->sc_tgts, sct_list) {
555 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
556 list_del(&conf_tgt->sct_list);
557 OBD_FREE_PTR(conf_tgt);
559 LASSERT(list_empty(&conf->sc_tgts));
561 conf->sc_updated = 0;
565 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
567 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
569 sptlrpc_conf_free_rsets(conf);
570 list_del(&conf->sc_list);
575 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
579 struct sptlrpc_conf_tgt *conf_tgt;
581 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
582 if (strcmp(conf_tgt->sct_name, name) == 0)
589 OBD_ALLOC_PTR(conf_tgt);
591 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
592 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
593 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
600 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
603 struct sptlrpc_conf *conf;
605 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
606 if (strcmp(conf->sc_fsname, fsname) == 0)
617 strcpy(conf->sc_fsname, fsname);
618 sptlrpc_rule_set_init(&conf->sc_rset);
619 INIT_LIST_HEAD(&conf->sc_tgts);
620 list_add(&conf->sc_list, &sptlrpc_confs);
622 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
627 * caller must hold conf_lock already.
629 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
631 struct sptlrpc_rule *rule)
633 struct sptlrpc_conf_tgt *conf_tgt;
634 struct sptlrpc_rule_set *rule_set;
636 /* fsname == target means general rules for the whole fs */
637 if (strcmp(conf->sc_fsname, target) == 0) {
638 rule_set = &conf->sc_rset;
640 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
642 rule_set = &conf_tgt->sct_rset;
644 CERROR("out of memory, can't merge rule!\n");
649 return sptlrpc_rule_set_merge(rule_set, rule);
653 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
654 * find one through the target name in the record inside conf_lock;
655 * otherwise means caller already hold conf_lock.
657 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
658 struct sptlrpc_conf *conf)
660 char *target, *param;
661 char fsname[MTI_NAME_MAXLEN];
662 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);
748 if (conf && conf->sc_local) {
749 LASSERT(conf->sc_updated == 0);
750 sptlrpc_conf_free_rsets(conf);
752 conf->sc_modified = 0;
754 mutex_unlock(&sptlrpc_conf_lock);
756 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
759 * mark a config log has been updated
761 void sptlrpc_conf_log_update_end(const char *logname)
763 struct sptlrpc_conf *conf;
766 if (logname2fsname(logname, fsname, sizeof(fsname)))
769 mutex_lock(&sptlrpc_conf_lock);
771 conf = sptlrpc_conf_get(fsname, 0);
774 * if original state is not updated, make sure the
775 * modified counter > 0 to enforce updating local copy.
777 if (conf->sc_updated == 0)
780 conf->sc_updated = 1;
783 mutex_unlock(&sptlrpc_conf_lock);
785 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
787 void sptlrpc_conf_log_start(const char *logname)
791 if (logname2fsname(logname, fsname, sizeof(fsname)))
794 mutex_lock(&sptlrpc_conf_lock);
795 sptlrpc_conf_get(fsname, 1);
796 mutex_unlock(&sptlrpc_conf_lock);
798 EXPORT_SYMBOL(sptlrpc_conf_log_start);
800 void sptlrpc_conf_log_stop(const char *logname)
802 struct sptlrpc_conf *conf;
805 if (logname2fsname(logname, fsname, sizeof(fsname)))
808 mutex_lock(&sptlrpc_conf_lock);
809 conf = sptlrpc_conf_get(fsname, 0);
811 sptlrpc_conf_free(conf);
812 mutex_unlock(&sptlrpc_conf_lock);
814 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
816 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
817 enum lustre_sec_part from,
818 enum lustre_sec_part to,
819 unsigned int fl_udesc)
822 * null flavor doesn't need to set any flavor, and in fact
823 * we'd better not do that because everybody share a single sec.
825 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
828 if (from == LUSTRE_SP_MDT) {
829 /* MDT->MDT; MDT->OST */
830 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
831 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
833 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
834 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
836 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
837 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
841 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
842 enum lustre_sec_part to,
843 struct obd_uuid *target,
845 struct sptlrpc_flavor *sf)
847 struct sptlrpc_conf *conf;
848 struct sptlrpc_conf_tgt *conf_tgt;
849 char name[MTI_NAME_MAXLEN];
852 target2fsname(target->uuid, name, sizeof(name));
854 mutex_lock(&sptlrpc_conf_lock);
856 conf = sptlrpc_conf_get(name, 0);
860 /* convert uuid name (supposed end with _UUID) to target name */
861 len = strlen(target->uuid);
863 memcpy(name, target->uuid, len - 5);
864 name[len - 5] = '\0';
866 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
868 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
874 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
876 mutex_unlock(&sptlrpc_conf_lock);
879 get_default_flavor(sf);
881 flavor_set_flags(sf, from, to, 1);
885 * called by target devices, determine the expected flavor from
886 * certain peer (from, nid).
888 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
889 enum lustre_sec_part from,
891 struct sptlrpc_flavor *sf)
893 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
894 get_default_flavor(sf);
896 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
898 #define SEC_ADAPT_DELAY (10)
901 * called by client devices, notify the sptlrpc config has changed and
902 * do import_sec_adapt later.
904 void sptlrpc_conf_client_adapt(struct obd_device *obd)
906 struct obd_import *imp;
908 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
909 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
910 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
912 /* serialize with connect/disconnect import */
913 down_read(&obd->u.cli.cl_sem);
915 imp = obd->u.cli.cl_import;
917 spin_lock(&imp->imp_lock);
919 imp->imp_sec_expire = cfs_time_current_sec() +
921 spin_unlock(&imp->imp_lock);
924 up_read(&obd->u.cli.cl_sem);
926 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
929 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
935 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
938 net = libcfs_net2str(r->sr_netid);
940 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
943 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
944 sptlrpc_part2name(r->sr_from),
945 sptlrpc_part2name(r->sr_to));
947 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
949 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
950 buf[buflen - 1] = '\0';
953 static int sptlrpc_record_rule_set(struct llog_handle *llh,
955 struct sptlrpc_rule_set *rset)
957 struct lustre_cfg_bufs bufs;
958 struct lustre_cfg *lcfg;
959 struct llog_rec_hdr rec;
964 for (i = 0; i < rset->srs_nrule; i++) {
965 rule2string(&rset->srs_rules[i], param, sizeof(param));
967 lustre_cfg_bufs_reset(&bufs, NULL);
968 lustre_cfg_bufs_set_string(&bufs, 1, target);
969 lustre_cfg_bufs_set_string(&bufs, 2, param);
970 lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
973 buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
975 rec.lrh_len = llog_data_len(buflen);
976 rec.lrh_type = OBD_CFG_REC;
977 rc = llog_write(NULL, llh, &rec, NULL, 0, (void *)lcfg, -1);
979 CERROR("failed to write a rec: rc = %d\n", rc);
980 lustre_cfg_free(lcfg);
985 static int sptlrpc_record_rules(struct llog_handle *llh,
986 struct sptlrpc_conf *conf)
988 struct sptlrpc_conf_tgt *conf_tgt;
990 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
992 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
993 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
994 &conf_tgt->sct_rset);
999 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1000 #define LOG_SPTLRPC "sptlrpc"
1003 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1004 struct sptlrpc_conf *conf)
1006 struct llog_handle *llh = NULL;
1007 struct llog_ctxt *ctxt;
1008 struct lvfs_run_ctxt saved;
1009 struct dentry *dentry;
1012 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1016 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1018 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
1019 strlen(MOUNT_CONFIGS_DIR));
1020 if (IS_ERR(dentry)) {
1021 rc = PTR_ERR(dentry);
1022 CERROR("cannot lookup %s directory: rc = %d\n",
1023 MOUNT_CONFIGS_DIR, rc);
1027 /* erase the old tmp log */
1028 rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1029 if (rc < 0 && rc != -ENOENT) {
1030 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1035 /* write temporary log */
1036 rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1039 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1041 GOTO(out_close, rc);
1043 rc = sptlrpc_record_rules(llh, conf);
1046 llog_close(NULL, llh);
1048 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1049 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1053 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1054 llog_ctxt_put(ctxt);
1055 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1060 static int local_read_handler(const struct lu_env *env,
1061 struct llog_handle *llh,
1062 struct llog_rec_hdr *rec, void *data)
1064 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1065 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1068 if (rec->lrh_type != OBD_CFG_REC) {
1069 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1073 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1074 sizeof(struct llog_rec_tail);
1076 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1078 CERROR("Insane cfg\n");
1082 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1083 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1087 RETURN(__sptlrpc_process_config(lcfg, conf));
1091 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1092 struct sptlrpc_conf *conf)
1094 struct llog_handle *llh = NULL;
1095 struct llog_ctxt *ctxt;
1096 struct lvfs_run_ctxt saved;
1099 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1101 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1103 CERROR("missing llog context\n");
1107 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1109 rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1116 rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1118 GOTO(out_close, rc);
1120 if (llog_get_size(llh) <= 1) {
1121 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1122 GOTO(out_close, rc = 0);
1125 rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1130 sptlrpc_conf_free_rsets(conf);
1134 llog_close(NULL, llh);
1136 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1137 llog_ctxt_put(ctxt);
1138 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1145 * called by target devices, extract sptlrpc rules which applies to
1146 * this target, to be used for future rpc flavor checking.
1148 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1149 struct sptlrpc_rule_set *rset,
1152 struct sptlrpc_conf *conf;
1153 struct sptlrpc_conf_tgt *conf_tgt;
1154 enum lustre_sec_part sp_dst;
1155 char fsname[MTI_NAME_MAXLEN];
1158 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1159 sp_dst = LUSTRE_SP_MDT;
1160 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1161 sp_dst = LUSTRE_SP_OST;
1163 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1166 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1168 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1170 mutex_lock(&sptlrpc_conf_lock);
1172 conf = sptlrpc_conf_get(fsname, 0);
1174 CERROR("missing sptlrpc config log\n");
1178 if (conf->sc_updated == 0) {
1180 * always read from local copy. here another option is
1181 * if we already have a local copy (read from another
1182 * target device hosted on the same node) we simply use that.
1185 sptlrpc_conf_free_rsets(conf);
1187 sptlrpc_target_local_read_conf(obd, conf);
1189 LASSERT(conf->sc_local == 0);
1191 /* write a local copy */
1192 if (initial || conf->sc_modified)
1193 sptlrpc_target_local_copy_conf(obd, conf);
1195 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1198 /* extract rule set for this target */
1199 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1201 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1202 conf_tgt ? &conf_tgt->sct_rset: NULL,
1203 LUSTRE_SP_ANY, sp_dst, rset);
1205 mutex_unlock(&sptlrpc_conf_lock);
1208 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1210 int sptlrpc_conf_init(void)
1212 mutex_init(&sptlrpc_conf_lock);
1216 void sptlrpc_conf_fini(void)
1218 struct sptlrpc_conf *conf, *conf_next;
1220 mutex_lock(&sptlrpc_conf_lock);
1221 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
1222 sptlrpc_conf_free(conf);
1224 LASSERT(list_empty(&sptlrpc_confs));
1225 mutex_unlock(&sptlrpc_conf_lock);