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 "../../include/linux/libcfs/libcfs.h"
40 #include <linux/crypto.h>
41 #include <linux/key.h>
43 #include "../include/obd.h"
44 #include "../include/obd_support.h"
45 #include "../include/lustre_import.h"
46 #include "../include/lustre_param.h"
47 #include "../include/lustre_sec.h"
49 #include "ptlrpc_internal.h"
51 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
53 const char *type = obd->obd_type->typ_name;
55 if (!strcmp(type, LUSTRE_MDT_NAME))
57 if (!strcmp(type, LUSTRE_OST_NAME))
59 if (!strcmp(type, LUSTRE_MGS_NAME))
62 CERROR("unknown target %p(%s)\n", obd, type);
65 EXPORT_SYMBOL(sptlrpc_target_sec_part);
67 /****************************************
68 * user supplied flavor string parsing *
69 ****************************************/
72 * format: <base_flavor>[-<bulk_type:alg_spec>]
74 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
79 memset(flvr, 0, sizeof(*flvr));
81 if (str == NULL || str[0] == '\0') {
82 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
86 strlcpy(buf, str, sizeof(buf));
88 bulk = strchr(buf, '-');
92 flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
93 if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
97 * currently only base flavor "plain" can have bulk specification.
99 if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
100 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
103 * format: plain-hash:<hash_alg>
105 alg = strchr(bulk, ':');
110 if (strcmp(bulk, "hash"))
113 flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
114 if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
118 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
119 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
121 flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
131 CERROR("invalid flavor string: %s\n", str);
134 EXPORT_SYMBOL(sptlrpc_parse_flavor);
136 /****************************************
138 ****************************************/
140 static void get_default_flavor(struct sptlrpc_flavor *sf)
142 memset(sf, 0, sizeof(*sf));
144 sf->sf_rpc = SPTLRPC_FLVR_NULL;
148 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
150 rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
151 rule->sr_from = LUSTRE_SP_ANY;
152 rule->sr_to = LUSTRE_SP_ANY;
153 rule->sr_padding = 0;
155 get_default_flavor(&rule->sr_flvr);
159 * format: network[.direction]=flavor
161 static int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
166 sptlrpc_rule_init(rule);
168 flavor = strchr(param, '=');
169 if (flavor == NULL) {
170 CERROR("invalid param, no '='\n");
175 dir = strchr(param, '.');
180 if (strcmp(param, "default")) {
181 rule->sr_netid = libcfs_str2net(param);
182 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
183 CERROR("invalid network name: %s\n", param);
190 if (!strcmp(dir, "mdt2ost")) {
191 rule->sr_from = LUSTRE_SP_MDT;
192 rule->sr_to = LUSTRE_SP_OST;
193 } else if (!strcmp(dir, "mdt2mdt")) {
194 rule->sr_from = LUSTRE_SP_MDT;
195 rule->sr_to = LUSTRE_SP_MDT;
196 } else if (!strcmp(dir, "cli2ost")) {
197 rule->sr_from = LUSTRE_SP_CLI;
198 rule->sr_to = LUSTRE_SP_OST;
199 } else if (!strcmp(dir, "cli2mdt")) {
200 rule->sr_from = LUSTRE_SP_CLI;
201 rule->sr_to = LUSTRE_SP_MDT;
203 CERROR("invalid rule dir segment: %s\n", dir);
209 rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
216 static void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
218 LASSERT(rset->srs_nslot ||
219 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
221 if (rset->srs_nslot) {
222 kfree(rset->srs_rules);
223 sptlrpc_rule_set_init(rset);
228 * return 0 if the rule set could accommodate one more rule.
230 static int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
232 struct sptlrpc_rule *rules;
237 if (rset->srs_nrule < rset->srs_nslot)
240 nslot = rset->srs_nslot + 8;
242 /* better use realloc() if available */
243 rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
247 if (rset->srs_nrule) {
248 LASSERT(rset->srs_nslot && rset->srs_rules);
249 memcpy(rules, rset->srs_rules,
250 rset->srs_nrule * sizeof(*rset->srs_rules));
252 kfree(rset->srs_rules);
255 rset->srs_rules = rules;
256 rset->srs_nslot = nslot;
260 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
262 return (rule->sr_from != LUSTRE_SP_ANY ||
263 rule->sr_to != LUSTRE_SP_ANY);
266 static inline int rule_spec_net(struct sptlrpc_rule *rule)
268 return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
271 static inline int rule_match_dir(struct sptlrpc_rule *r1,
272 struct sptlrpc_rule *r2)
274 return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
277 static inline int rule_match_net(struct sptlrpc_rule *r1,
278 struct sptlrpc_rule *r2)
280 return (r1->sr_netid == r2->sr_netid);
284 * merge @rule into @rset.
285 * the @rset slots might be expanded.
287 static int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
288 struct sptlrpc_rule *rule)
290 struct sptlrpc_rule *p = rset->srs_rules;
291 int spec_dir, spec_net;
292 int rc, n, match = 0;
296 spec_net = rule_spec_net(rule);
297 spec_dir = rule_spec_dir(rule);
299 for (n = 0; n < rset->srs_nrule; n++) {
300 p = &rset->srs_rules[n];
302 /* test network match, if failed:
303 * - spec rule: skip rules which is also spec rule match, until
304 * we hit a wild rule, which means no more chance
305 * - wild rule: skip until reach the one which is also wild
308 if (!rule_match_net(p, rule)) {
310 if (rule_spec_net(p))
319 /* test dir match, same logic as net matching */
320 if (!rule_match_dir(p, rule)) {
322 if (rule_spec_dir(p))
337 LASSERT(n >= 0 && n < rset->srs_nrule);
339 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
340 /* remove this rule */
341 if (n < rset->srs_nrule - 1)
342 memmove(&rset->srs_rules[n],
343 &rset->srs_rules[n + 1],
344 (rset->srs_nrule - n - 1) *
348 /* override the rule */
349 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
352 LASSERT(n >= 0 && n <= rset->srs_nrule);
354 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
355 rc = sptlrpc_rule_set_expand(rset);
359 if (n < rset->srs_nrule)
360 memmove(&rset->srs_rules[n + 1],
362 (rset->srs_nrule - n) * sizeof(*rule));
363 memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
366 CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
374 * given from/to/nid, determine a matching flavor in ruleset.
375 * return 1 if a match found, otherwise return 0.
377 static int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
378 enum lustre_sec_part from,
379 enum lustre_sec_part to,
381 struct sptlrpc_flavor *sf)
383 struct sptlrpc_rule *r;
386 for (n = 0; n < rset->srs_nrule; n++) {
387 r = &rset->srs_rules[n];
389 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
390 r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
391 LNET_NIDNET(nid) != r->sr_netid)
394 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
398 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
409 /**********************************
410 * sptlrpc configuration support *
411 **********************************/
413 struct sptlrpc_conf_tgt {
414 struct list_head sct_list;
415 char sct_name[MAX_OBD_NAME];
416 struct sptlrpc_rule_set sct_rset;
419 struct sptlrpc_conf {
420 struct list_head sc_list;
421 char sc_fsname[MTI_NAME_MAXLEN];
422 unsigned int sc_modified; /* modified during updating */
423 unsigned int sc_updated:1, /* updated copy from MGS */
424 sc_local:1; /* local copy from target */
425 struct sptlrpc_rule_set sc_rset; /* fs general rules */
426 struct list_head sc_tgts; /* target-specific rules */
429 static struct mutex sptlrpc_conf_lock;
430 static LIST_HEAD(sptlrpc_confs);
432 static inline int is_hex(char c)
434 return ((c >= '0' && c <= '9') ||
435 (c >= 'a' && c <= 'f'));
438 static void target2fsname(const char *tgt, char *fsname, int buflen)
443 ptr = strrchr(tgt, '-');
445 if ((strncmp(ptr, "-MDT", 4) != 0 &&
446 strncmp(ptr, "-OST", 4) != 0) ||
447 !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
448 !is_hex(ptr[6]) || !is_hex(ptr[7]))
452 /* if we didn't find the pattern, treat the whole string as fsname */
458 len = min(len, buflen - 1);
459 memcpy(fsname, tgt, len);
463 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
465 struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
467 sptlrpc_rule_set_free(&conf->sc_rset);
469 list_for_each_entry_safe(conf_tgt, conf_tgt_next,
470 &conf->sc_tgts, sct_list) {
471 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
472 list_del(&conf_tgt->sct_list);
475 LASSERT(list_empty(&conf->sc_tgts));
477 conf->sc_updated = 0;
481 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
483 CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
485 sptlrpc_conf_free_rsets(conf);
486 list_del(&conf->sc_list);
491 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
495 struct sptlrpc_conf_tgt *conf_tgt;
497 list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
498 if (strcmp(conf_tgt->sct_name, name) == 0)
505 conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
507 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
508 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
509 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
516 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
519 struct sptlrpc_conf *conf;
521 list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
522 if (strcmp(conf->sc_fsname, fsname) == 0)
529 conf = kzalloc(sizeof(*conf), GFP_NOFS);
533 strcpy(conf->sc_fsname, fsname);
534 sptlrpc_rule_set_init(&conf->sc_rset);
535 INIT_LIST_HEAD(&conf->sc_tgts);
536 list_add(&conf->sc_list, &sptlrpc_confs);
538 CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
543 * caller must hold conf_lock already.
545 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
547 struct sptlrpc_rule *rule)
549 struct sptlrpc_conf_tgt *conf_tgt;
550 struct sptlrpc_rule_set *rule_set;
552 /* fsname == target means general rules for the whole fs */
553 if (strcmp(conf->sc_fsname, target) == 0) {
554 rule_set = &conf->sc_rset;
556 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
558 rule_set = &conf_tgt->sct_rset;
560 CERROR("out of memory, can't merge rule!\n");
565 return sptlrpc_rule_set_merge(rule_set, rule);
569 * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
570 * find one through the target name in the record inside conf_lock;
571 * otherwise means caller already hold conf_lock.
573 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
574 struct sptlrpc_conf *conf)
576 char *target, *param;
577 char fsname[MTI_NAME_MAXLEN];
578 struct sptlrpc_rule rule;
581 target = lustre_cfg_string(lcfg, 1);
582 if (target == NULL) {
583 CERROR("missing target name\n");
587 param = lustre_cfg_string(lcfg, 2);
589 CERROR("missing parameter\n");
593 CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
595 /* parse rule to make sure the format is correct */
596 if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
597 CERROR("Invalid sptlrpc parameter: %s\n", param);
600 param += sizeof(PARAM_SRPC_FLVR) - 1;
602 rc = sptlrpc_parse_rule(param, &rule);
607 target2fsname(target, fsname, sizeof(fsname));
609 mutex_lock(&sptlrpc_conf_lock);
610 conf = sptlrpc_conf_get(fsname, 0);
612 CERROR("can't find conf\n");
615 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
617 mutex_unlock(&sptlrpc_conf_lock);
619 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
620 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
629 int sptlrpc_process_config(struct lustre_cfg *lcfg)
631 return __sptlrpc_process_config(lcfg, NULL);
633 EXPORT_SYMBOL(sptlrpc_process_config);
635 static int logname2fsname(const char *logname, char *buf, int buflen)
640 ptr = strrchr(logname, '-');
641 if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
642 CERROR("%s is not a sptlrpc config log\n", logname);
646 len = min((int) (ptr - logname), buflen - 1);
648 memcpy(buf, logname, len);
653 void sptlrpc_conf_log_update_begin(const char *logname)
655 struct sptlrpc_conf *conf;
658 if (logname2fsname(logname, fsname, sizeof(fsname)))
661 mutex_lock(&sptlrpc_conf_lock);
663 conf = sptlrpc_conf_get(fsname, 0);
665 if (conf->sc_local) {
666 LASSERT(conf->sc_updated == 0);
667 sptlrpc_conf_free_rsets(conf);
669 conf->sc_modified = 0;
672 mutex_unlock(&sptlrpc_conf_lock);
674 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
677 * mark a config log has been updated
679 void sptlrpc_conf_log_update_end(const char *logname)
681 struct sptlrpc_conf *conf;
684 if (logname2fsname(logname, fsname, sizeof(fsname)))
687 mutex_lock(&sptlrpc_conf_lock);
689 conf = sptlrpc_conf_get(fsname, 0);
692 * if original state is not updated, make sure the
693 * modified counter > 0 to enforce updating local copy.
695 if (conf->sc_updated == 0)
698 conf->sc_updated = 1;
701 mutex_unlock(&sptlrpc_conf_lock);
703 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
705 void sptlrpc_conf_log_start(const char *logname)
709 if (logname2fsname(logname, fsname, sizeof(fsname)))
712 mutex_lock(&sptlrpc_conf_lock);
713 sptlrpc_conf_get(fsname, 1);
714 mutex_unlock(&sptlrpc_conf_lock);
716 EXPORT_SYMBOL(sptlrpc_conf_log_start);
718 void sptlrpc_conf_log_stop(const char *logname)
720 struct sptlrpc_conf *conf;
723 if (logname2fsname(logname, fsname, sizeof(fsname)))
726 mutex_lock(&sptlrpc_conf_lock);
727 conf = sptlrpc_conf_get(fsname, 0);
729 sptlrpc_conf_free(conf);
730 mutex_unlock(&sptlrpc_conf_lock);
732 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
734 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
735 enum lustre_sec_part from,
736 enum lustre_sec_part to,
737 unsigned int fl_udesc)
740 * null flavor doesn't need to set any flavor, and in fact
741 * we'd better not do that because everybody share a single sec.
743 if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
746 if (from == LUSTRE_SP_MDT) {
747 /* MDT->MDT; MDT->OST */
748 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
749 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
751 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
752 } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
754 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
755 sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
759 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
760 enum lustre_sec_part to,
761 struct obd_uuid *target,
763 struct sptlrpc_flavor *sf)
765 struct sptlrpc_conf *conf;
766 struct sptlrpc_conf_tgt *conf_tgt;
767 char name[MTI_NAME_MAXLEN];
770 target2fsname(target->uuid, name, sizeof(name));
772 mutex_lock(&sptlrpc_conf_lock);
774 conf = sptlrpc_conf_get(name, 0);
778 /* convert uuid name (supposed end with _UUID) to target name */
779 len = strlen(target->uuid);
781 memcpy(name, target->uuid, len - 5);
782 name[len - 5] = '\0';
784 conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
786 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
792 rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
794 mutex_unlock(&sptlrpc_conf_lock);
797 get_default_flavor(sf);
799 flavor_set_flags(sf, from, to, 1);
802 #define SEC_ADAPT_DELAY (10)
805 * called by client devices, notify the sptlrpc config has changed and
806 * do import_sec_adapt later.
808 void sptlrpc_conf_client_adapt(struct obd_device *obd)
810 struct obd_import *imp;
812 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
813 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
814 CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
816 /* serialize with connect/disconnect import */
817 down_read(&obd->u.cli.cl_sem);
819 imp = obd->u.cli.cl_import;
821 spin_lock(&imp->imp_lock);
823 imp->imp_sec_expire = ktime_get_real_seconds() +
825 spin_unlock(&imp->imp_lock);
828 up_read(&obd->u.cli.cl_sem);
830 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
832 int sptlrpc_conf_init(void)
834 mutex_init(&sptlrpc_conf_lock);
838 void sptlrpc_conf_fini(void)
840 struct sptlrpc_conf *conf, *conf_next;
842 mutex_lock(&sptlrpc_conf_lock);
843 list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
844 sptlrpc_conf_free(conf);
846 LASSERT(list_empty(&sptlrpc_confs));
847 mutex_unlock(&sptlrpc_conf_lock);