Whamcloud - gitweb
LU-14291 ptlrpc: format UPDATE messages in server-only code
[fs/lustre-release.git] / lustre / ptlrpc / sec_config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_SEC
34
35 #include <libcfs/libcfs.h>
36 #include <linux/crypto.h>
37 #include <linux/key.h>
38
39 #include <obd.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_log.h>
45 #include <lustre_disk.h>
46 #include <lustre_dlm.h>
47 #include <uapi/linux/lustre/lustre_param.h>
48 #include <lustre_sec.h>
49
50 #include "ptlrpc_internal.h"
51
52 const char *sptlrpc_part2name(enum lustre_sec_part part)
53 {
54         switch (part) {
55         case LUSTRE_SP_CLI:
56                 return "cli";
57         case LUSTRE_SP_MDT:
58                 return "mdt";
59         case LUSTRE_SP_OST:
60                 return "ost";
61         case LUSTRE_SP_MGC:
62                 return "mgc";
63         case LUSTRE_SP_MGS:
64                 return "mgs";
65         case LUSTRE_SP_ANY:
66                 return "any";
67         default:
68                 return "err";
69         }
70 }
71 EXPORT_SYMBOL(sptlrpc_part2name);
72
73 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
74 {
75         const char *type = obd->obd_type->typ_name;
76
77         if (!strcmp(type, LUSTRE_MDT_NAME))
78                 return LUSTRE_SP_MDT;
79         if (!strcmp(type, LUSTRE_OST_NAME))
80                 return LUSTRE_SP_OST;
81         if (!strcmp(type, LUSTRE_MGS_NAME))
82                 return LUSTRE_SP_MGS;
83
84         CERROR("unknown target %p(%s)\n", obd, type);
85         return LUSTRE_SP_ANY;
86 }
87
88 /****************************************
89  * user supplied flavor string parsing  *
90  ****************************************/
91
92 /*
93  * format: <base_flavor>[-<bulk_type:alg_spec>]
94  */
95 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
96 {
97         char            buf[32];
98         char           *bulk, *alg;
99
100         memset(flvr, 0, sizeof(*flvr));
101
102         if (str == NULL || str[0] == '\0') {
103                 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
104                 return 0;
105         }
106
107         strlcpy(buf, str, sizeof(buf));
108
109         bulk = strchr(buf, '-');
110         if (bulk)
111                 *bulk++ = '\0';
112
113         flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
114         if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
115                 goto err_out;
116
117         /*
118          * currently only base flavor "plain" can have bulk specification.
119          */
120         if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
121                 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
122                 if (bulk) {
123                         /*
124                          * format: plain-hash:<hash_alg>
125                          */
126                         alg = strchr(bulk, ':');
127                         if (alg == NULL)
128                                 goto err_out;
129                         *alg++ = '\0';
130
131                         if (strcmp(bulk, "hash"))
132                                 goto err_out;
133
134                         flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
135                         if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
136                                 goto err_out;
137                 }
138
139                 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
140                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
141                 else
142                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
143         } else {
144                 if (bulk)
145                         goto err_out;
146         }
147
148         flvr->sf_flags = 0;
149         return 0;
150
151 err_out:
152         CERROR("invalid flavor string: %s\n", str);
153         return -EINVAL;
154 }
155 EXPORT_SYMBOL(sptlrpc_parse_flavor);
156
157 /****************************************
158  * configure rules                      *
159  ****************************************/
160
161 static void get_default_flavor(struct sptlrpc_flavor *sf)
162 {
163         memset(sf, 0, sizeof(*sf));
164
165         sf->sf_rpc = SPTLRPC_FLVR_NULL;
166         sf->sf_flags = 0;
167 }
168
169 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
170 {
171         rule->sr_netid = LNET_NET_ANY;
172         rule->sr_from = LUSTRE_SP_ANY;
173         rule->sr_to = LUSTRE_SP_ANY;
174         rule->sr_padding = 0;
175
176         get_default_flavor(&rule->sr_flvr);
177 }
178
179 /*
180  * format: network[.direction]=flavor
181  */
182 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
183 {
184         char *flavor, *dir;
185         int rc;
186
187         sptlrpc_rule_init(rule);
188
189         flavor = strchr(param, '=');
190         if (flavor == NULL) {
191                 CERROR("invalid param, no '='\n");
192                 RETURN(-EINVAL);
193         }
194         *flavor++ = '\0';
195
196         dir = strchr(param, '.');
197         if (dir)
198                 *dir++ = '\0';
199
200         /* 1.1 network */
201         if (strcmp(param, "default")) {
202                 rule->sr_netid = libcfs_str2net(param);
203                 if (rule->sr_netid == LNET_NET_ANY) {
204                         CERROR("invalid network name: %s\n", param);
205                         RETURN(-EINVAL);
206                 }
207         }
208
209         /* 1.2 direction */
210         if (dir) {
211                 if (!strcmp(dir, "mdt2ost")) {
212                         rule->sr_from = LUSTRE_SP_MDT;
213                         rule->sr_to = LUSTRE_SP_OST;
214                 } else if (!strcmp(dir, "mdt2mdt")) {
215                         rule->sr_from = LUSTRE_SP_MDT;
216                         rule->sr_to = LUSTRE_SP_MDT;
217                 } else if (!strcmp(dir, "cli2ost")) {
218                         rule->sr_from = LUSTRE_SP_CLI;
219                         rule->sr_to = LUSTRE_SP_OST;
220                 } else if (!strcmp(dir, "cli2mdt")) {
221                         rule->sr_from = LUSTRE_SP_CLI;
222                         rule->sr_to = LUSTRE_SP_MDT;
223                 } else {
224                         CERROR("invalid rule dir segment: %s\n", dir);
225                         RETURN(-EINVAL);
226                 }
227         }
228
229         /* 2.1 flavor */
230         rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
231         if (rc)
232                 RETURN(-EINVAL);
233
234         RETURN(0);
235 }
236 EXPORT_SYMBOL(sptlrpc_parse_rule);
237
238 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
239 {
240         LASSERT(rset->srs_nslot ||
241                 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
242
243         if (rset->srs_nslot) {
244                 OBD_FREE_PTR_ARRAY(rset->srs_rules, rset->srs_nslot);
245                 sptlrpc_rule_set_init(rset);
246         }
247 }
248 EXPORT_SYMBOL(sptlrpc_rule_set_free);
249
250 /*
251  * return 0 if the rule set could accomodate one more rule.
252  */
253 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
254 {
255         struct sptlrpc_rule *rules;
256         int nslot;
257
258         might_sleep();
259
260         if (rset->srs_nrule < rset->srs_nslot)
261                 return 0;
262
263         nslot = rset->srs_nslot + 8;
264
265         /* better use realloc() if available */
266         OBD_ALLOC_PTR_ARRAY(rules, nslot);
267         if (rules == NULL)
268                 return -ENOMEM;
269
270         if (rset->srs_nrule) {
271                 LASSERT(rset->srs_nslot && rset->srs_rules);
272                 memcpy(rules, rset->srs_rules,
273                        rset->srs_nrule * sizeof(*rset->srs_rules));
274
275                 OBD_FREE_PTR_ARRAY(rset->srs_rules, rset->srs_nslot);
276         }
277
278         rset->srs_rules = rules;
279         rset->srs_nslot = nslot;
280         return 0;
281 }
282
283 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
284 {
285         return (rule->sr_from != LUSTRE_SP_ANY ||
286                 rule->sr_to != LUSTRE_SP_ANY);
287 }
288 static inline int rule_spec_net(struct sptlrpc_rule *rule)
289 {
290         return (rule->sr_netid != LNET_NET_ANY);
291 }
292 static inline int rule_match_dir(struct sptlrpc_rule *r1,
293                                  struct sptlrpc_rule *r2)
294 {
295         return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
296 }
297 static inline int rule_match_net(struct sptlrpc_rule *r1,
298                                  struct sptlrpc_rule *r2)
299 {
300         return (r1->sr_netid == r2->sr_netid);
301 }
302
303 /*
304  * merge @rule into @rset.
305  * the @rset slots might be expanded.
306  */
307 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset, 
308                            struct sptlrpc_rule *rule)
309 {
310         struct sptlrpc_rule      *p = rset->srs_rules;
311         int                       spec_dir, spec_net;
312         int                       rc, n, match = 0;
313
314         might_sleep();
315
316         spec_net = rule_spec_net(rule);
317         spec_dir = rule_spec_dir(rule);
318
319         for (n = 0; n < rset->srs_nrule; n++) {
320                 p = &rset->srs_rules[n]; 
321
322                 /* test network match, if failed:
323                  * - spec rule: skip rules which is also spec rule match, until
324                  *   we hit a wild rule, which means no more chance
325                  * - wild rule: skip until reach the one which is also wild
326                  *   and matches
327                  */
328                 if (!rule_match_net(p, rule)) {
329                         if (spec_net) {
330                                 if (rule_spec_net(p))
331                                         continue;
332                                 else
333                                         break;
334                         } else {
335                                 continue;
336                         }
337                 }
338
339                 /* test dir match, same logic as net matching */
340                 if (!rule_match_dir(p, rule)) {
341                         if (spec_dir) {
342                                 if (rule_spec_dir(p))
343                                         continue;
344                                 else
345                                         break;
346                         } else {
347                                 continue;
348                         }
349                 }
350
351                 /* find a match */
352                 match = 1;
353                 break;
354         }
355
356         if (match) {
357                 LASSERT(n >= 0 && n < rset->srs_nrule);
358
359                 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
360                         /* remove this rule */
361                         if (n < rset->srs_nrule - 1)
362                                 memmove(&rset->srs_rules[n],
363                                         &rset->srs_rules[n + 1],
364                                         (rset->srs_nrule - n - 1) *
365                                         sizeof(*rule));
366                         rset->srs_nrule--;
367                 } else {
368                         /* override the rule */
369                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
370                 }
371         } else {
372                 LASSERT(n >= 0 && n <= rset->srs_nrule);
373
374                 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
375                         rc = sptlrpc_rule_set_expand(rset);
376                         if (rc)
377                                 return rc;
378
379                         if (n < rset->srs_nrule)
380                                 memmove(&rset->srs_rules[n + 1],
381                                         &rset->srs_rules[n],
382                                         (rset->srs_nrule - n) * sizeof(*rule));
383                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
384                         rset->srs_nrule++;
385                 } else {
386                         CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
387                 }
388         }
389
390         return 0;
391 }
392 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
393
394 /**
395  * given from/to/nid, determine a matching flavor in ruleset.
396  * return 1 if a match found, otherwise return 0.
397  */
398 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
399                             enum lustre_sec_part from,
400                             enum lustre_sec_part to,
401                             lnet_nid_t nid,
402                             struct sptlrpc_flavor *sf)
403 {
404         struct sptlrpc_rule *r;
405         int n;
406
407         for (n = 0; n < rset->srs_nrule; n++) {
408                 r = &rset->srs_rules[n];
409
410                 if (LNET_NIDNET(nid) != LNET_NET_ANY &&
411                     r->sr_netid != LNET_NET_ANY &&
412                     LNET_NIDNET(nid) != r->sr_netid)
413                         continue;
414
415                 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
416                     from != r->sr_from)
417                         continue;
418
419                 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
420                     to != r->sr_to)
421                         continue;
422
423                 *sf = r->sr_flvr;
424                 return 1;
425         }
426
427         return 0;
428 }
429
430 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
431 {
432         struct sptlrpc_rule *r;
433         int     n;
434
435         for (n = 0; n < rset->srs_nrule; n++) {
436                 r = &rset->srs_rules[n];
437                 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
438                        r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
439         }
440 }
441
442 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
443                                     struct sptlrpc_rule_set *tgt,
444                                     enum lustre_sec_part from,
445                                     enum lustre_sec_part to,
446                                     struct sptlrpc_rule_set *rset)
447 {
448         struct sptlrpc_rule_set *src[2] = { gen, tgt };
449         struct sptlrpc_rule     *rule;
450         int                      i, n, rc;
451
452         might_sleep();
453
454         /* merge general rules firstly, then target-specific rules */
455         for (i = 0; i < 2; i++) {
456                 if (src[i] == NULL)
457                         continue;
458
459                 for (n = 0; n < src[i]->srs_nrule; n++) {
460                         rule = &src[i]->srs_rules[n];
461
462                         if (from != LUSTRE_SP_ANY &&
463                             rule->sr_from != LUSTRE_SP_ANY &&
464                             rule->sr_from != from)
465                                 continue;
466                         if (to != LUSTRE_SP_ANY &&
467                             rule->sr_to != LUSTRE_SP_ANY &&
468                             rule->sr_to != to)
469                                 continue;
470
471                         rc = sptlrpc_rule_set_merge(rset, rule);
472                         if (rc) {
473                                 CERROR("can't merge: %d\n", rc);
474                                 return rc;
475                         }
476                 }
477         }
478
479         return 0;
480 }
481
482 /**********************************
483  * sptlrpc configuration support  *
484  **********************************/
485
486 struct sptlrpc_conf_tgt {
487         struct list_head              sct_list;
488         char                    sct_name[MAX_OBD_NAME];
489         struct sptlrpc_rule_set sct_rset;
490 };
491
492 struct sptlrpc_conf {
493         struct list_head        sc_list;
494         char                    sc_fsname[MTI_NAME_MAXLEN];
495         unsigned int            sc_modified;    /* modified during updating */
496         unsigned int            sc_updated:1,   /* updated copy from MGS */
497                                 sc_local:1;     /* local copy from target */
498         struct sptlrpc_rule_set sc_rset;        /* fs general rules */
499         struct list_head        sc_tgts;        /* target-specific rules */
500 };
501
502 static struct mutex sptlrpc_conf_lock;
503 static LIST_HEAD(sptlrpc_confs);
504
505 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
506 {
507         struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
508
509         sptlrpc_rule_set_free(&conf->sc_rset);
510
511         list_for_each_entry_safe(conf_tgt, conf_tgt_next,
512                                  &conf->sc_tgts, sct_list) {
513                 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
514                 list_del(&conf_tgt->sct_list);
515                 OBD_FREE_PTR(conf_tgt);
516         }
517         LASSERT(list_empty(&conf->sc_tgts));
518
519         conf->sc_updated = 0;
520         conf->sc_local = 0;
521 }
522
523 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
524 {
525         CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
526
527         sptlrpc_conf_free_rsets(conf);
528         list_del(&conf->sc_list);
529         OBD_FREE_PTR(conf);
530 }
531
532 static
533 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
534                                               const char *name,
535                                               int create)
536 {
537         struct sptlrpc_conf_tgt *conf_tgt;
538
539         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
540                 if (strcmp(conf_tgt->sct_name, name) == 0)
541                         return conf_tgt;
542         }
543
544         if (!create)
545                 return NULL;
546
547         OBD_ALLOC_PTR(conf_tgt);
548         if (conf_tgt) {
549                 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
550                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
551                 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
552         }
553
554         return conf_tgt;
555 }
556
557 static
558 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
559                                       int create)
560 {
561         struct sptlrpc_conf *conf;
562
563         list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
564                 if (strcmp(conf->sc_fsname, fsname) == 0)
565                         return conf;
566         }
567
568         if (!create)
569                 return NULL;
570
571         OBD_ALLOC_PTR(conf);
572         if (conf == NULL)
573                 return NULL;
574
575         if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
576             sizeof(conf->sc_fsname)) {
577                 OBD_FREE_PTR(conf);
578                 return NULL;
579         }
580         sptlrpc_rule_set_init(&conf->sc_rset);
581         INIT_LIST_HEAD(&conf->sc_tgts);
582         list_add(&conf->sc_list, &sptlrpc_confs);
583
584         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
585         return conf;
586 }
587
588 /**
589  * caller must hold conf_lock already.
590  */
591 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
592                                    const char *target,
593                                    struct sptlrpc_rule *rule)
594 {
595         struct sptlrpc_conf_tgt  *conf_tgt;
596         struct sptlrpc_rule_set  *rule_set;
597
598         /* fsname == target means general rules for the whole fs */
599         if (strcmp(conf->sc_fsname, target) == 0) {
600                 rule_set = &conf->sc_rset;
601         } else {
602                 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
603                 if (conf_tgt) {
604                         rule_set = &conf_tgt->sct_rset;
605                 } else {
606                         CERROR("out of memory, can't merge rule!\n");
607                         return -ENOMEM;
608                 }
609         }
610
611         return sptlrpc_rule_set_merge(rule_set, rule);
612 }
613
614 /**
615  * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
616  * find one through the target name in the record inside conf_lock;
617  * otherwise means caller already hold conf_lock.
618  */
619 static int __sptlrpc_process_config(char *target, const char *fsname,
620                                     struct sptlrpc_rule *rule,
621                                     struct sptlrpc_conf *conf)
622 {
623         int rc;
624
625         ENTRY;
626         if (!conf) {
627                 if (!fsname)
628                         return -ENODEV;
629
630                 mutex_lock(&sptlrpc_conf_lock);
631                 conf = sptlrpc_conf_get(fsname, 0);
632                 if (!conf) {
633                         CERROR("can't find conf\n");
634                         rc = -ENOMEM;
635                 } else {
636                         rc = sptlrpc_conf_merge_rule(conf, target, rule);
637                 }
638                 mutex_unlock(&sptlrpc_conf_lock);
639         } else {
640                 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
641                 rc = sptlrpc_conf_merge_rule(conf, target, rule);
642         }
643
644         if (!rc)
645                 conf->sc_modified++;
646
647         RETURN(rc);
648 }
649
650 int sptlrpc_process_config(struct lustre_cfg *lcfg)
651 {
652         char fsname[MTI_NAME_MAXLEN];
653         struct sptlrpc_rule rule;
654         char *target, *param;
655         int rc;
656
657         print_lustre_cfg(lcfg);
658
659         target = lustre_cfg_string(lcfg, 1);
660         if (!target) {
661                 CERROR("missing target name\n");
662                 return -EINVAL;
663         }
664
665         param = lustre_cfg_string(lcfg, 2);
666         if (!param) {
667                 CERROR("missing parameter\n");
668                 return -EINVAL;
669         }
670
671         /* parse rule to make sure the format is correct */
672         if (strncmp(param, PARAM_SRPC_FLVR,
673                     sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
674                 CERROR("Invalid sptlrpc parameter: %s\n", param);
675                 return -EINVAL;
676         }
677         param += sizeof(PARAM_SRPC_FLVR) - 1;
678
679         CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
680
681         /*
682          * Three types of targets exist for sptlrpc using conf_param
683          * 1.   '_mgs' which targets mgc srpc settings. Treat it as
684          *      as a special file system name.
685          * 2.   target is a device which can be fsname-MDTXXXX or
686          *      fsname-OSTXXXX. This can be verified by the function
687          *      server_name2fsname.
688          * 3.   If both above conditions are not meet then the target
689          *      is a actual filesystem.
690          */
691         if (server_name2fsname(target, fsname, NULL))
692                 strlcpy(fsname, target, sizeof(target));
693
694         rc = sptlrpc_parse_rule(param, &rule);
695         if (rc)
696                 return rc;
697
698         return __sptlrpc_process_config(target, fsname, &rule, NULL);
699 }
700 EXPORT_SYMBOL(sptlrpc_process_config);
701
702 static int logname2fsname(const char *logname, char *buf, int buflen)
703 {
704         char   *ptr;
705         int     len;
706
707         ptr = strrchr(logname, '-');
708         if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
709                 CERROR("%s is not a sptlrpc config log\n", logname);
710                 return -EINVAL;
711         }
712
713         len = min((int) (ptr - logname), buflen - 1);
714
715         memcpy(buf, logname, len);
716         buf[len] = '\0';
717         return 0;
718 }
719
720 void sptlrpc_conf_log_update_begin(const char *logname)
721 {
722         struct sptlrpc_conf *conf;
723         char                 fsname[16];
724
725         if (logname2fsname(logname, fsname, sizeof(fsname)))
726                 return;
727
728         mutex_lock(&sptlrpc_conf_lock);
729
730         conf = sptlrpc_conf_get(fsname, 0);
731         if (conf) {
732                 if (conf->sc_local) {
733                         LASSERT(conf->sc_updated == 0);
734                         sptlrpc_conf_free_rsets(conf);
735                 }
736                 conf->sc_modified = 0;
737         }
738
739         mutex_unlock(&sptlrpc_conf_lock);
740 }
741 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
742
743 /**
744  * mark a config log has been updated
745  */
746 void sptlrpc_conf_log_update_end(const char *logname)
747 {
748         struct sptlrpc_conf *conf;
749         char                 fsname[16];
750
751         if (logname2fsname(logname, fsname, sizeof(fsname)))
752                 return;
753
754         mutex_lock(&sptlrpc_conf_lock);
755
756         conf = sptlrpc_conf_get(fsname, 0);
757         if (conf) {
758                 /*
759                  * if original state is not updated, make sure the
760                  * modified counter > 0 to enforce updating local copy.
761                  */
762                 if (conf->sc_updated == 0)
763                         conf->sc_modified++;
764
765                 conf->sc_updated = 1;
766         }
767
768         mutex_unlock(&sptlrpc_conf_lock);
769 }
770 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
771
772 void sptlrpc_conf_log_start(const char *logname)
773 {
774         char                 fsname[16];
775
776         if (logname2fsname(logname, fsname, sizeof(fsname)))
777                 return;
778
779         mutex_lock(&sptlrpc_conf_lock);
780         sptlrpc_conf_get(fsname, 1);
781         mutex_unlock(&sptlrpc_conf_lock);
782 }
783 EXPORT_SYMBOL(sptlrpc_conf_log_start);
784
785 void sptlrpc_conf_log_stop(const char *logname)
786 {
787         struct sptlrpc_conf *conf;
788         char                 fsname[16];
789
790         if (logname2fsname(logname, fsname, sizeof(fsname)))
791                 return;
792
793         mutex_lock(&sptlrpc_conf_lock);
794         conf = sptlrpc_conf_get(fsname, 0);
795         if (conf)
796                 sptlrpc_conf_free(conf);
797         mutex_unlock(&sptlrpc_conf_lock);
798 }
799 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
800
801 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
802                                     enum lustre_sec_part from,
803                                     enum lustre_sec_part to,
804                                     unsigned int fl_udesc)
805 {
806         /*
807          * null flavor doesn't need to set any flavor, and in fact
808          * we'd better not do that because everybody share a single sec.
809          */
810         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
811                 return;
812
813         if (from == LUSTRE_SP_MDT) {
814                 /* MDT->MDT; MDT->OST */
815                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
816         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
817                 /* CLI->OST */
818                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
819         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
820                 /* CLI->MDT */
821                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
822                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
823         }
824
825         /* Some flavors use a single uid (0) context */
826         if (flvr_is_rootonly(sf->sf_rpc))
827                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
828
829         /* User descriptor might need to be cleared */
830         if (flvr_allows_user_desc(sf->sf_rpc) == 0)
831                 sf->sf_flags &= ~PTLRPC_SEC_FL_UDESC;
832 }
833
834 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
835                                 enum lustre_sec_part to,
836                                 struct obd_uuid *target,
837                                 lnet_nid_t nid,
838                                 struct sptlrpc_flavor *sf)
839 {
840         struct sptlrpc_conf     *conf;
841         struct sptlrpc_conf_tgt *conf_tgt;
842         char                     name[MTI_NAME_MAXLEN];
843         int                      len, rc = 0;
844
845         obd_uuid2fsname(name, target->uuid, sizeof(name));
846
847         mutex_lock(&sptlrpc_conf_lock);
848
849         conf = sptlrpc_conf_get(name, 0);
850         if (conf == NULL)
851                 goto out;
852
853         /* convert uuid name (supposed end with _UUID) to target name */
854         len = strlen(target->uuid);
855         LASSERT(len > 5);
856         memcpy(name, target->uuid, len - 5);
857         name[len - 5] = '\0';
858
859         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
860         if (conf_tgt) {
861                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
862                                              from, to, nid, sf);
863                 if (rc)
864                         goto out;
865         }
866
867         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
868 out:
869         mutex_unlock(&sptlrpc_conf_lock);
870
871         if (rc == 0)
872                 get_default_flavor(sf);
873
874         flavor_set_flags(sf, from, to, 1);
875 }
876
877 /**
878  * called by target devices, determine the expected flavor from
879  * certain peer (from, nid).
880  */
881 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
882                                   enum lustre_sec_part from,
883                                   lnet_nid_t nid,
884                                   struct sptlrpc_flavor *sf)
885 {
886         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
887                 get_default_flavor(sf);
888 }
889
890 #define SEC_ADAPT_DELAY         (10)
891
892 /**
893  * called by client devices, notify the sptlrpc config has changed and
894  * do import_sec_adapt later.
895  */
896 void sptlrpc_conf_client_adapt(struct obd_device *obd)
897 {
898         struct obd_import  *imp;
899         int rc;
900         ENTRY;
901
902         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
903                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
904                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0 ||
905                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0);
906         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
907
908         /* serialize with connect/disconnect import */
909         with_imp_locked_nested(obd, imp, rc, OBD_CLI_SEM_MDCOSC) {
910                 write_lock(&imp->imp_sec_lock);
911                 if (imp->imp_sec)
912                         imp->imp_sec_expire = ktime_get_real_seconds() +
913                                 SEC_ADAPT_DELAY;
914                 write_unlock(&imp->imp_sec_lock);
915         }
916
917         EXIT;
918 }
919 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
920
921 /**
922  * called by target devices, extract sptlrpc rules which applies to
923  * this target, to be used for future rpc flavor checking.
924  */
925 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
926                                   struct sptlrpc_rule_set *rset)
927 {
928         struct sptlrpc_conf *conf;
929         struct sptlrpc_conf_tgt *conf_tgt;
930         enum lustre_sec_part sp_dst;
931         char fsname[MTI_NAME_MAXLEN];
932         int rc = 0;
933         ENTRY;
934
935         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
936                 sp_dst = LUSTRE_SP_MDT;
937         } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
938                 sp_dst = LUSTRE_SP_OST;
939         } else {
940                 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
941                 RETURN(-EINVAL);
942         }
943
944         obd_uuid2fsname(fsname, obd->obd_uuid.uuid, sizeof(fsname));
945
946         mutex_lock(&sptlrpc_conf_lock);
947         conf = sptlrpc_conf_get(fsname, 0);
948         if (conf == NULL) {
949                 CERROR("missing sptlrpc config log\n");
950                 rc = -EFAULT;
951         } else {
952                 /* extract rule set for this target */
953                 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
954
955                 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
956                                       conf_tgt ? &conf_tgt->sct_rset : NULL,
957                                       LUSTRE_SP_ANY, sp_dst, rset);
958         }
959         mutex_unlock(&sptlrpc_conf_lock);
960
961         RETURN(rc);
962 }
963
964 int  sptlrpc_conf_init(void)
965 {
966         mutex_init(&sptlrpc_conf_lock);
967         return 0;
968 }
969
970 void sptlrpc_conf_fini(void)
971 {
972         struct sptlrpc_conf  *conf, *conf_next;
973
974         mutex_lock(&sptlrpc_conf_lock);
975         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
976                 sptlrpc_conf_free(conf);
977         LASSERT(list_empty(&sptlrpc_confs));
978         mutex_unlock(&sptlrpc_conf_lock);
979 }