Whamcloud - gitweb
6812d82e3789fc2cc5d08bbb3b3b0b47d62f7da3
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include <libcfs/libcfs.h>
40 #include <linux/crypto.h>
41 #include <linux/key.h>
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre_net.h>
47 #include <lustre_import.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <lustre_dlm.h>
51 #include <lustre_param.h>
52 #include <lustre_sec.h>
53
54 #include "ptlrpc_internal.h"
55
56 const char *sptlrpc_part2name(enum lustre_sec_part part)
57 {
58         switch (part) {
59         case LUSTRE_SP_CLI:
60                 return "cli";
61         case LUSTRE_SP_MDT:
62                 return "mdt";
63         case LUSTRE_SP_OST:
64                 return "ost";
65         case LUSTRE_SP_MGC:
66                 return "mgc";
67         case LUSTRE_SP_MGS:
68                 return "mgs";
69         case LUSTRE_SP_ANY:
70                 return "any";
71         default:
72                 return "err";
73         }
74 }
75 EXPORT_SYMBOL(sptlrpc_part2name);
76
77 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
78 {
79         const char *type = obd->obd_type->typ_name;
80
81         if (!strcmp(type, LUSTRE_MDT_NAME))
82                 return LUSTRE_SP_MDT;
83         if (!strcmp(type, LUSTRE_OST_NAME))
84                 return LUSTRE_SP_OST;
85         if (!strcmp(type, LUSTRE_MGS_NAME))
86                 return LUSTRE_SP_MGS;
87
88         CERROR("unknown target %p(%s)\n", obd, type);
89         return LUSTRE_SP_ANY;
90 }
91
92 /****************************************
93  * user supplied flavor string parsing  *
94  ****************************************/
95
96 /*
97  * format: <base_flavor>[-<bulk_type:alg_spec>]
98  */
99 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
100 {
101         char            buf[32];
102         char           *bulk, *alg;
103
104         memset(flvr, 0, sizeof(*flvr));
105
106         if (str == NULL || str[0] == '\0') {
107                 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
108                 return 0;
109         }
110
111         strlcpy(buf, str, sizeof(buf));
112
113         bulk = strchr(buf, '-');
114         if (bulk)
115                 *bulk++ = '\0';
116
117         flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
118         if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
119                 goto err_out;
120
121         /*
122          * currently only base flavor "plain" can have bulk specification.
123          */
124         if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
125                 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
126                 if (bulk) {
127                         /*
128                          * format: plain-hash:<hash_alg>
129                          */
130                         alg = strchr(bulk, ':');
131                         if (alg == NULL)
132                                 goto err_out;
133                         *alg++ = '\0';
134
135                         if (strcmp(bulk, "hash"))
136                                 goto err_out;
137
138                         flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
139                         if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
140                                 goto err_out;
141                 }
142
143                 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
144                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
145                 else
146                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
147         } else {
148                 if (bulk)
149                         goto err_out;
150         }
151
152         flvr->sf_flags = 0;
153         return 0;
154
155 err_out:
156         CERROR("invalid flavor string: %s\n", str);
157         return -EINVAL;
158 }
159 EXPORT_SYMBOL(sptlrpc_parse_flavor);
160
161 /****************************************
162  * configure rules                      *
163  ****************************************/
164
165 static void get_default_flavor(struct sptlrpc_flavor *sf)
166 {
167         memset(sf, 0, sizeof(*sf));
168
169         sf->sf_rpc = SPTLRPC_FLVR_NULL;
170         sf->sf_flags = 0;
171 }
172
173 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
174 {
175         rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
176         rule->sr_from = LUSTRE_SP_ANY;
177         rule->sr_to = LUSTRE_SP_ANY;
178         rule->sr_padding = 0;
179
180         get_default_flavor(&rule->sr_flvr);
181 }
182
183 /*
184  * format: network[.direction]=flavor
185  */
186 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
187 {
188         char           *flavor, *dir;
189         int             rc;
190
191         sptlrpc_rule_init(rule);
192
193         flavor = strchr(param, '=');
194         if (flavor == NULL) {
195                 CERROR("invalid param, no '='\n");
196                 RETURN(-EINVAL);
197         }
198         *flavor++ = '\0';
199
200         dir = strchr(param, '.');
201         if (dir)
202                 *dir++ = '\0';
203
204         /* 1.1 network */
205         if (strcmp(param, "default")) {
206                 rule->sr_netid = libcfs_str2net(param);
207                 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
208                         CERROR("invalid network name: %s\n", param);
209                         RETURN(-EINVAL);
210                 }
211         }
212
213         /* 1.2 direction */
214         if (dir) {
215                 if (!strcmp(dir, "mdt2ost")) {
216                         rule->sr_from = LUSTRE_SP_MDT;
217                         rule->sr_to = LUSTRE_SP_OST;
218                 } else if (!strcmp(dir, "mdt2mdt")) {
219                         rule->sr_from = LUSTRE_SP_MDT;
220                         rule->sr_to = LUSTRE_SP_MDT;
221                 } else if (!strcmp(dir, "cli2ost")) {
222                         rule->sr_from = LUSTRE_SP_CLI;
223                         rule->sr_to = LUSTRE_SP_OST;
224                 } else if (!strcmp(dir, "cli2mdt")) {
225                         rule->sr_from = LUSTRE_SP_CLI;
226                         rule->sr_to = LUSTRE_SP_MDT;
227                 } else {
228                         CERROR("invalid rule dir segment: %s\n", dir);
229                         RETURN(-EINVAL);
230                 }
231         }
232
233         /* 2.1 flavor */
234         rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
235         if (rc)
236                 RETURN(-EINVAL);
237
238         RETURN(0);
239 }
240 EXPORT_SYMBOL(sptlrpc_parse_rule);
241
242 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
243 {
244         LASSERT(rset->srs_nslot ||
245                 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
246
247         if (rset->srs_nslot) {
248                 OBD_FREE(rset->srs_rules,
249                          rset->srs_nslot * sizeof(*rset->srs_rules));
250                 sptlrpc_rule_set_init(rset);
251         }
252 }
253 EXPORT_SYMBOL(sptlrpc_rule_set_free);
254
255 /*
256  * return 0 if the rule set could accomodate one more rule.
257  */
258 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
259 {
260         struct sptlrpc_rule *rules;
261         int nslot;
262
263         might_sleep();
264
265         if (rset->srs_nrule < rset->srs_nslot)
266                 return 0;
267
268         nslot = rset->srs_nslot + 8;
269
270         /* better use realloc() if available */
271         OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
272         if (rules == NULL)
273                 return -ENOMEM;
274
275         if (rset->srs_nrule) {
276                 LASSERT(rset->srs_nslot && rset->srs_rules);
277                 memcpy(rules, rset->srs_rules,
278                        rset->srs_nrule * sizeof(*rset->srs_rules));
279
280                 OBD_FREE(rset->srs_rules,
281                          rset->srs_nslot * sizeof(*rset->srs_rules));
282         }
283
284         rset->srs_rules = rules;
285         rset->srs_nslot = nslot;
286         return 0;
287 }
288
289 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
290 {
291         return (rule->sr_from != LUSTRE_SP_ANY ||
292                 rule->sr_to != LUSTRE_SP_ANY);
293 }
294 static inline int rule_spec_net(struct sptlrpc_rule *rule)
295 {
296         return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
297 }
298 static inline int rule_match_dir(struct sptlrpc_rule *r1,
299                                  struct sptlrpc_rule *r2)
300 {
301         return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
302 }
303 static inline int rule_match_net(struct sptlrpc_rule *r1,
304                                  struct sptlrpc_rule *r2)
305 {
306         return (r1->sr_netid == r2->sr_netid);
307 }
308
309 /*
310  * merge @rule into @rset.
311  * the @rset slots might be expanded.
312  */
313 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset, 
314                            struct sptlrpc_rule *rule)
315 {
316         struct sptlrpc_rule      *p = rset->srs_rules;
317         int                       spec_dir, spec_net;
318         int                       rc, n, match = 0;
319
320         might_sleep();
321
322         spec_net = rule_spec_net(rule);
323         spec_dir = rule_spec_dir(rule);
324
325         for (n = 0; n < rset->srs_nrule; n++) {
326                 p = &rset->srs_rules[n]; 
327
328                 /* test network match, if failed:
329                  * - spec rule: skip rules which is also spec rule match, until
330                  *   we hit a wild rule, which means no more chance
331                  * - wild rule: skip until reach the one which is also wild
332                  *   and matches
333                  */
334                 if (!rule_match_net(p, rule)) {
335                         if (spec_net) {
336                                 if (rule_spec_net(p))
337                                         continue;
338                                 else
339                                         break;
340                         } else {
341                                 continue;
342                         }
343                 }
344
345                 /* test dir match, same logic as net matching */
346                 if (!rule_match_dir(p, rule)) {
347                         if (spec_dir) {
348                                 if (rule_spec_dir(p))
349                                         continue;
350                                 else
351                                         break;
352                         } else {
353                                 continue;
354                         }
355                 }
356
357                 /* find a match */
358                 match = 1;
359                 break;
360         }
361
362         if (match) {
363                 LASSERT(n >= 0 && n < rset->srs_nrule);
364
365                 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
366                         /* remove this rule */
367                         if (n < rset->srs_nrule - 1)
368                                 memmove(&rset->srs_rules[n],
369                                         &rset->srs_rules[n + 1],
370                                         (rset->srs_nrule - n - 1) *
371                                         sizeof(*rule));
372                         rset->srs_nrule--;
373                 } else {
374                         /* override the rule */
375                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
376                 }
377         } else {
378                 LASSERT(n >= 0 && n <= rset->srs_nrule);
379
380                 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
381                         rc = sptlrpc_rule_set_expand(rset);
382                         if (rc)
383                                 return rc;
384
385                         if (n < rset->srs_nrule)
386                                 memmove(&rset->srs_rules[n + 1],
387                                         &rset->srs_rules[n],
388                                         (rset->srs_nrule - n) * sizeof(*rule));
389                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
390                         rset->srs_nrule++;
391                 } else {
392                         CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
393                 }
394         }
395
396         return 0;
397 }
398 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
399
400 /**
401  * given from/to/nid, determine a matching flavor in ruleset.
402  * return 1 if a match found, otherwise return 0.
403  */
404 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
405                             enum lustre_sec_part from,
406                             enum lustre_sec_part to,
407                             lnet_nid_t nid,
408                             struct sptlrpc_flavor *sf)
409 {
410         struct sptlrpc_rule    *r;
411         int                     n;
412
413         for (n = 0; n < rset->srs_nrule; n++) {
414                 r = &rset->srs_rules[n];
415
416                 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
417                     r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
418                     LNET_NIDNET(nid) != r->sr_netid)
419                         continue;
420
421                 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
422                     from != r->sr_from)
423                         continue;
424
425                 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
426                     to != r->sr_to)
427                         continue;
428
429                 *sf = r->sr_flvr;
430                 return 1;
431         }
432
433         return 0;
434 }
435
436 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
437 {
438         struct sptlrpc_rule *r;
439         int     n;
440
441         for (n = 0; n < rset->srs_nrule; n++) {
442                 r = &rset->srs_rules[n];
443                 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
444                        r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
445         }
446 }
447
448 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
449                                     struct sptlrpc_rule_set *tgt,
450                                     enum lustre_sec_part from,
451                                     enum lustre_sec_part to,
452                                     struct sptlrpc_rule_set *rset)
453 {
454         struct sptlrpc_rule_set *src[2] = { gen, tgt };
455         struct sptlrpc_rule     *rule;
456         int                      i, n, rc;
457
458         might_sleep();
459
460         /* merge general rules firstly, then target-specific rules */
461         for (i = 0; i < 2; i++) {
462                 if (src[i] == NULL)
463                         continue;
464
465                 for (n = 0; n < src[i]->srs_nrule; n++) {
466                         rule = &src[i]->srs_rules[n];
467
468                         if (from != LUSTRE_SP_ANY &&
469                             rule->sr_from != LUSTRE_SP_ANY &&
470                             rule->sr_from != from)
471                                 continue;
472                         if (to != LUSTRE_SP_ANY &&
473                             rule->sr_to != LUSTRE_SP_ANY &&
474                             rule->sr_to != to)
475                                 continue;
476
477                         rc = sptlrpc_rule_set_merge(rset, rule);
478                         if (rc) {
479                                 CERROR("can't merge: %d\n", rc);
480                                 return rc;
481                         }
482                 }
483         }
484
485         return 0;
486 }
487
488 /**********************************
489  * sptlrpc configuration support  *
490  **********************************/
491
492 struct sptlrpc_conf_tgt {
493         struct list_head              sct_list;
494         char                    sct_name[MAX_OBD_NAME];
495         struct sptlrpc_rule_set sct_rset;
496 };
497
498 struct sptlrpc_conf {
499         struct list_head        sc_list;
500         char                    sc_fsname[MTI_NAME_MAXLEN];
501         unsigned int            sc_modified;    /* modified during updating */
502         unsigned int            sc_updated:1,   /* updated copy from MGS */
503                                 sc_local:1;     /* local copy from target */
504         struct sptlrpc_rule_set sc_rset;        /* fs general rules */
505         struct list_head        sc_tgts;        /* target-specific rules */
506 };
507
508 static struct mutex sptlrpc_conf_lock;
509 static struct list_head sptlrpc_confs;
510
511 static inline int is_hex(char c)
512 {
513         return ((c >= '0' && c <= '9') ||
514                 (c >= 'a' && c <= 'f'));
515 }
516
517 static void target2fsname(const char *tgt, char *fsname, int buflen)
518 {
519         const char     *ptr;
520         int             len;
521
522         ptr = strrchr(tgt, '-');
523         if (ptr) {
524                 if ((strncmp(ptr, "-MDT", 4) != 0 &&
525                      strncmp(ptr, "-OST", 4) != 0) ||
526                     !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
527                     !is_hex(ptr[6]) || !is_hex(ptr[7]))
528                         ptr = NULL;
529         }
530
531         /* if we didn't find the pattern, treat the whole string as fsname */
532         if (ptr == NULL)
533                 len = strlen(tgt);
534         else
535                 len = ptr - tgt;
536
537         len = min(len, buflen - 1);
538         memcpy(fsname, tgt, len);
539         fsname[len] = '\0';
540 }
541
542 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
543 {
544         struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
545
546         sptlrpc_rule_set_free(&conf->sc_rset);
547
548         list_for_each_entry_safe(conf_tgt, conf_tgt_next,
549                                  &conf->sc_tgts, sct_list) {
550                 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
551                 list_del(&conf_tgt->sct_list);
552                 OBD_FREE_PTR(conf_tgt);
553         }
554         LASSERT(list_empty(&conf->sc_tgts));
555
556         conf->sc_updated = 0;
557         conf->sc_local = 0;
558 }
559
560 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
561 {
562         CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
563
564         sptlrpc_conf_free_rsets(conf);
565         list_del(&conf->sc_list);
566         OBD_FREE_PTR(conf);
567 }
568
569 static
570 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
571                                               const char *name,
572                                               int create)
573 {
574         struct sptlrpc_conf_tgt *conf_tgt;
575
576         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
577                 if (strcmp(conf_tgt->sct_name, name) == 0)
578                         return conf_tgt;
579         }
580
581         if (!create)
582                 return NULL;
583
584         OBD_ALLOC_PTR(conf_tgt);
585         if (conf_tgt) {
586                 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
587                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
588                 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
589         }
590
591         return conf_tgt;
592 }
593
594 static
595 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
596                                       int create)
597 {
598         struct sptlrpc_conf *conf;
599
600         list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
601                 if (strcmp(conf->sc_fsname, fsname) == 0)
602                         return conf;
603         }
604
605         if (!create)
606                 return NULL;
607
608         OBD_ALLOC_PTR(conf);
609         if (conf == NULL)
610                 return NULL;
611
612         if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
613             sizeof(conf->sc_fsname)) {
614                 OBD_FREE_PTR(conf);
615                 return NULL;
616         }
617         sptlrpc_rule_set_init(&conf->sc_rset);
618         INIT_LIST_HEAD(&conf->sc_tgts);
619         list_add(&conf->sc_list, &sptlrpc_confs);
620
621         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
622         return conf;
623 }
624
625 /**
626  * caller must hold conf_lock already.
627  */
628 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
629                                    const char *target,
630                                    struct sptlrpc_rule *rule)
631 {
632         struct sptlrpc_conf_tgt  *conf_tgt;
633         struct sptlrpc_rule_set  *rule_set;
634
635         /* fsname == target means general rules for the whole fs */
636         if (strcmp(conf->sc_fsname, target) == 0) {
637                 rule_set = &conf->sc_rset;
638         } else {
639                 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
640                 if (conf_tgt) {
641                         rule_set = &conf_tgt->sct_rset;
642                 } else {
643                         CERROR("out of memory, can't merge rule!\n");
644                         return -ENOMEM;
645                 }
646         }
647
648         return sptlrpc_rule_set_merge(rule_set, rule);
649 }
650
651 /**
652  * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
653  * find one through the target name in the record inside conf_lock;
654  * otherwise means caller already hold conf_lock.
655  */
656 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
657                                     struct sptlrpc_conf *conf)
658 {
659         char                   *target, *param;
660         char                    fsname[MTI_NAME_MAXLEN];
661         struct sptlrpc_rule     rule;
662         int                     rc;
663         ENTRY;
664
665         target = lustre_cfg_string(lcfg, 1);
666         if (target == NULL) {
667                 CERROR("missing target name\n");
668                 RETURN(-EINVAL);
669         }
670
671         param = lustre_cfg_string(lcfg, 2);
672         if (param == NULL) {
673                 CERROR("missing parameter\n");
674                 RETURN(-EINVAL);
675         }
676
677         CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
678
679         /* parse rule to make sure the format is correct */
680         if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
681                 CERROR("Invalid sptlrpc parameter: %s\n", param);
682                 RETURN(-EINVAL);
683         }
684         param += sizeof(PARAM_SRPC_FLVR) - 1;
685
686         rc = sptlrpc_parse_rule(param, &rule);
687         if (rc)
688                 RETURN(-EINVAL);
689
690         if (conf == NULL) {
691                 target2fsname(target, fsname, sizeof(fsname));
692
693                 mutex_lock(&sptlrpc_conf_lock);
694                 conf = sptlrpc_conf_get(fsname, 0);
695                 if (conf == NULL) {
696                         CERROR("can't find conf\n");
697                         rc = -ENOMEM;
698                 } else {
699                         rc = sptlrpc_conf_merge_rule(conf, target, &rule);
700                 }
701                 mutex_unlock(&sptlrpc_conf_lock);
702         } else {
703                 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
704                 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
705         }
706
707         if (rc == 0)
708                 conf->sc_modified++;
709
710         RETURN(rc);
711 }
712
713 int sptlrpc_process_config(struct lustre_cfg *lcfg)
714 {
715         return __sptlrpc_process_config(lcfg, NULL);
716 }
717 EXPORT_SYMBOL(sptlrpc_process_config);
718
719 static int logname2fsname(const char *logname, char *buf, int buflen)
720 {
721         char   *ptr;
722         int     len;
723
724         ptr = strrchr(logname, '-');
725         if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
726                 CERROR("%s is not a sptlrpc config log\n", logname);
727                 return -EINVAL;
728         }
729
730         len = min((int) (ptr - logname), buflen - 1);
731
732         memcpy(buf, logname, len);
733         buf[len] = '\0';
734         return 0;
735 }
736
737 void sptlrpc_conf_log_update_begin(const char *logname)
738 {
739         struct sptlrpc_conf *conf;
740         char                 fsname[16];
741
742         if (logname2fsname(logname, fsname, sizeof(fsname)))
743                 return;
744
745         mutex_lock(&sptlrpc_conf_lock);
746
747         conf = sptlrpc_conf_get(fsname, 0);
748         if (conf) {
749                 if (conf->sc_local) {
750                         LASSERT(conf->sc_updated == 0);
751                         sptlrpc_conf_free_rsets(conf);
752                 }
753                 conf->sc_modified = 0;
754         }
755
756         mutex_unlock(&sptlrpc_conf_lock);
757 }
758 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
759
760 /**
761  * mark a config log has been updated
762  */
763 void sptlrpc_conf_log_update_end(const char *logname)
764 {
765         struct sptlrpc_conf *conf;
766         char                 fsname[16];
767
768         if (logname2fsname(logname, fsname, sizeof(fsname)))
769                 return;
770
771         mutex_lock(&sptlrpc_conf_lock);
772
773         conf = sptlrpc_conf_get(fsname, 0);
774         if (conf) {
775                 /*
776                  * if original state is not updated, make sure the
777                  * modified counter > 0 to enforce updating local copy.
778                  */
779                 if (conf->sc_updated == 0)
780                         conf->sc_modified++;
781
782                 conf->sc_updated = 1;
783         }
784
785         mutex_unlock(&sptlrpc_conf_lock);
786 }
787 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
788
789 void sptlrpc_conf_log_start(const char *logname)
790 {
791         char                 fsname[16];
792
793         if (logname2fsname(logname, fsname, sizeof(fsname)))
794                 return;
795
796         mutex_lock(&sptlrpc_conf_lock);
797         sptlrpc_conf_get(fsname, 1);
798         mutex_unlock(&sptlrpc_conf_lock);
799 }
800 EXPORT_SYMBOL(sptlrpc_conf_log_start);
801
802 void sptlrpc_conf_log_stop(const char *logname)
803 {
804         struct sptlrpc_conf *conf;
805         char                 fsname[16];
806
807         if (logname2fsname(logname, fsname, sizeof(fsname)))
808                 return;
809
810         mutex_lock(&sptlrpc_conf_lock);
811         conf = sptlrpc_conf_get(fsname, 0);
812         if (conf)
813                 sptlrpc_conf_free(conf);
814         mutex_unlock(&sptlrpc_conf_lock);
815 }
816 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
817
818 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
819                                     enum lustre_sec_part from,
820                                     enum lustre_sec_part to,
821                                     unsigned int fl_udesc)
822 {
823         /*
824          * null flavor doesn't need to set any flavor, and in fact
825          * we'd better not do that because everybody share a single sec.
826          */
827         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
828                 return;
829
830         if (from == LUSTRE_SP_MDT) {
831                 /* MDT->MDT; MDT->OST */
832                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
833         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
834                 /* CLI->OST */
835                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
836         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
837                 /* CLI->MDT */
838                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
839                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
840         }
841 }
842
843 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
844                                 enum lustre_sec_part to,
845                                 struct obd_uuid *target,
846                                 lnet_nid_t nid,
847                                 struct sptlrpc_flavor *sf)
848 {
849         struct sptlrpc_conf     *conf;
850         struct sptlrpc_conf_tgt *conf_tgt;
851         char                     name[MTI_NAME_MAXLEN];
852         int                      len, rc = 0;
853
854         target2fsname(target->uuid, name, sizeof(name));
855
856         mutex_lock(&sptlrpc_conf_lock);
857
858         conf = sptlrpc_conf_get(name, 0);
859         if (conf == NULL)
860                 goto out;
861
862         /* convert uuid name (supposed end with _UUID) to target name */
863         len = strlen(target->uuid);
864         LASSERT(len > 5);
865         memcpy(name, target->uuid, len - 5);
866         name[len - 5] = '\0';
867
868         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
869         if (conf_tgt) {
870                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
871                                              from, to, nid, sf);
872                 if (rc)
873                         goto out;
874         }
875
876         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
877 out:
878         mutex_unlock(&sptlrpc_conf_lock);
879
880         if (rc == 0)
881                 get_default_flavor(sf);
882
883         flavor_set_flags(sf, from, to, 1);
884 }
885
886 /**
887  * called by target devices, determine the expected flavor from
888  * certain peer (from, nid).
889  */
890 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
891                                   enum lustre_sec_part from,
892                                   lnet_nid_t nid,
893                                   struct sptlrpc_flavor *sf)
894 {
895         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
896                 get_default_flavor(sf);
897 }
898
899 #define SEC_ADAPT_DELAY         (10)
900
901 /**
902  * called by client devices, notify the sptlrpc config has changed and
903  * do import_sec_adapt later.
904  */
905 void sptlrpc_conf_client_adapt(struct obd_device *obd)
906 {
907         struct obd_import  *imp;
908         ENTRY;
909
910         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
911                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
912         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
913
914         /* serialize with connect/disconnect import */
915         down_read(&obd->u.cli.cl_sem);
916
917         imp = obd->u.cli.cl_import;
918         if (imp) {
919                 spin_lock(&imp->imp_lock);
920                 if (imp->imp_sec)
921                         imp->imp_sec_expire = cfs_time_current_sec() +
922                                 SEC_ADAPT_DELAY;
923                 spin_unlock(&imp->imp_lock);
924         }
925
926         up_read(&obd->u.cli.cl_sem);
927         EXIT;
928 }
929 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
930
931
932 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
933 {
934         char     dirbuf[8];
935         char     net[LNET_NIDSTR_SIZE] = "default";
936         char    *ptr = buf;
937
938         if (r->sr_netid != LNET_NIDNET(LNET_NID_ANY))
939                 libcfs_net2str_r(r->sr_netid, net, sizeof(net));
940
941         if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
942                 dirbuf[0] = '\0';
943         else
944                 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
945                          sptlrpc_part2name(r->sr_from),
946                          sptlrpc_part2name(r->sr_to));
947
948         ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
949
950         sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
951         buf[buflen - 1] = '\0';
952 }
953
954 static int sptlrpc_record_rule_set(struct llog_handle *llh,
955                                    char *target,
956                                    struct sptlrpc_rule_set *rset)
957 {
958         struct llog_cfg_rec     *lcr;
959         struct lustre_cfg_bufs   bufs;
960         char                     param[48];
961         int                      i, rc;
962
963         for (i = 0; i < rset->srs_nrule; i++) {
964                 rule2string(&rset->srs_rules[i], param, sizeof(param));
965
966                 lustre_cfg_bufs_reset(&bufs, NULL);
967                 lustre_cfg_bufs_set_string(&bufs, 1, target);
968                 lustre_cfg_bufs_set_string(&bufs, 2, param);
969                 lcr = lustre_cfg_rec_new(LCFG_SPTLRPC_CONF, &bufs);
970                 if (lcr == NULL)
971                         return -ENOMEM;
972                 rc = llog_write(NULL, llh, &lcr->lcr_hdr, LLOG_NEXT_IDX);
973                 lustre_cfg_rec_free(lcr);
974                 if (rc)
975                         return rc;
976         }
977         return 0;
978 }
979
980 static int sptlrpc_record_rules(struct llog_handle *llh,
981                                 struct sptlrpc_conf *conf)
982 {
983         struct sptlrpc_conf_tgt *conf_tgt;
984
985         sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
986
987         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
988                 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
989                                         &conf_tgt->sct_rset);
990         }
991         return 0;
992 }
993
994 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
995 #define LOG_SPTLRPC     "sptlrpc"
996
997 static
998 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
999                                    struct sptlrpc_conf *conf)
1000 {
1001         struct llog_handle   *llh = NULL;
1002         struct llog_ctxt     *ctxt;
1003         struct lvfs_run_ctxt  saved;
1004         struct dentry        *dentry;
1005         int                   rc;
1006         ENTRY;
1007
1008         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1009         if (ctxt == NULL)
1010                 RETURN(-EINVAL);
1011
1012         push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1013
1014         dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd.dentry,
1015                                    strlen(MOUNT_CONFIGS_DIR));
1016         if (IS_ERR(dentry)) {
1017                 rc = PTR_ERR(dentry);
1018                 CERROR("cannot lookup %s directory: rc = %d\n",
1019                        MOUNT_CONFIGS_DIR, rc);
1020                 GOTO(out_ctx, rc);
1021         }
1022
1023         /* erase the old tmp log */
1024         rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1025         if (rc < 0 && rc != -ENOENT) {
1026                 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1027                        obd->obd_name, rc);
1028                 GOTO(out_dput, rc);
1029         }
1030
1031         /* write temporary log */
1032         rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1033         if (rc)
1034                 GOTO(out_dput, rc);
1035         rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1036         if (rc)
1037                 GOTO(out_close, rc);
1038
1039         rc = sptlrpc_record_rules(llh, conf);
1040
1041 out_close:
1042         llog_close(NULL, llh);
1043         if (rc == 0)
1044                 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1045                                    LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1046 out_dput:
1047         dput(dentry);
1048 out_ctx:
1049         pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1050         llog_ctxt_put(ctxt);
1051         CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1052                 obd->obd_name, rc);
1053         RETURN(rc);
1054 }
1055
1056 static int local_read_handler(const struct lu_env *env,
1057                               struct llog_handle *llh,
1058                               struct llog_rec_hdr *rec, void *data)
1059 {
1060         struct sptlrpc_conf  *conf = (struct sptlrpc_conf *) data;
1061         struct lustre_cfg    *lcfg = (struct lustre_cfg *)(rec + 1);
1062         int                   cfg_len, rc;
1063         ENTRY;
1064
1065         if (rec->lrh_type != OBD_CFG_REC) {
1066                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1067                 RETURN(-EINVAL);
1068         }
1069
1070         cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1071                   sizeof(struct llog_rec_tail);
1072
1073         rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1074         if (rc) {
1075                 CERROR("Insane cfg\n");
1076                 RETURN(rc);
1077         }
1078
1079         if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1080                 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1081                 RETURN(-EINVAL);
1082         }
1083
1084         RETURN(__sptlrpc_process_config(lcfg, conf));
1085 }
1086
1087 static
1088 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1089                                    struct sptlrpc_conf *conf)
1090 {
1091         struct llog_handle    *llh = NULL;
1092         struct llog_ctxt      *ctxt;
1093         struct lvfs_run_ctxt   saved;
1094         int                    rc;
1095         ENTRY;
1096
1097         LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1098
1099         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1100         if (ctxt == NULL) {
1101                 CERROR("missing llog context\n");
1102                 RETURN(-EINVAL);
1103         }
1104
1105         push_ctxt(&saved, &obd->obd_lvfs_ctxt);
1106
1107         rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1108         if (rc < 0) {
1109                 if (rc == -ENOENT)
1110                         rc = 0;
1111                 GOTO(out_pop, rc);
1112         }
1113
1114         rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1115         if (rc)
1116                 GOTO(out_close, rc);
1117
1118         if (llog_get_size(llh) <= 1) {
1119                 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1120                 GOTO(out_close, rc = 0);
1121         }
1122
1123         rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1124
1125         if (rc == 0) {
1126                 conf->sc_local = 1;
1127         } else {
1128                 sptlrpc_conf_free_rsets(conf);
1129         }
1130
1131 out_close:
1132         llog_close(NULL, llh);
1133 out_pop:
1134         pop_ctxt(&saved, &obd->obd_lvfs_ctxt);
1135         llog_ctxt_put(ctxt);
1136         CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1137                 obd->obd_name, rc);
1138         RETURN(rc);
1139 }
1140
1141
1142 /**
1143  * called by target devices, extract sptlrpc rules which applies to
1144  * this target, to be used for future rpc flavor checking.
1145  */
1146 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1147                                   struct sptlrpc_rule_set *rset,
1148                                   int initial)
1149 {
1150         struct sptlrpc_conf      *conf;
1151         struct sptlrpc_conf_tgt  *conf_tgt;
1152         enum lustre_sec_part      sp_dst;
1153         char                      fsname[MTI_NAME_MAXLEN];
1154         int                       rc = 0;
1155         ENTRY;
1156
1157         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1158                 sp_dst = LUSTRE_SP_MDT;
1159         } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1160                 sp_dst = LUSTRE_SP_OST;
1161         } else {
1162                 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1163                 RETURN(-EINVAL);
1164         }
1165         CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1166
1167         target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1168
1169         mutex_lock(&sptlrpc_conf_lock);
1170
1171         conf = sptlrpc_conf_get(fsname, 0);
1172         if (conf == NULL) {
1173                 CERROR("missing sptlrpc config log\n");
1174                 GOTO(out, rc);
1175         }
1176
1177         if (conf->sc_updated  == 0) {
1178                 /*
1179                  * always read from local copy. here another option is
1180                  * if we already have a local copy (read from another
1181                  * target device hosted on the same node) we simply use that.
1182                  */
1183                 if (conf->sc_local)
1184                         sptlrpc_conf_free_rsets(conf);
1185
1186                 sptlrpc_target_local_read_conf(obd, conf);
1187         } else {
1188                 LASSERT(conf->sc_local == 0);
1189
1190                 /* write a local copy */
1191                 if (initial || conf->sc_modified)
1192                         sptlrpc_target_local_copy_conf(obd, conf);
1193                 else
1194                         CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1195         }
1196
1197         /* extract rule set for this target */
1198         conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1199
1200         rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1201                                       conf_tgt ? &conf_tgt->sct_rset: NULL,
1202                                       LUSTRE_SP_ANY, sp_dst, rset);
1203 out:
1204         mutex_unlock(&sptlrpc_conf_lock);
1205         RETURN(rc);
1206 }
1207
1208 int  sptlrpc_conf_init(void)
1209 {
1210         INIT_LIST_HEAD(&sptlrpc_confs);
1211         mutex_init(&sptlrpc_conf_lock);
1212         return 0;
1213 }
1214
1215 void sptlrpc_conf_fini(void)
1216 {
1217         struct sptlrpc_conf  *conf, *conf_next;
1218
1219         mutex_lock(&sptlrpc_conf_lock);
1220         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
1221                 sptlrpc_conf_free(conf);
1222         LASSERT(list_empty(&sptlrpc_confs));
1223         mutex_unlock(&sptlrpc_conf_lock);
1224 }