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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
42 #define DEBUG_SUBSYSTEM S_SEC
44 #include <libcfs/libcfs.h>
46 #include <liblustre.h>
47 #include <libcfs/list.h>
49 #include <linux/crypto.h>
50 #include <linux/key.h>
54 #include <obd_class.h>
55 #include <obd_support.h>
56 #include <lustre_net.h>
57 #include <lustre_import.h>
58 #include <lustre_log.h>
59 #include <lustre_disk.h>
60 #include <lustre_dlm.h>
61 #include <lustre_param.h>
62 #include <lustre_sec.h>
64 #include "ptlrpc_internal.h"
66 const char *sptlrpc_part2name(enum lustre_sec_part part)
85 EXPORT_SYMBOL(sptlrpc_part2name);
87 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
89 const char *type = obd->obd_type->typ_name;
91 if (!strcmp(type, LUSTRE_MDT_NAME))
93 if (!strcmp(type, LUSTRE_OST_NAME))
95 if (!strcmp(type, LUSTRE_MGS_NAME))
98 CERROR("unknown target %p(%s)\n", obd, type);
101 EXPORT_SYMBOL(sptlrpc_target_sec_part);
103 /****************************************
104 * user supplied flavor string parsing *
105 ****************************************/
108 * format: <base_flavor>[-<bulk_type:alg_spec>]
110 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
115 memset(flvr, 0, sizeof(*flvr));
117 if (str == NULL || str[0] == '\0') {
118 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
122 strncpy(buf, str, sizeof(buf));
123 buf[sizeof(buf) - 1] = '\0';
125 bulk = strchr(buf, '-');
129 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
130 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
134 * currently only base flavor "plain" can have bulk specification.
136 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
137 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
140 * format: plain-hash:<hash_alg>
142 alg = strchr(bulk, ':');
147 if (strcmp(bulk, "hash"))
150 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
151 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
155 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
156 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
158 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
168 CERROR("invalid flavor string: %s\n", str);
171 EXPORT_SYMBOL(sptlrpc_parse_flavor);
173 /****************************************
175 ****************************************/
177 static void get_default_flavor(struct sptlrpc_flavor *sf)
179 memset(sf, 0, sizeof(*sf));
181 sf->sf_rpc = SPTLRPC_FLVR_NULL;
185 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
187 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
188 rule->sr_from = LUSTRE_SP_ANY;
189 rule->sr_to = LUSTRE_SP_ANY;
190 rule->sr_padding = 0;
192 get_default_flavor(&rule->sr_flvr);
196 * format: network[.direction]=flavor
198 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
203 sptlrpc_rule_init(rule);
205 flavor = strchr(param, '=');
206 if (flavor == NULL) {
207 CERROR("invalid param, no '='\n");
212 dir = strchr(param, '.');
217 if (strcmp(param, "default")) {
218 rule->sr_netid = libcfs_str2net(param);
219 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
220 CERROR("invalid network name: %s\n", param);
227 if (!strcmp(dir, "mdt2ost")) {
228 rule->sr_from = LUSTRE_SP_MDT;
229 rule->sr_to = LUSTRE_SP_OST;
230 } else if (!strcmp(dir, "mdt2mdt")) {
231 rule->sr_from = LUSTRE_SP_MDT;
232 rule->sr_to = LUSTRE_SP_MDT;
233 } else if (!strcmp(dir, "cli2ost")) {
234 rule->sr_from = LUSTRE_SP_CLI;
235 rule->sr_to = LUSTRE_SP_OST;
236 } else if (!strcmp(dir, "cli2mdt")) {
237 rule->sr_from = LUSTRE_SP_CLI;
238 rule->sr_to = LUSTRE_SP_MDT;
240 CERROR("invalid rule dir segment: %s\n", dir);
246 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
252 EXPORT_SYMBOL(sptlrpc_parse_rule);
254 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
256 LASSERT(rset->srs_nslot ||
257 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
259 if (rset->srs_nslot) {
260 OBD_FREE(rset->srs_rules,
261 rset->srs_nslot * sizeof(*rset->srs_rules));
262 sptlrpc_rule_set_init(rset);
265 EXPORT_SYMBOL(sptlrpc_rule_set_free);
268 * return 0 if the rule set could accomodate one more rule.
270 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
272 struct sptlrpc_rule *rules;
277 if (rset->srs_nrule < rset->srs_nslot)
280 nslot = rset->srs_nslot + 8;
282 /* better use realloc() if available */
283 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
287 if (rset->srs_nrule) {
288 LASSERT(rset->srs_nslot && rset->srs_rules);
289 memcpy(rules, rset->srs_rules,
290 rset->srs_nrule * sizeof(*rset->srs_rules));
292 OBD_FREE(rset->srs_rules,
293 rset->srs_nslot * sizeof(*rset->srs_rules));
296 rset->srs_rules = rules;
297 rset->srs_nslot = nslot;
300 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
302 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
304 return (rule->sr_from != LUSTRE_SP_ANY ||
305 rule->sr_to != LUSTRE_SP_ANY);
307 static inline int rule_spec_net(struct sptlrpc_rule *rule)
309 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
311 static inline int rule_match_dir(struct sptlrpc_rule *r1,
312 struct sptlrpc_rule *r2)
314 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
316 static inline int rule_match_net(struct sptlrpc_rule *r1,
317 struct sptlrpc_rule *r2)
319 return (r1->sr_netid == r2->sr_netid);
323 * merge @rule into @rset.
324 * the @rset slots might be expanded.
326 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
327 struct sptlrpc_rule *rule)
329 struct sptlrpc_rule *p = rset->srs_rules;
330 int spec_dir, spec_net;
331 int rc, n, match = 0;
335 spec_net = rule_spec_net(rule);
336 spec_dir = rule_spec_dir(rule);
338 for (n = 0; n < rset->srs_nrule; n++) {
339 p = &rset->srs_rules[n];
341 /* test network match, if failed:
342 * - spec rule: skip rules which is also spec rule match, until
343 * we hit a wild rule, which means no more chance
344 * - wild rule: skip until reach the one which is also wild
347 if (!rule_match_net(p, rule)) {
349 if (rule_spec_net(p))
358 /* test dir match, same logic as net matching */
359 if (!rule_match_dir(p, rule)) {
361 if (rule_spec_dir(p))
376 LASSERT(n >= 0 && n < rset->srs_nrule);
378 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
379 /* remove this rule */
380 if (n < rset->srs_nrule - 1)
381 memmove(&rset->srs_rules[n],
382 &rset->srs_rules[n + 1],
383 (rset->srs_nrule - n - 1) *
387 /* override the rule */
388 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
391 LASSERT(n >= 0 && n <= rset->srs_nrule);
393 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
394 rc = sptlrpc_rule_set_expand(rset);
398 if (n < rset->srs_nrule)
399 memmove(&rset->srs_rules[n + 1],
401 (rset->srs_nrule - n) * sizeof(*rule));
402 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
405 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
411 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
414 * given from/to/nid, determine a matching flavor in ruleset.
415 * return 1 if a match found, otherwise return 0.
417 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
418 enum lustre_sec_part from,
419 enum lustre_sec_part to,
421 struct sptlrpc_flavor *sf)
423 struct sptlrpc_rule *r;
426 for (n = 0; n < rset->srs_nrule; n++) {
427 r = &rset->srs_rules[n];
429 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
430 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
431 LNET_NIDNET(nid) != r->sr_netid)
434 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
438 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
448 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
450 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
452 struct sptlrpc_rule *r;
455 for (n = 0; n < rset->srs_nrule; n++) {
456 r = &rset->srs_rules[n];
457 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
458 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
461 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
463 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
464 struct sptlrpc_rule_set *tgt,
465 enum lustre_sec_part from,
466 enum lustre_sec_part to,
467 struct sptlrpc_rule_set *rset)
469 struct sptlrpc_rule_set *src[2] = { gen, tgt };
470 struct sptlrpc_rule *rule;
475 /* merge general rules firstly, then target-specific rules */
476 for (i = 0; i < 2; i++) {
480 for (n = 0; n < src[i]->srs_nrule; n++) {
481 rule = &src[i]->srs_rules[n];
483 if (from != LUSTRE_SP_ANY &&
484 rule->sr_from != LUSTRE_SP_ANY &&
485 rule->sr_from != from)
487 if (to != LUSTRE_SP_ANY &&
488 rule->sr_to != LUSTRE_SP_ANY &&
492 rc = sptlrpc_rule_set_merge(rset, rule);
494 CERROR("can't merge: %d\n", rc);
503 /**********************************
504 * sptlrpc configuration support *
505 **********************************/
507 struct sptlrpc_conf_tgt {
509 char sct_name[MAX_OBD_NAME];
510 struct sptlrpc_rule_set sct_rset;
513 struct sptlrpc_conf {
515 char sc_fsname[MTI_NAME_MAXLEN];
516 unsigned int sc_modified; /* modified during updating */
517 unsigned int sc_updated:1, /* updated copy from MGS */
518 sc_local:1; /* local copy from target */
519 struct sptlrpc_rule_set sc_rset; /* fs general rules */
520 cfs_list_t sc_tgts; /* target-specific rules */
523 static cfs_mutex_t sptlrpc_conf_lock;
524 static CFS_LIST_HEAD(sptlrpc_confs);
526 static inline int is_hex(char c)
528 return ((c >= '0' && c <= '9') ||
529 (c >= 'a' && c <= 'f'));
532 static void target2fsname(const char *tgt, char *fsname, int buflen)
537 ptr = strrchr(tgt, '-');
539 if ((strncmp(ptr, "-MDT", 4) != 0 &&
540 strncmp(ptr, "-OST", 4) != 0) ||
541 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
542 !is_hex(ptr[6]) || !is_hex(ptr[7]))
546 /* if we didn't find the pattern, treat the whole string as fsname */
552 len = min(len, buflen - 1);
553 memcpy(fsname, tgt, len);
557 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
559 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
561 sptlrpc_rule_set_free(&conf->sc_rset);
563 cfs_list_for_each_entry_safe(conf_tgt, conf_tgt_next,
564 &conf->sc_tgts, sct_list) {
565 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
566 cfs_list_del(&conf_tgt->sct_list);
567 OBD_FREE_PTR(conf_tgt);
569 LASSERT(cfs_list_empty(&conf->sc_tgts));
571 conf->sc_updated = 0;
575 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
577 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
579 sptlrpc_conf_free_rsets(conf);
580 cfs_list_del(&conf->sc_list);
585 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
589 struct sptlrpc_conf_tgt *conf_tgt;
591 cfs_list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
592 if (strcmp(conf_tgt->sct_name, name) == 0)
599 OBD_ALLOC_PTR(conf_tgt);
601 strncpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
602 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
603 cfs_list_add(&conf_tgt->sct_list, &conf->sc_tgts);
610 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
613 struct sptlrpc_conf *conf;
615 cfs_list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
616 if (strcmp(conf->sc_fsname, fsname) == 0)
627 strcpy(conf->sc_fsname, fsname);
628 sptlrpc_rule_set_init(&conf->sc_rset);
629 CFS_INIT_LIST_HEAD(&conf->sc_tgts);
630 cfs_list_add(&conf->sc_list, &sptlrpc_confs);
632 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
637 * caller must hold conf_lock already.
639 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
641 struct sptlrpc_rule *rule)
643 struct sptlrpc_conf_tgt *conf_tgt;
644 struct sptlrpc_rule_set *rule_set;
646 /* fsname == target means general rules for the whole fs */
647 if (strcmp(conf->sc_fsname, target) == 0) {
648 rule_set = &conf->sc_rset;
650 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
652 rule_set = &conf_tgt->sct_rset;
654 CERROR("out of memory, can't merge rule!\n");
659 return sptlrpc_rule_set_merge(rule_set, rule);
663 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
664 * find one through the target name in the record inside conf_lock;
665 * otherwise means caller already hold conf_lock.
667 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
668 struct sptlrpc_conf *conf)
670 char *target, *param;
671 char fsname[MTI_NAME_MAXLEN];
672 struct sptlrpc_rule rule;
676 target = lustre_cfg_string(lcfg, 1);
677 if (target == NULL) {
678 CERROR("missing target name\n");
682 param = lustre_cfg_string(lcfg, 2);
684 CERROR("missing parameter\n");
688 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
690 /* parse rule to make sure the format is correct */
691 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
692 CERROR("Invalid sptlrpc parameter: %s\n", param);
695 param += sizeof(PARAM_SRPC_FLVR) - 1;
697 rc = sptlrpc_parse_rule(param, &rule);
702 target2fsname(target, fsname, sizeof(fsname));
704 cfs_mutex_lock(&sptlrpc_conf_lock);
705 conf = sptlrpc_conf_get(fsname, 0);
707 CERROR("can't find conf\n");
710 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
712 cfs_mutex_unlock(&sptlrpc_conf_lock);
714 LASSERT(cfs_mutex_is_locked(&sptlrpc_conf_lock));
715 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
724 int sptlrpc_process_config(struct lustre_cfg *lcfg)
726 return __sptlrpc_process_config(lcfg, NULL);
728 EXPORT_SYMBOL(sptlrpc_process_config);
730 static int logname2fsname(const char *logname, char *buf, int buflen)
735 ptr = strrchr(logname, '-');
736 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
737 CERROR("%s is not a sptlrpc config log\n", logname);
741 len = min((int) (ptr - logname), buflen - 1);
743 memcpy(buf, logname, len);
748 void sptlrpc_conf_log_update_begin(const char *logname)
750 struct sptlrpc_conf *conf;
753 if (logname2fsname(logname, fsname, sizeof(fsname)))
756 cfs_mutex_lock(&sptlrpc_conf_lock);
758 conf = sptlrpc_conf_get(fsname, 0);
759 if (conf && conf->sc_local) {
760 LASSERT(conf->sc_updated == 0);
761 sptlrpc_conf_free_rsets(conf);
763 conf->sc_modified = 0;
765 cfs_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 cfs_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 cfs_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 cfs_mutex_lock(&sptlrpc_conf_lock);
806 sptlrpc_conf_get(fsname, 1);
807 cfs_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 cfs_mutex_lock(&sptlrpc_conf_lock);
820 conf = sptlrpc_conf_get(fsname, 0);
822 sptlrpc_conf_free(conf);
823 cfs_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 cfs_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 cfs_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 cfs_down_read(&obd->u.cli.cl_sem);
927 imp = obd->u.cli.cl_import;
929 cfs_spin_lock(&imp->imp_lock);
931 imp->imp_sec_expire = cfs_time_current_sec() +
933 cfs_spin_unlock(&imp->imp_lock);
936 cfs_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 lustre_cfg_bufs bufs;
972 struct lustre_cfg *lcfg;
973 struct llog_rec_hdr rec;
978 for (i = 0; i < rset->srs_nrule; i++) {
979 rule2string(&rset->srs_rules[i], param, sizeof(param));
981 lustre_cfg_bufs_reset(&bufs, NULL);
982 lustre_cfg_bufs_set_string(&bufs, 1, target);
983 lustre_cfg_bufs_set_string(&bufs, 2, param);
984 lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
987 buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
989 rec.lrh_len = llog_data_len(buflen);
990 rec.lrh_type = OBD_CFG_REC;
991 rc = llog_write_rec(llh, &rec, NULL, 0, (void *)lcfg, -1);
993 CERROR("failed to write a rec: rc = %d\n", rc);
994 lustre_cfg_free(lcfg);
999 static int sptlrpc_record_rules(struct llog_handle *llh,
1000 struct sptlrpc_conf *conf)
1002 struct sptlrpc_conf_tgt *conf_tgt;
1004 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
1006 cfs_list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
1007 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
1008 &conf_tgt->sct_rset);
1013 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1014 #define LOG_SPTLRPC "sptlrpc"
1017 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1018 struct sptlrpc_conf *conf)
1020 struct llog_handle *llh = NULL;
1021 struct llog_ctxt *ctxt;
1022 struct lvfs_run_ctxt saved;
1023 struct dentry *dentry;
1027 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1029 CERROR("missing llog context\n");
1033 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1035 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
1036 strlen(MOUNT_CONFIGS_DIR));
1037 if (IS_ERR(dentry)) {
1038 rc = PTR_ERR(dentry);
1039 CERROR("cannot lookup %s directory: rc = %d\n",
1040 MOUNT_CONFIGS_DIR, rc);
1044 /* erase the old tmp log */
1045 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1047 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1049 rc = llog_destroy(llh);
1050 llog_free_handle(llh);
1057 CERROR("target %s: cannot erase temporary sptlrpc log: "
1058 "rc = %d\n", obd->obd_name, rc);
1062 /* write temporary log */
1063 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1066 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1068 GOTO(out_close, rc);
1070 rc = sptlrpc_record_rules(llh, conf);
1076 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1077 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1083 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1084 llog_ctxt_put(ctxt);
1085 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1090 static int local_read_handler(struct llog_handle *llh,
1091 struct llog_rec_hdr *rec,
1094 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1095 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1099 if (rec->lrh_type != OBD_CFG_REC) {
1100 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1104 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1105 sizeof(struct llog_rec_tail);
1107 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1109 CERROR("Insane cfg\n");
1113 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1114 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1118 RETURN(__sptlrpc_process_config(lcfg, conf));
1122 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1123 struct sptlrpc_conf *conf)
1125 struct llog_handle *llh = NULL;
1126 struct llog_ctxt *ctxt;
1127 struct lvfs_run_ctxt saved;
1131 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1133 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1135 CERROR("missing llog context\n");
1139 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1141 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC);
1145 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1147 GOTO(out_close, rc);
1149 if (llog_get_size(llh) <= 1) {
1150 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1151 GOTO(out_close, rc = 0);
1154 rc = llog_process(llh, local_read_handler, (void *) conf, NULL);
1159 sptlrpc_conf_free_rsets(conf);
1165 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1166 llog_ctxt_put(ctxt);
1167 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1172 #endif /* __KRENEL__ */
1175 * called by target devices, extract sptlrpc rules which applies to
1176 * this target, to be used for future rpc flavor checking.
1178 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1179 struct sptlrpc_rule_set *rset,
1182 struct sptlrpc_conf *conf;
1183 struct sptlrpc_conf_tgt *conf_tgt;
1184 enum lustre_sec_part sp_dst;
1185 char fsname[MTI_NAME_MAXLEN];
1189 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1190 sp_dst = LUSTRE_SP_MDT;
1191 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1192 sp_dst = LUSTRE_SP_OST;
1194 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1197 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1199 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1201 cfs_mutex_lock(&sptlrpc_conf_lock);
1203 conf = sptlrpc_conf_get(fsname, 0);
1205 CERROR("missing sptlrpc config log\n");
1210 if (conf->sc_updated == 0) {
1212 * always read from local copy. here another option is
1213 * if we already have a local copy (read from another
1214 * target device hosted on the same node) we simply use that.
1217 sptlrpc_conf_free_rsets(conf);
1219 sptlrpc_target_local_read_conf(obd, conf);
1221 LASSERT(conf->sc_local == 0);
1223 /* write a local copy */
1224 if (initial || conf->sc_modified)
1225 sptlrpc_target_local_copy_conf(obd, conf);
1227 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1231 /* extract rule set for this target */
1232 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1234 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1235 conf_tgt ? &conf_tgt->sct_rset: NULL,
1236 LUSTRE_SP_ANY, sp_dst, rset);
1238 cfs_mutex_unlock(&sptlrpc_conf_lock);
1241 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1243 int sptlrpc_conf_init(void)
1245 cfs_mutex_init(&sptlrpc_conf_lock);
1249 void sptlrpc_conf_fini(void)
1251 struct sptlrpc_conf *conf, *conf_next;
1253 cfs_mutex_lock(&sptlrpc_conf_lock);
1254 cfs_list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
1255 sptlrpc_conf_free(conf);
1257 LASSERT(cfs_list_empty(&sptlrpc_confs));
1258 cfs_mutex_unlock(&sptlrpc_conf_lock);