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