1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 #define DEBUG_SUBSYSTEM S_SEC
42 #include <libcfs/libcfs.h>
44 #include <liblustre.h>
45 #include <libcfs/list.h>
47 #include <linux/crypto.h>
48 #include <linux/key.h>
52 #include <obd_class.h>
53 #include <obd_support.h>
54 #include <lustre_net.h>
55 #include <lustre_import.h>
56 #include <lustre_log.h>
57 #include <lustre_disk.h>
58 #include <lustre_dlm.h>
59 #include <lustre_param.h>
60 #include <lustre_sec.h>
62 #include "ptlrpc_internal.h"
64 const char *sptlrpc_part2name(enum lustre_sec_part part)
83 EXPORT_SYMBOL(sptlrpc_part2name);
85 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
87 const char *type = obd->obd_type->typ_name;
89 if (!strcmp(type, LUSTRE_MDT_NAME))
91 if (!strcmp(type, LUSTRE_OST_NAME))
93 if (!strcmp(type, LUSTRE_MGS_NAME))
96 CERROR("unknown target %p(%s)\n", obd, type);
99 EXPORT_SYMBOL(sptlrpc_target_sec_part);
101 /****************************************
102 * user supplied flavor string parsing *
103 ****************************************/
106 * format: <base_flavor>[-<bulk_type:alg_spec>]
108 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
113 memset(flvr, 0, sizeof(*flvr));
115 if (str == NULL || str[0] == '\0') {
116 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
120 strncpy(buf, str, sizeof(buf));
121 buf[sizeof(buf) - 1] = '\0';
123 bulk = strchr(buf, '-');
127 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
128 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
132 * currently only base flavor "plain" can have bulk specification.
134 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
135 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
138 * format: plain-hash:<hash_alg>
140 alg = strchr(bulk, ':');
145 if (strcmp(bulk, "hash"))
148 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
149 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
153 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
154 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
156 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
166 CERROR("invalid flavor string: %s\n", str);
169 EXPORT_SYMBOL(sptlrpc_parse_flavor);
171 /****************************************
173 ****************************************/
175 static void get_default_flavor(struct sptlrpc_flavor *sf)
177 memset(sf, 0, sizeof(*sf));
179 sf->sf_rpc = SPTLRPC_FLVR_NULL;
183 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
185 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
186 rule->sr_from = LUSTRE_SP_ANY;
187 rule->sr_to = LUSTRE_SP_ANY;
188 rule->sr_padding = 0;
190 get_default_flavor(&rule->sr_flvr);
194 * format: network[.direction]=flavor
196 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
201 sptlrpc_rule_init(rule);
203 flavor = strchr(param, '=');
204 if (flavor == NULL) {
205 CERROR("invalid param, no '='\n");
210 dir = strchr(param, '.');
215 if (strcmp(param, "default")) {
216 rule->sr_netid = libcfs_str2net(param);
217 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
218 CERROR("invalid network name: %s\n", param);
225 if (!strcmp(dir, "mdt2ost")) {
226 rule->sr_from = LUSTRE_SP_MDT;
227 rule->sr_to = LUSTRE_SP_OST;
228 } else if (!strcmp(dir, "mdt2mdt")) {
229 rule->sr_from = LUSTRE_SP_MDT;
230 rule->sr_to = LUSTRE_SP_MDT;
231 } else if (!strcmp(dir, "cli2ost")) {
232 rule->sr_from = LUSTRE_SP_CLI;
233 rule->sr_to = LUSTRE_SP_OST;
234 } else if (!strcmp(dir, "cli2mdt")) {
235 rule->sr_from = LUSTRE_SP_CLI;
236 rule->sr_to = LUSTRE_SP_MDT;
238 CERROR("invalid rule dir segment: %s\n", dir);
244 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
250 EXPORT_SYMBOL(sptlrpc_parse_rule);
252 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
254 LASSERT(rset->srs_nslot ||
255 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
257 if (rset->srs_nslot) {
258 OBD_FREE(rset->srs_rules,
259 rset->srs_nslot * sizeof(*rset->srs_rules));
260 sptlrpc_rule_set_init(rset);
263 EXPORT_SYMBOL(sptlrpc_rule_set_free);
266 * return 0 if the rule set could accomodate one more rule.
268 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
270 struct sptlrpc_rule *rules;
275 if (rset->srs_nrule < rset->srs_nslot)
278 nslot = rset->srs_nslot + 8;
280 /* better use realloc() if available */
281 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
285 if (rset->srs_nrule) {
286 LASSERT(rset->srs_nslot && rset->srs_rules);
287 memcpy(rules, rset->srs_rules,
288 rset->srs_nrule * sizeof(*rset->srs_rules));
290 OBD_FREE(rset->srs_rules,
291 rset->srs_nslot * sizeof(*rset->srs_rules));
294 rset->srs_rules = rules;
295 rset->srs_nslot = nslot;
298 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
300 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
302 return (rule->sr_from != LUSTRE_SP_ANY ||
303 rule->sr_to != LUSTRE_SP_ANY);
305 static inline int rule_spec_net(struct sptlrpc_rule *rule)
307 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
309 static inline int rule_match_dir(struct sptlrpc_rule *r1,
310 struct sptlrpc_rule *r2)
312 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
314 static inline int rule_match_net(struct sptlrpc_rule *r1,
315 struct sptlrpc_rule *r2)
317 return (r1->sr_netid == r2->sr_netid);
321 * merge @rule into @rset.
322 * the @rset slots might be expanded.
324 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
325 struct sptlrpc_rule *rule)
327 struct sptlrpc_rule *p = rset->srs_rules;
328 int spec_dir, spec_net;
329 int rc, n, match = 0;
333 spec_net = rule_spec_net(rule);
334 spec_dir = rule_spec_dir(rule);
336 for (n = 0; n < rset->srs_nrule; n++) {
337 p = &rset->srs_rules[n];
339 /* test network match, if failed:
340 * - spec rule: skip rules which is also spec rule match, until
341 * we hit a wild rule, which means no more chance
342 * - wild rule: skip until reach the one which is also wild
345 if (!rule_match_net(p, rule)) {
347 if (rule_spec_net(p))
356 /* test dir match, same logic as net matching */
357 if (!rule_match_dir(p, rule)) {
359 if (rule_spec_dir(p))
374 LASSERT(n >= 0 && n < rset->srs_nrule);
376 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
377 /* remove this rule */
378 if (n < rset->srs_nrule - 1)
379 memmove(&rset->srs_rules[n],
380 &rset->srs_rules[n + 1],
381 (rset->srs_nrule - n - 1) *
385 /* override the rule */
386 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
389 LASSERT(n >= 0 && n <= rset->srs_nrule);
391 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
392 rc = sptlrpc_rule_set_expand(rset);
396 if (n < rset->srs_nrule)
397 memmove(&rset->srs_rules[n + 1],
399 (rset->srs_nrule - n) * sizeof(*rule));
400 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
403 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
409 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
412 * given from/to/nid, determine a matching flavor in ruleset.
413 * return 1 if a match found, otherwise return 0.
415 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
416 enum lustre_sec_part from,
417 enum lustre_sec_part to,
419 struct sptlrpc_flavor *sf)
421 struct sptlrpc_rule *r;
424 for (n = 0; n < rset->srs_nrule; n++) {
425 r = &rset->srs_rules[n];
427 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
428 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
429 LNET_NIDNET(nid) != r->sr_netid)
432 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
436 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
446 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
448 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
450 struct sptlrpc_rule *r;
453 for (n = 0; n < rset->srs_nrule; n++) {
454 r = &rset->srs_rules[n];
455 CWARN("<%02d> from %x to %x, net %x, rpc %x\n", n,
456 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
459 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
461 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
462 struct sptlrpc_rule_set *tgt,
463 enum lustre_sec_part from,
464 enum lustre_sec_part to,
465 struct sptlrpc_rule_set *rset)
467 struct sptlrpc_rule_set *src[2] = { gen, tgt };
468 struct sptlrpc_rule *rule;
473 /* merge general rules firstly, then target-specific rules */
474 for (i = 0; i < 2; i++) {
478 for (n = 0; n < src[i]->srs_nrule; n++) {
479 rule = &src[i]->srs_rules[n];
481 if (from != LUSTRE_SP_ANY &&
482 rule->sr_from != LUSTRE_SP_ANY &&
483 rule->sr_from != from)
485 if (to != LUSTRE_SP_ANY &&
486 rule->sr_to != LUSTRE_SP_ANY &&
490 rc = sptlrpc_rule_set_merge(rset, rule);
492 CERROR("can't merge: %d\n", rc);
501 /**********************************
502 * sptlrpc configuration support *
503 **********************************/
505 struct sptlrpc_conf_tgt {
506 struct list_head sct_list;
507 char sct_name[MAX_OBD_NAME];
508 struct sptlrpc_rule_set sct_rset;
511 struct sptlrpc_conf {
512 struct list_head sc_list;
513 char sc_fsname[MTI_NAME_MAXLEN];
514 unsigned int sc_modified; /* modified during updating */
515 unsigned int sc_updated:1, /* updated copy from MGS */
516 sc_local:1; /* local copy from target */
517 struct sptlrpc_rule_set sc_rset; /* fs general rules */
518 struct list_head sc_tgts; /* target-specific rules */
521 static struct mutex sptlrpc_conf_lock;
522 static CFS_LIST_HEAD(sptlrpc_confs);
524 static inline int is_hex(char c)
526 return ((c >= '0' && c <= '9') ||
527 (c >= 'a' && c <= 'f'));
530 static void target2fsname(const char *tgt, char *fsname, int buflen)
535 ptr = strrchr(tgt, '-');
537 if ((strncmp(ptr, "-MDT", 4) != 0 &&
538 strncmp(ptr, "-OST", 4) != 0) ||
539 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
540 !is_hex(ptr[6]) || !is_hex(ptr[7]))
544 /* if we didn't find the pattern, treat the whole string as fsname */
550 len = min(len, buflen - 1);
551 memcpy(fsname, tgt, len);
555 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
557 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
559 sptlrpc_rule_set_free(&conf->sc_rset);
561 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
562 &conf->sc_tgts, sct_list) {
563 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
564 list_del(&conf_tgt->sct_list);
565 OBD_FREE_PTR(conf_tgt);
567 LASSERT(list_empty(&conf->sc_tgts));
569 conf->sc_updated = 0;
573 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
575 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
577 sptlrpc_conf_free_rsets(conf);
578 list_del(&conf->sc_list);
583 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
587 struct sptlrpc_conf_tgt *conf_tgt;
589 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
590 if (strcmp(conf_tgt->sct_name, name) == 0)
597 OBD_ALLOC_PTR(conf_tgt);
599 strncpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
600 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
601 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
608 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
611 struct sptlrpc_conf *conf;
613 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
614 if (strcmp(conf->sc_fsname, fsname) == 0)
625 strcpy(conf->sc_fsname, fsname);
626 sptlrpc_rule_set_init(&conf->sc_rset);
627 CFS_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);
757 if (conf && conf->sc_local) {
758 LASSERT(conf->sc_updated == 0);
759 sptlrpc_conf_free_rsets(conf);
761 conf->sc_modified = 0;
763 mutex_unlock(&sptlrpc_conf_lock);
765 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
768 * mark a config log has been updated
770 void sptlrpc_conf_log_update_end(const char *logname)
772 struct sptlrpc_conf *conf;
775 if (logname2fsname(logname, fsname, sizeof(fsname)))
778 mutex_lock(&sptlrpc_conf_lock);
780 conf = sptlrpc_conf_get(fsname, 0);
783 * if original state is not updated, make sure the
784 * modified counter > 0 to enforce updating local copy.
786 if (conf->sc_updated == 0)
789 conf->sc_updated = 1;
792 mutex_unlock(&sptlrpc_conf_lock);
794 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
796 void sptlrpc_conf_log_start(const char *logname)
798 struct sptlrpc_conf *conf;
801 if (logname2fsname(logname, fsname, sizeof(fsname)))
804 mutex_lock(&sptlrpc_conf_lock);
805 conf = sptlrpc_conf_get(fsname, 1);
806 mutex_unlock(&sptlrpc_conf_lock);
808 EXPORT_SYMBOL(sptlrpc_conf_log_start);
810 void sptlrpc_conf_log_stop(const char *logname)
812 struct sptlrpc_conf *conf;
815 if (logname2fsname(logname, fsname, sizeof(fsname)))
818 mutex_lock(&sptlrpc_conf_lock);
819 conf = sptlrpc_conf_get(fsname, 0);
821 sptlrpc_conf_free(conf);
822 mutex_unlock(&sptlrpc_conf_lock);
824 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
826 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
827 enum lustre_sec_part from,
828 enum lustre_sec_part to,
829 unsigned int fl_udesc)
832 * null flavor doesn't need to set any flavor, and in fact
833 * we'd better not do that because everybody share a single sec.
835 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
838 if (from == LUSTRE_SP_MDT) {
839 /* MDT->MDT; MDT->OST */
840 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
841 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
843 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
844 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
846 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
847 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);
906 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
908 #define SEC_ADAPT_DELAY (10)
911 * called by client devices, notify the sptlrpc config has changed and
912 * do import_sec_adapt later.
914 void sptlrpc_conf_client_adapt(struct obd_device *obd)
916 struct obd_import *imp;
919 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
920 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
921 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
923 /* serialize with connect/disconnect import */
924 down_read(&obd->u.cli.cl_sem);
926 imp = obd->u.cli.cl_import;
928 spin_lock(&imp->imp_lock);
930 imp->imp_sec_expire = cfs_time_current_sec() +
932 spin_unlock(&imp->imp_lock);
935 up_read(&obd->u.cli.cl_sem);
938 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
942 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
948 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
951 net = libcfs_net2str(r->sr_netid);
953 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
956 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
957 sptlrpc_part2name(r->sr_from),
958 sptlrpc_part2name(r->sr_to));
960 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
962 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
963 buf[buflen - 1] = '\0';
966 static int sptlrpc_record_rule_set(struct llog_handle *llh,
968 struct sptlrpc_rule_set *rset)
970 struct lustre_cfg_bufs bufs;
971 struct lustre_cfg *lcfg;
972 struct llog_rec_hdr rec;
977 for (i = 0; i < rset->srs_nrule; i++) {
978 rule2string(&rset->srs_rules[i], param, sizeof(param));
980 lustre_cfg_bufs_reset(&bufs, NULL);
981 lustre_cfg_bufs_set_string(&bufs, 1, target);
982 lustre_cfg_bufs_set_string(&bufs, 2, param);
983 lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
986 buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
988 rec.lrh_len = llog_data_len(buflen);
989 rec.lrh_type = OBD_CFG_REC;
990 rc = llog_write_rec(llh, &rec, NULL, 0, (void *)lcfg, -1);
992 CERROR("failed to write a rec: rc = %d\n", rc);
993 lustre_cfg_free(lcfg);
998 static int sptlrpc_record_rules(struct llog_handle *llh,
999 struct sptlrpc_conf *conf)
1001 struct sptlrpc_conf_tgt *conf_tgt;
1003 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
1005 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
1006 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
1007 &conf_tgt->sct_rset);
1012 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1013 #define LOG_SPTLRPC "sptlrpc"
1016 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1017 struct sptlrpc_conf *conf)
1019 struct llog_handle *llh = NULL;
1020 struct llog_ctxt *ctxt;
1021 struct lvfs_run_ctxt saved;
1022 struct dentry *dentry;
1026 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1028 CERROR("missing llog context\n");
1032 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1034 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
1035 strlen(MOUNT_CONFIGS_DIR));
1036 if (IS_ERR(dentry)) {
1037 rc = PTR_ERR(dentry);
1038 CERROR("cannot lookup %s directory: rc = %d\n",
1039 MOUNT_CONFIGS_DIR, rc);
1043 /* erase the old tmp log */
1044 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1046 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1048 rc = llog_destroy(llh);
1049 llog_free_handle(llh);
1056 CERROR("target %s: cannot erase temporary sptlrpc log: "
1057 "rc = %d\n", obd->obd_name, rc);
1061 /* write temporary log */
1062 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1065 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1067 GOTO(out_close, rc);
1069 rc = sptlrpc_record_rules(llh, conf);
1075 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1076 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1082 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1083 llog_ctxt_put(ctxt);
1084 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1089 static int local_read_handler(struct llog_handle *llh,
1090 struct llog_rec_hdr *rec,
1093 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1094 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1098 if (rec->lrh_type != OBD_CFG_REC) {
1099 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1103 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1104 sizeof(struct llog_rec_tail);
1106 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1108 CERROR("Insane cfg\n");
1112 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1113 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1117 RETURN(__sptlrpc_process_config(lcfg, conf));
1121 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1122 struct sptlrpc_conf *conf)
1124 struct llog_handle *llh = NULL;
1125 struct llog_ctxt *ctxt;
1126 struct lvfs_run_ctxt saved;
1130 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1132 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1134 CERROR("missing llog context\n");
1138 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1140 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC);
1144 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1146 GOTO(out_close, rc);
1148 if (llog_get_size(llh) <= 1) {
1149 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1150 GOTO(out_close, rc = 0);
1153 rc = llog_process(llh, local_read_handler, (void *) conf, NULL);
1158 sptlrpc_conf_free_rsets(conf);
1164 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1165 llog_ctxt_put(ctxt);
1166 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1171 #endif /* __KRENEL__ */
1174 * called by target devices, extract sptlrpc rules which applies to
1175 * this target, to be used for future rpc flavor checking.
1177 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1178 struct sptlrpc_rule_set *rset,
1181 struct sptlrpc_conf *conf;
1182 struct sptlrpc_conf_tgt *conf_tgt;
1183 enum lustre_sec_part sp_dst;
1184 char fsname[MTI_NAME_MAXLEN];
1188 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1189 sp_dst = LUSTRE_SP_MDT;
1190 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1191 sp_dst = LUSTRE_SP_OST;
1193 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1196 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1198 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1200 mutex_lock(&sptlrpc_conf_lock);
1202 conf = sptlrpc_conf_get(fsname, 0);
1204 CERROR("missing sptlrpc config log\n");
1209 if (conf->sc_updated == 0) {
1211 * always read from local copy. here another option is
1212 * if we already have a local copy (read from another
1213 * target device hosted on the same node) we simply use that.
1216 sptlrpc_conf_free_rsets(conf);
1218 sptlrpc_target_local_read_conf(obd, conf);
1220 LASSERT(conf->sc_local == 0);
1222 /* write a local copy */
1223 if (initial || conf->sc_modified)
1224 sptlrpc_target_local_copy_conf(obd, conf);
1226 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1230 /* extract rule set for this target */
1231 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1233 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1234 conf_tgt ? &conf_tgt->sct_rset: NULL,
1235 LUSTRE_SP_ANY, sp_dst, rset);
1237 mutex_unlock(&sptlrpc_conf_lock);
1240 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1242 int sptlrpc_conf_init(void)
1244 mutex_init(&sptlrpc_conf_lock);
1248 void sptlrpc_conf_fini(void)
1250 struct sptlrpc_conf *conf, *conf_next;
1252 mutex_lock(&sptlrpc_conf_lock);
1253 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
1254 sptlrpc_conf_free(conf);
1256 LASSERT(list_empty(&sptlrpc_confs));
1257 mutex_unlock(&sptlrpc_conf_lock);