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