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 #define BULK_HASH_ALG_DEFAULT BULK_HASH_ALG_ADLER32
108 #define BULK_HASH_ALG_DEFAULT BULK_HASH_ALG_CRC32
117 static void get_default_flavor(struct sptlrpc_flavor *sf)
119 sf->sf_rpc = SPTLRPC_FLVR_NULL;
120 sf->sf_bulk_ciph = BULK_CIPH_ALG_NULL;
121 sf->sf_bulk_hash = BULK_HASH_ALG_NULL;
125 static void get_flavor_by_rpc(struct sptlrpc_flavor *flvr, __u16 rpc_flavor)
127 get_default_flavor(flvr);
129 flvr->sf_rpc = rpc_flavor;
131 switch (rpc_flavor) {
132 case SPTLRPC_FLVR_NULL:
134 case SPTLRPC_FLVR_PLAIN:
135 case SPTLRPC_FLVR_KRB5N:
136 case SPTLRPC_FLVR_KRB5A:
137 flvr->sf_bulk_hash = BULK_HASH_ALG_DEFAULT;
139 case SPTLRPC_FLVR_KRB5P:
140 flvr->sf_bulk_ciph = BULK_CIPH_ALG_AES128;
142 case SPTLRPC_FLVR_KRB5I:
143 flvr->sf_bulk_hash = BULK_HASH_ALG_SHA1;
150 static void get_flavor_by_bulk(struct sptlrpc_flavor *flvr,
151 __u16 rpc_flavor, bulk_type_t bulk_type)
155 flvr->sf_bulk_hash = BULK_HASH_ALG_NULL;
156 flvr->sf_bulk_ciph = BULK_CIPH_ALG_NULL;
159 switch (rpc_flavor) {
160 case SPTLRPC_FLVR_PLAIN:
161 case SPTLRPC_FLVR_KRB5N:
162 case SPTLRPC_FLVR_KRB5A:
163 flvr->sf_bulk_hash = BULK_HASH_ALG_DEFAULT;
165 case SPTLRPC_FLVR_KRB5I:
166 case SPTLRPC_FLVR_KRB5P:
167 flvr->sf_bulk_hash = BULK_HASH_ALG_SHA1;
172 flvr->sf_bulk_ciph = BULK_CIPH_ALG_NULL;
175 flvr->sf_bulk_hash = BULK_HASH_ALG_SHA1;
176 flvr->sf_bulk_ciph = BULK_CIPH_ALG_AES128;
183 static __u16 __flavors[] = {
192 #define __nflavors ARRAY_SIZE(__flavors)
195 * flavor string format: rpc[-bulk{n|i|p}[:cksum/enc]]
201 * krb5i-bulkp:sha512/arc4
203 static int parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
206 char *bulk, *alg, *enc;
208 bulk_type_t bulk_type;
212 if (str == NULL || str[0] == '\0') {
213 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
217 for (i = 0; i < __nflavors; i++) {
218 f = sptlrpc_rpcflavor2name(__flavors[i]);
219 if (strncmp(str, f, strlen(f)) == 0)
224 GOTO(invalid, -EINVAL);
226 /* prepare local buffer thus we can modify it as we want */
227 strncpy(buf, str, 64);
230 /* find bulk string */
231 bulk = strchr(buf, '-');
235 /* now the first part must equal to rpc flavor name */
236 if (strcmp(buf, f) != 0)
237 GOTO(invalid, -EINVAL);
239 get_flavor_by_rpc(flvr, __flavors[i]);
244 /* find bulk algorithm string */
245 alg = strchr(bulk, ':');
249 /* verify bulk section */
250 if (strcmp(bulk, "bulkn") == 0) {
251 flvr->sf_bulk_hash = BULK_HASH_ALG_NULL;
252 flvr->sf_bulk_ciph = BULK_CIPH_ALG_NULL;
253 bulk_type = BULK_TYPE_N;
254 } else if (strcmp(bulk, "bulki") == 0)
255 bulk_type = BULK_TYPE_I;
256 else if (strcmp(bulk, "bulkp") == 0)
257 bulk_type = BULK_TYPE_P;
259 GOTO(invalid, -EINVAL);
261 /* null flavor don't support bulk i/p */
262 if (__flavors[i] == SPTLRPC_FLVR_NULL && bulk_type != BULK_TYPE_N)
263 GOTO(invalid, -EINVAL);
265 /* plain policy dosen't support bulk p */
266 if (__flavors[i] == SPTLRPC_FLVR_PLAIN && bulk_type == BULK_TYPE_P)
267 GOTO(invalid, -EINVAL);
269 get_flavor_by_bulk(flvr, __flavors[i], bulk_type);
274 /* find encryption algorithm string */
275 enc = strchr(alg, '/');
279 /* checksum algorithm */
280 for (i = 0; i < BULK_HASH_ALG_MAX; i++) {
281 if (strcmp(alg, sptlrpc_get_hash_name(i)) == 0) {
282 flvr->sf_bulk_hash = i;
286 if (i >= BULK_HASH_ALG_MAX)
287 GOTO(invalid, -EINVAL);
289 /* privacy algorithm */
291 for (i = 0; i < BULK_CIPH_ALG_MAX; i++) {
292 if (strcmp(enc, sptlrpc_get_ciph_name(i)) == 0) {
293 flvr->sf_bulk_ciph = i;
297 if (i >= BULK_CIPH_ALG_MAX)
298 GOTO(invalid, -EINVAL);
302 * bulk combination sanity checks
304 if (bulk_type == BULK_TYPE_P &&
305 flvr->sf_bulk_ciph == BULK_CIPH_ALG_NULL)
306 GOTO(invalid, -EINVAL);
308 if (bulk_type == BULK_TYPE_I &&
309 (flvr->sf_bulk_hash == BULK_HASH_ALG_NULL ||
310 flvr->sf_bulk_ciph != BULK_CIPH_ALG_NULL))
311 GOTO(invalid, -EINVAL);
313 if (bulk_type == BULK_TYPE_N &&
314 (flvr->sf_bulk_hash != BULK_HASH_ALG_NULL ||
315 flvr->sf_bulk_ciph != BULK_CIPH_ALG_NULL))
316 GOTO(invalid, -EINVAL);
321 CERROR("invalid flavor string: %s\n", str);
325 /****************************************
327 ****************************************/
329 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
331 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
332 rule->sr_from = LUSTRE_SP_ANY;
333 rule->sr_to = LUSTRE_SP_ANY;
334 rule->sr_padding = 0;
336 get_default_flavor(&rule->sr_flvr);
340 * format: network[.direction]=flavor
342 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
347 sptlrpc_rule_init(rule);
349 flavor = strchr(param, '=');
350 if (flavor == NULL) {
351 CERROR("invalid param, no '='\n");
356 dir = strchr(param, '.');
361 if (strcmp(param, "default")) {
362 rule->sr_netid = libcfs_str2net(param);
363 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
364 CERROR("invalid network name: %s\n", param);
371 if (!strcmp(dir, "mdt2ost")) {
372 rule->sr_from = LUSTRE_SP_MDT;
373 rule->sr_to = LUSTRE_SP_OST;
374 } else if (!strcmp(dir, "mdt2mdt")) {
375 rule->sr_from = LUSTRE_SP_MDT;
376 rule->sr_to = LUSTRE_SP_MDT;
377 } else if (!strcmp(dir, "cli2ost")) {
378 rule->sr_from = LUSTRE_SP_CLI;
379 rule->sr_to = LUSTRE_SP_OST;
380 } else if (!strcmp(dir, "cli2mdt")) {
381 rule->sr_from = LUSTRE_SP_CLI;
382 rule->sr_to = LUSTRE_SP_MDT;
384 CERROR("invalid rule dir segment: %s\n", dir);
390 rc = parse_flavor(flavor, &rule->sr_flvr);
396 EXPORT_SYMBOL(sptlrpc_parse_rule);
398 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
400 LASSERT(rset->srs_nslot ||
401 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
403 if (rset->srs_nslot) {
404 OBD_FREE(rset->srs_rules,
405 rset->srs_nslot * sizeof(*rset->srs_rules));
406 sptlrpc_rule_set_init(rset);
409 EXPORT_SYMBOL(sptlrpc_rule_set_free);
412 * return 0 if the rule set could accomodate one more rule.
413 * if @expand != 0, the rule set might be expanded.
415 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset, int expand)
417 struct sptlrpc_rule *rules;
420 if (rset->srs_nrule < rset->srs_nslot)
426 nslot = rset->srs_nslot + 8;
428 /* better use realloc() if available */
429 OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
433 if (rset->srs_nrule) {
434 LASSERT(rset->srs_nslot && rset->srs_rules);
435 memcpy(rules, rset->srs_rules,
436 rset->srs_nrule * sizeof(*rset->srs_rules));
438 OBD_FREE(rset->srs_rules,
439 rset->srs_nslot * sizeof(*rset->srs_rules));
442 rset->srs_rules = rules;
443 rset->srs_nslot = nslot;
446 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
448 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
450 return (rule->sr_from != LUSTRE_SP_ANY ||
451 rule->sr_to != LUSTRE_SP_ANY);
453 static inline int rule_spec_net(struct sptlrpc_rule *rule)
455 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
457 static inline int rule_match_dir(struct sptlrpc_rule *r1,
458 struct sptlrpc_rule *r2)
460 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
462 static inline int rule_match_net(struct sptlrpc_rule *r1,
463 struct sptlrpc_rule *r2)
465 return (r1->sr_netid == r2->sr_netid);
469 * merge @rule into @rset.
470 * if @expand != 0 then @rset slots might be expanded.
472 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
473 struct sptlrpc_rule *rule,
476 struct sptlrpc_rule *p = rset->srs_rules;
477 int spec_dir, spec_net;
478 int rc, n, match = 0;
480 spec_net = rule_spec_net(rule);
481 spec_dir = rule_spec_dir(rule);
483 for (n = 0; n < rset->srs_nrule; n++) {
484 p = &rset->srs_rules[n];
486 /* test network match, if failed:
487 * - spec rule: skip rules which is also spec rule match, until
488 * we hit a wild rule, which means no more chance
489 * - wild rule: skip until reach the one which is also wild
492 if (!rule_match_net(p, rule)) {
494 if (rule_spec_net(p))
503 /* test dir match, same logic as net matching */
504 if (!rule_match_dir(p, rule)) {
506 if (rule_spec_dir(p))
521 LASSERT(n >= 0 && n < rset->srs_nrule);
523 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
524 /* remove this rule */
525 if (n < rset->srs_nrule - 1)
526 memmove(&rset->srs_rules[n],
527 &rset->srs_rules[n + 1],
528 (rset->srs_nrule - n - 1) *
532 /* override the rule */
533 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
536 LASSERT(n >= 0 && n <= rset->srs_nrule);
538 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
539 rc = sptlrpc_rule_set_expand(rset, expand);
543 if (n < rset->srs_nrule)
544 memmove(&rset->srs_rules[n + 1],
546 (rset->srs_nrule - n) * sizeof(*rule));
547 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
550 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
556 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
559 * given from/to/nid, determine a matching flavor in ruleset.
560 * return 1 if a match found, otherwise return 0.
562 static int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
563 enum lustre_sec_part from,
564 enum lustre_sec_part to,
566 struct sptlrpc_flavor *sf)
568 struct sptlrpc_rule *r;
571 for (n = 0; n < rset->srs_nrule; n++) {
572 r = &rset->srs_rules[n];
574 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
575 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
576 LNET_NIDNET(nid) != r->sr_netid)
579 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
583 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
594 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
596 struct sptlrpc_rule *r;
599 for (n = 0; n < rset->srs_nrule; n++) {
600 r = &rset->srs_rules[n];
601 CWARN("<%02d> from %x to %x, net %x, rpc %x\n", n,
602 r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
605 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
607 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
608 struct sptlrpc_rule_set *tgt,
609 enum lustre_sec_part from,
610 enum lustre_sec_part to,
611 struct sptlrpc_rule_set *rset)
613 struct sptlrpc_rule_set *src[2] = { gen, tgt };
614 struct sptlrpc_rule *rule;
617 /* merge general rules firstly, then target-specific rules */
618 for (i = 0; i < 2; i++) {
622 for (n = 0; n < src[i]->srs_nrule; n++) {
623 rule = &src[i]->srs_rules[n];
625 if (from != LUSTRE_SP_ANY &&
626 rule->sr_from != LUSTRE_SP_ANY &&
627 rule->sr_from != from)
629 if (to != LUSTRE_SP_ANY &&
630 rule->sr_to != LUSTRE_SP_ANY &&
634 rc = sptlrpc_rule_set_merge(rset, rule, 1);
636 CERROR("can't merge: %d\n", rc);
645 /**********************************
646 * sptlrpc configuration support *
647 **********************************/
649 struct sptlrpc_conf_tgt {
650 struct list_head sct_list;
651 char sct_name[MAX_OBD_NAME];
652 struct sptlrpc_rule_set sct_rset;
655 struct sptlrpc_conf {
656 struct list_head sc_list;
657 char sc_fsname[MTI_NAME_MAXLEN];
658 unsigned int sc_modified; /* modified during updating */
659 unsigned int sc_updated:1, /* updated copy from MGS */
660 sc_local:1; /* local copy from target */
661 struct sptlrpc_rule_set sc_rset; /* fs general rules */
662 struct list_head sc_tgts; /* target-specific rules */
665 static struct mutex sptlrpc_conf_lock;
666 static CFS_LIST_HEAD(sptlrpc_confs);
668 static inline int is_hex(char c)
670 return ((c >= '0' && c <= '9') ||
671 (c >= 'a' && c <= 'f'));
674 static void target2fsname(const char *tgt, char *fsname, int buflen)
679 ptr = strrchr(tgt, '-');
681 if ((strncmp(ptr, "-MDT", 4) != 0 &&
682 strncmp(ptr, "-OST", 4) != 0) ||
683 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
684 !is_hex(ptr[6]) || !is_hex(ptr[7]))
688 /* if we didn't find the pattern, treat the whole string as fsname */
694 len = min(len, buflen - 1);
695 memcpy(fsname, tgt, len);
699 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
701 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
703 sptlrpc_rule_set_free(&conf->sc_rset);
705 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
706 &conf->sc_tgts, sct_list) {
707 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
708 list_del(&conf_tgt->sct_list);
709 OBD_FREE_PTR(conf_tgt);
711 LASSERT(list_empty(&conf->sc_tgts));
713 conf->sc_updated = 0;
717 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
719 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
721 sptlrpc_conf_free_rsets(conf);
722 list_del(&conf->sc_list);
727 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
731 struct sptlrpc_conf_tgt *conf_tgt;
733 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
734 if (strcmp(conf_tgt->sct_name, name) == 0)
741 OBD_ALLOC_PTR(conf_tgt);
743 strncpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
744 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
745 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
752 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
755 struct sptlrpc_conf *conf;
757 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
758 if (strcmp(conf->sc_fsname, fsname) == 0)
769 strcpy(conf->sc_fsname, fsname);
770 sptlrpc_rule_set_init(&conf->sc_rset);
771 CFS_INIT_LIST_HEAD(&conf->sc_tgts);
772 list_add(&conf->sc_list, &sptlrpc_confs);
774 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
779 * caller must hold conf_lock already.
781 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
783 struct sptlrpc_rule *rule)
785 struct sptlrpc_conf_tgt *conf_tgt;
786 struct sptlrpc_rule_set *rule_set;
788 /* fsname == target means general rules for the whole fs */
789 if (strcmp(conf->sc_fsname, target) == 0) {
790 rule_set = &conf->sc_rset;
792 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
794 rule_set = &conf_tgt->sct_rset;
796 CERROR("out of memory, can't merge rule!\n");
801 return sptlrpc_rule_set_merge(rule_set, rule, 1);
805 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
806 * find one through the target name in the record inside conf_lock;
807 * otherwise means caller already hold conf_lock.
809 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
810 struct sptlrpc_conf *conf)
812 char *target, *param;
813 char fsname[MTI_NAME_MAXLEN];
814 struct sptlrpc_rule rule;
818 target = lustre_cfg_string(lcfg, 1);
819 if (target == NULL) {
820 CERROR("missing target name\n");
824 param = lustre_cfg_string(lcfg, 2);
826 CERROR("missing parameter\n");
830 /* parse rule to make sure the format is correct */
831 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
832 CERROR("Invalid sptlrpc parameter: %s\n", param);
835 param += sizeof(PARAM_SRPC_FLVR) - 1;
837 rc = sptlrpc_parse_rule(param, &rule);
842 target2fsname(target, fsname, sizeof(fsname));
844 mutex_lock(&sptlrpc_conf_lock);
845 conf = sptlrpc_conf_get(fsname, 0);
847 CERROR("can't find conf\n");
850 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
852 mutex_unlock(&sptlrpc_conf_lock);
854 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
855 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
864 int sptlrpc_process_config(struct lustre_cfg *lcfg)
866 return __sptlrpc_process_config(lcfg, NULL);
868 EXPORT_SYMBOL(sptlrpc_process_config);
870 static int logname2fsname(const char *logname, char *buf, int buflen)
875 ptr = strrchr(logname, '-');
876 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
877 CERROR("%s is not a sptlrpc config log\n", logname);
881 len = min((int) (ptr - logname), buflen - 1);
883 memcpy(buf, logname, len);
888 void sptlrpc_conf_log_update_begin(const char *logname)
890 struct sptlrpc_conf *conf;
893 if (logname2fsname(logname, fsname, sizeof(fsname)))
896 mutex_lock(&sptlrpc_conf_lock);
898 conf = sptlrpc_conf_get(fsname, 0);
899 if (conf && conf->sc_local) {
900 LASSERT(conf->sc_updated == 0);
901 sptlrpc_conf_free_rsets(conf);
903 conf->sc_modified = 0;
905 mutex_unlock(&sptlrpc_conf_lock);
907 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
910 * mark a config log has been updated
912 void sptlrpc_conf_log_update_end(const char *logname)
914 struct sptlrpc_conf *conf;
917 if (logname2fsname(logname, fsname, sizeof(fsname)))
920 mutex_lock(&sptlrpc_conf_lock);
922 conf = sptlrpc_conf_get(fsname, 0);
925 * if original state is not updated, make sure the
926 * modified counter > 0 to enforce updating local copy.
928 if (conf->sc_updated == 0)
931 conf->sc_updated = 1;
934 mutex_unlock(&sptlrpc_conf_lock);
936 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
938 void sptlrpc_conf_log_start(const char *logname)
940 struct sptlrpc_conf *conf;
943 if (logname2fsname(logname, fsname, sizeof(fsname)))
946 mutex_lock(&sptlrpc_conf_lock);
947 conf = sptlrpc_conf_get(fsname, 1);
948 mutex_unlock(&sptlrpc_conf_lock);
950 EXPORT_SYMBOL(sptlrpc_conf_log_start);
952 void sptlrpc_conf_log_stop(const char *logname)
954 struct sptlrpc_conf *conf;
957 if (logname2fsname(logname, fsname, sizeof(fsname)))
960 mutex_lock(&sptlrpc_conf_lock);
961 conf = sptlrpc_conf_get(fsname, 0);
963 sptlrpc_conf_free(conf);
964 mutex_unlock(&sptlrpc_conf_lock);
966 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
968 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
969 enum lustre_sec_part from,
970 enum lustre_sec_part to,
971 unsigned int fl_udesc)
973 if (from == LUSTRE_SP_MDT) {
974 /* MDT->MDT; MDT->OST */
975 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
976 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
978 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
979 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
981 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
982 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
986 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
987 enum lustre_sec_part to,
988 struct obd_uuid *target,
990 struct sptlrpc_flavor *sf)
992 struct sptlrpc_conf *conf;
993 struct sptlrpc_conf_tgt *conf_tgt;
994 char name[MTI_NAME_MAXLEN];
997 target2fsname(target->uuid, name, sizeof(name));
999 mutex_lock(&sptlrpc_conf_lock);
1001 conf = sptlrpc_conf_get(name, 0);
1005 /* convert uuid name (supposed end with _UUID) to target name */
1006 len = strlen(target->uuid);
1008 memcpy(name, target->uuid, len - 5);
1009 name[len - 5] = '\0';
1011 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
1013 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
1019 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
1021 mutex_unlock(&sptlrpc_conf_lock);
1024 get_default_flavor(sf);
1026 flavor_set_flags(sf, from, to, 1);
1030 * called by target devices, determine the expected flavor from
1031 * certain peer (from, nid).
1033 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
1034 enum lustre_sec_part from,
1036 struct sptlrpc_flavor *sf)
1038 if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
1039 get_default_flavor(sf);
1041 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
1043 #define SEC_ADAPT_DELAY (10)
1046 * called by client devices, notify the sptlrpc config has changed and
1047 * do import_sec_adapt later.
1049 void sptlrpc_conf_client_adapt(struct obd_device *obd)
1051 struct obd_import *imp;
1054 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
1055 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
1056 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
1058 /* serialize with connect/disconnect import */
1059 down_read(&obd->u.cli.cl_sem);
1061 imp = obd->u.cli.cl_import;
1063 spin_lock(&imp->imp_lock);
1065 imp->imp_sec_expire = cfs_time_current_sec() +
1067 spin_unlock(&imp->imp_lock);
1070 up_read(&obd->u.cli.cl_sem);
1073 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
1077 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
1083 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
1086 net = libcfs_net2str(r->sr_netid);
1088 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
1091 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
1092 sptlrpc_part2name(r->sr_from),
1093 sptlrpc_part2name(r->sr_to));
1095 ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
1097 sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
1098 buf[buflen - 1] = '\0';
1101 static int sptlrpc_record_rule_set(struct llog_handle *llh,
1103 struct sptlrpc_rule_set *rset)
1105 struct lustre_cfg_bufs bufs;
1106 struct lustre_cfg *lcfg;
1107 struct llog_rec_hdr rec;
1112 for (i = 0; i < rset->srs_nrule; i++) {
1113 rule2string(&rset->srs_rules[i], param, sizeof(param));
1115 lustre_cfg_bufs_reset(&bufs, NULL);
1116 lustre_cfg_bufs_set_string(&bufs, 1, target);
1117 lustre_cfg_bufs_set_string(&bufs, 2, param);
1118 lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
1121 buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
1122 lcfg->lcfg_buflens);
1123 rec.lrh_len = llog_data_len(buflen);
1124 rec.lrh_type = OBD_CFG_REC;
1125 rc = llog_write_rec(llh, &rec, NULL, 0, (void *)lcfg, -1);
1127 CERROR("failed to write a rec: rc = %d\n", rc);
1128 lustre_cfg_free(lcfg);
1133 static int sptlrpc_record_rules(struct llog_handle *llh,
1134 struct sptlrpc_conf *conf)
1136 struct sptlrpc_conf_tgt *conf_tgt;
1138 sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
1140 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
1141 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
1142 &conf_tgt->sct_rset);
1147 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1148 #define LOG_SPTLRPC "sptlrpc"
1151 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1152 struct sptlrpc_conf *conf)
1154 struct llog_handle *llh = NULL;
1155 struct llog_ctxt *ctxt;
1156 struct lvfs_run_ctxt saved;
1157 struct dentry *dentry;
1161 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1163 CERROR("missing llog context\n");
1167 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1169 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
1170 strlen(MOUNT_CONFIGS_DIR));
1171 if (IS_ERR(dentry)) {
1172 rc = PTR_ERR(dentry);
1173 CERROR("cannot lookup %s directory: rc = %d\n",
1174 MOUNT_CONFIGS_DIR, rc);
1178 /* erase the old tmp log */
1179 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1181 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1183 rc = llog_destroy(llh);
1184 llog_free_handle(llh);
1191 CERROR("target %s: cannot erase temporary sptlrpc log: "
1192 "rc = %d\n", obd->obd_name, rc);
1196 /* write temporary log */
1197 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1200 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1202 GOTO(out_close, rc);
1204 rc = sptlrpc_record_rules(llh, conf);
1210 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1211 LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1217 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1218 llog_ctxt_put(ctxt);
1219 CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1224 static int local_read_handler(struct llog_handle *llh,
1225 struct llog_rec_hdr *rec,
1228 struct sptlrpc_conf *conf = (struct sptlrpc_conf *) data;
1229 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1233 if (rec->lrh_type != OBD_CFG_REC) {
1234 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1238 cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1239 sizeof(struct llog_rec_tail);
1241 rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1243 CERROR("Insane cfg\n");
1247 if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1248 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1252 RETURN(__sptlrpc_process_config(lcfg, conf));
1256 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1257 struct sptlrpc_conf *conf)
1259 struct llog_handle *llh = NULL;
1260 struct llog_ctxt *ctxt;
1261 struct lvfs_run_ctxt saved;
1265 LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1267 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1269 CERROR("missing llog context\n");
1273 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1275 rc = llog_create(ctxt, &llh, NULL, LOG_SPTLRPC);
1279 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1281 GOTO(out_close, rc);
1283 if (llog_get_size(llh) <= 1) {
1284 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1285 GOTO(out_close, rc = 0);
1288 rc = llog_process(llh, local_read_handler, (void *) conf, NULL);
1293 sptlrpc_conf_free_rsets(conf);
1299 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1300 llog_ctxt_put(ctxt);
1301 CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1306 #endif /* __KRENEL__ */
1309 * called by target devices, extract sptlrpc rules which applies to
1310 * this target, to be used for future rpc flavor checking.
1312 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1313 struct sptlrpc_rule_set *rset,
1316 struct sptlrpc_conf *conf;
1317 struct sptlrpc_conf_tgt *conf_tgt;
1318 enum lustre_sec_part sp_dst;
1319 char fsname[MTI_NAME_MAXLEN];
1323 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1324 sp_dst = LUSTRE_SP_MDT;
1325 } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1326 sp_dst = LUSTRE_SP_OST;
1328 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1331 CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1333 target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1335 mutex_lock(&sptlrpc_conf_lock);
1337 conf = sptlrpc_conf_get(fsname, 0);
1339 CERROR("missing sptlrpc config log\n");
1344 if (conf->sc_updated == 0) {
1346 * always read from local copy. here another option is
1347 * if we already have a local copy (read from another
1348 * target device hosted on the same node) we simply use that.
1351 sptlrpc_conf_free_rsets(conf);
1353 sptlrpc_target_local_read_conf(obd, conf);
1355 LASSERT(conf->sc_local == 0);
1357 /* write a local copy */
1358 if (initial || conf->sc_modified)
1359 sptlrpc_target_local_copy_conf(obd, conf);
1361 CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1365 /* extract rule set for this target */
1366 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1368 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1369 conf_tgt ? &conf_tgt->sct_rset: NULL,
1370 LUSTRE_SP_ANY, sp_dst, rset);
1372 mutex_unlock(&sptlrpc_conf_lock);
1375 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1377 int sptlrpc_conf_init(void)
1379 mutex_init(&sptlrpc_conf_lock);
1383 void sptlrpc_conf_fini(void)
1385 struct sptlrpc_conf *conf, *conf_next;
1387 mutex_lock(&sptlrpc_conf_lock);
1388 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
1389 sptlrpc_conf_free(conf);
1391 LASSERT(list_empty(&sptlrpc_confs));
1392 mutex_unlock(&sptlrpc_conf_lock);