]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/ptlrpc/sec_config.c
Merge tag 'dt-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[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
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 && conf->sc_local) {
749                 LASSERT(conf->sc_updated == 0);
750                 sptlrpc_conf_free_rsets(conf);
751         }
752         conf->sc_modified = 0;
753
754         mutex_unlock(&sptlrpc_conf_lock);
755 }
756 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
757
758 /**
759  * mark a config log has been updated
760  */
761 void sptlrpc_conf_log_update_end(const char *logname)
762 {
763         struct sptlrpc_conf *conf;
764         char             fsname[16];
765
766         if (logname2fsname(logname, fsname, sizeof(fsname)))
767                 return;
768
769         mutex_lock(&sptlrpc_conf_lock);
770
771         conf = sptlrpc_conf_get(fsname, 0);
772         if (conf) {
773                 /*
774                  * if original state is not updated, make sure the
775                  * modified counter > 0 to enforce updating local copy.
776                  */
777                 if (conf->sc_updated == 0)
778                         conf->sc_modified++;
779
780                 conf->sc_updated = 1;
781         }
782
783         mutex_unlock(&sptlrpc_conf_lock);
784 }
785 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
786
787 void sptlrpc_conf_log_start(const char *logname)
788 {
789         char             fsname[16];
790
791         if (logname2fsname(logname, fsname, sizeof(fsname)))
792                 return;
793
794         mutex_lock(&sptlrpc_conf_lock);
795         sptlrpc_conf_get(fsname, 1);
796         mutex_unlock(&sptlrpc_conf_lock);
797 }
798 EXPORT_SYMBOL(sptlrpc_conf_log_start);
799
800 void sptlrpc_conf_log_stop(const char *logname)
801 {
802         struct sptlrpc_conf *conf;
803         char             fsname[16];
804
805         if (logname2fsname(logname, fsname, sizeof(fsname)))
806                 return;
807
808         mutex_lock(&sptlrpc_conf_lock);
809         conf = sptlrpc_conf_get(fsname, 0);
810         if (conf)
811                 sptlrpc_conf_free(conf);
812         mutex_unlock(&sptlrpc_conf_lock);
813 }
814 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
815
816 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
817                                     enum lustre_sec_part from,
818                                     enum lustre_sec_part to,
819                                     unsigned int fl_udesc)
820 {
821         /*
822          * null flavor doesn't need to set any flavor, and in fact
823          * we'd better not do that because everybody share a single sec.
824          */
825         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
826                 return;
827
828         if (from == LUSTRE_SP_MDT) {
829                 /* MDT->MDT; MDT->OST */
830                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
831         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
832                 /* CLI->OST */
833                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
834         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
835                 /* CLI->MDT */
836                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
837                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
838         }
839 }
840
841 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
842                                 enum lustre_sec_part to,
843                                 struct obd_uuid *target,
844                                 lnet_nid_t nid,
845                                 struct sptlrpc_flavor *sf)
846 {
847         struct sptlrpc_conf     *conf;
848         struct sptlrpc_conf_tgt *conf_tgt;
849         char                 name[MTI_NAME_MAXLEN];
850         int                   len, rc = 0;
851
852         target2fsname(target->uuid, name, sizeof(name));
853
854         mutex_lock(&sptlrpc_conf_lock);
855
856         conf = sptlrpc_conf_get(name, 0);
857         if (conf == NULL)
858                 goto out;
859
860         /* convert uuid name (supposed end with _UUID) to target name */
861         len = strlen(target->uuid);
862         LASSERT(len > 5);
863         memcpy(name, target->uuid, len - 5);
864         name[len - 5] = '\0';
865
866         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
867         if (conf_tgt) {
868                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
869                                              from, to, nid, sf);
870                 if (rc)
871                         goto out;
872         }
873
874         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
875 out:
876         mutex_unlock(&sptlrpc_conf_lock);
877
878         if (rc == 0)
879                 get_default_flavor(sf);
880
881         flavor_set_flags(sf, from, to, 1);
882 }
883
884 /**
885  * called by target devices, determine the expected flavor from
886  * certain peer (from, nid).
887  */
888 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
889                                   enum lustre_sec_part from,
890                                   lnet_nid_t nid,
891                                   struct sptlrpc_flavor *sf)
892 {
893         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
894                 get_default_flavor(sf);
895 }
896 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
897
898 #define SEC_ADAPT_DELAY  (10)
899
900 /**
901  * called by client devices, notify the sptlrpc config has changed and
902  * do import_sec_adapt later.
903  */
904 void sptlrpc_conf_client_adapt(struct obd_device *obd)
905 {
906         struct obd_import  *imp;
907
908         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
909                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) ==0);
910         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
911
912         /* serialize with connect/disconnect import */
913         down_read(&obd->u.cli.cl_sem);
914
915         imp = obd->u.cli.cl_import;
916         if (imp) {
917                 spin_lock(&imp->imp_lock);
918                 if (imp->imp_sec)
919                         imp->imp_sec_expire = cfs_time_current_sec() +
920                                 SEC_ADAPT_DELAY;
921                 spin_unlock(&imp->imp_lock);
922         }
923
924         up_read(&obd->u.cli.cl_sem);
925 }
926 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
927
928
929 static void rule2string(struct sptlrpc_rule *r, char *buf, int buflen)
930 {
931         char    dirbuf[8];
932         char   *net;
933         char   *ptr = buf;
934
935         if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
936                 net = "default";
937         else
938                 net = libcfs_net2str(r->sr_netid);
939
940         if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
941                 dirbuf[0] = '\0';
942         else
943                 snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
944                          sptlrpc_part2name(r->sr_from),
945                          sptlrpc_part2name(r->sr_to));
946
947         ptr += snprintf(buf, buflen, "srpc.flavor.%s%s=", net, dirbuf);
948
949         sptlrpc_flavor2name(&r->sr_flvr, ptr, buflen - (ptr - buf));
950         buf[buflen - 1] = '\0';
951 }
952
953 static int sptlrpc_record_rule_set(struct llog_handle *llh,
954                                    char *target,
955                                    struct sptlrpc_rule_set *rset)
956 {
957         struct lustre_cfg_bufs  bufs;
958         struct lustre_cfg      *lcfg;
959         struct llog_rec_hdr     rec;
960         int                  buflen;
961         char                param[48];
962         int                  i, rc;
963
964         for (i = 0; i < rset->srs_nrule; i++) {
965                 rule2string(&rset->srs_rules[i], param, sizeof(param));
966
967                 lustre_cfg_bufs_reset(&bufs, NULL);
968                 lustre_cfg_bufs_set_string(&bufs, 1, target);
969                 lustre_cfg_bufs_set_string(&bufs, 2, param);
970                 lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &bufs);
971                 LASSERT(lcfg);
972
973                 buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
974                                         lcfg->lcfg_buflens);
975                 rec.lrh_len = llog_data_len(buflen);
976                 rec.lrh_type = OBD_CFG_REC;
977                 rc = llog_write(NULL, llh, &rec, NULL, 0, (void *)lcfg, -1);
978                 if (rc)
979                         CERROR("failed to write a rec: rc = %d\n", rc);
980                 lustre_cfg_free(lcfg);
981         }
982         return 0;
983 }
984
985 static int sptlrpc_record_rules(struct llog_handle *llh,
986                                 struct sptlrpc_conf *conf)
987 {
988         struct sptlrpc_conf_tgt *conf_tgt;
989
990         sptlrpc_record_rule_set(llh, conf->sc_fsname, &conf->sc_rset);
991
992         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
993                 sptlrpc_record_rule_set(llh, conf_tgt->sct_name,
994                                         &conf_tgt->sct_rset);
995         }
996         return 0;
997 }
998
999 #define LOG_SPTLRPC_TMP "sptlrpc.tmp"
1000 #define LOG_SPTLRPC     "sptlrpc"
1001
1002 static
1003 int sptlrpc_target_local_copy_conf(struct obd_device *obd,
1004                                    struct sptlrpc_conf *conf)
1005 {
1006         struct llog_handle   *llh = NULL;
1007         struct llog_ctxt     *ctxt;
1008         struct lvfs_run_ctxt  saved;
1009         struct dentry   *dentry;
1010         int                rc;
1011
1012         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1013         if (ctxt == NULL)
1014                 return -EINVAL;
1015
1016         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1017
1018         dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
1019                                    strlen(MOUNT_CONFIGS_DIR));
1020         if (IS_ERR(dentry)) {
1021                 rc = PTR_ERR(dentry);
1022                 CERROR("cannot lookup %s directory: rc = %d\n",
1023                        MOUNT_CONFIGS_DIR, rc);
1024                 GOTO(out_ctx, rc);
1025         }
1026
1027         /* erase the old tmp log */
1028         rc = llog_erase(NULL, ctxt, NULL, LOG_SPTLRPC_TMP);
1029         if (rc < 0 && rc != -ENOENT) {
1030                 CERROR("%s: cannot erase temporary sptlrpc log: rc = %d\n",
1031                        obd->obd_name, rc);
1032                 GOTO(out_dput, rc);
1033         }
1034
1035         /* write temporary log */
1036         rc = llog_open_create(NULL, ctxt, &llh, NULL, LOG_SPTLRPC_TMP);
1037         if (rc)
1038                 GOTO(out_dput, rc);
1039         rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1040         if (rc)
1041                 GOTO(out_close, rc);
1042
1043         rc = sptlrpc_record_rules(llh, conf);
1044
1045 out_close:
1046         llog_close(NULL, llh);
1047         if (rc == 0)
1048                 rc = lustre_rename(dentry, obd->obd_lvfs_ctxt.pwdmnt,
1049                                    LOG_SPTLRPC_TMP, LOG_SPTLRPC);
1050 out_dput:
1051         l_dput(dentry);
1052 out_ctx:
1053         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1054         llog_ctxt_put(ctxt);
1055         CDEBUG(D_SEC, "target %s: write local sptlrpc conf: rc = %d\n",
1056                obd->obd_name, rc);
1057         return rc;
1058 }
1059
1060 static int local_read_handler(const struct lu_env *env,
1061                               struct llog_handle *llh,
1062                               struct llog_rec_hdr *rec, void *data)
1063 {
1064         struct sptlrpc_conf  *conf = (struct sptlrpc_conf *) data;
1065         struct lustre_cfg    *lcfg = (struct lustre_cfg *)(rec + 1);
1066         int                cfg_len, rc;
1067
1068         if (rec->lrh_type != OBD_CFG_REC) {
1069                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1070                 return -EINVAL;
1071         }
1072
1073         cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
1074                   sizeof(struct llog_rec_tail);
1075
1076         rc = lustre_cfg_sanity_check(lcfg, cfg_len);
1077         if (rc) {
1078                 CERROR("Insane cfg\n");
1079                 return rc;
1080         }
1081
1082         if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
1083                 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
1084                 return -EINVAL;
1085         }
1086
1087         return __sptlrpc_process_config(lcfg, conf);
1088 }
1089
1090 static
1091 int sptlrpc_target_local_read_conf(struct obd_device *obd,
1092                                    struct sptlrpc_conf *conf)
1093 {
1094         struct llog_handle    *llh = NULL;
1095         struct llog_ctxt      *ctxt;
1096         struct lvfs_run_ctxt   saved;
1097         int                 rc;
1098
1099         LASSERT(conf->sc_updated == 0 && conf->sc_local == 0);
1100
1101         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1102         if (ctxt == NULL) {
1103                 CERROR("missing llog context\n");
1104                 return -EINVAL;
1105         }
1106
1107         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1108
1109         rc = llog_open(NULL, ctxt, &llh, NULL, LOG_SPTLRPC, LLOG_OPEN_EXISTS);
1110         if (rc < 0) {
1111                 if (rc == -ENOENT)
1112                         rc = 0;
1113                 GOTO(out_pop, rc);
1114         }
1115
1116         rc = llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1117         if (rc)
1118                 GOTO(out_close, rc);
1119
1120         if (llog_get_size(llh) <= 1) {
1121                 CDEBUG(D_SEC, "no local sptlrpc copy found\n");
1122                 GOTO(out_close, rc = 0);
1123         }
1124
1125         rc = llog_process(NULL, llh, local_read_handler, (void *)conf, NULL);
1126
1127         if (rc == 0) {
1128                 conf->sc_local = 1;
1129         } else {
1130                 sptlrpc_conf_free_rsets(conf);
1131         }
1132
1133 out_close:
1134         llog_close(NULL, llh);
1135 out_pop:
1136         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1137         llog_ctxt_put(ctxt);
1138         CDEBUG(D_SEC, "target %s: read local sptlrpc conf: rc = %d\n",
1139                obd->obd_name, rc);
1140         return rc;
1141 }
1142
1143
1144 /**
1145  * called by target devices, extract sptlrpc rules which applies to
1146  * this target, to be used for future rpc flavor checking.
1147  */
1148 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
1149                                   struct sptlrpc_rule_set *rset,
1150                                   int initial)
1151 {
1152         struct sptlrpc_conf      *conf;
1153         struct sptlrpc_conf_tgt  *conf_tgt;
1154         enum lustre_sec_part      sp_dst;
1155         char                  fsname[MTI_NAME_MAXLEN];
1156         int                    rc = 0;
1157
1158         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
1159                 sp_dst = LUSTRE_SP_MDT;
1160         } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
1161                 sp_dst = LUSTRE_SP_OST;
1162         } else {
1163                 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
1164                 return -EINVAL;
1165         }
1166         CDEBUG(D_SEC, "get rules for target %s\n", obd->obd_uuid.uuid);
1167
1168         target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
1169
1170         mutex_lock(&sptlrpc_conf_lock);
1171
1172         conf = sptlrpc_conf_get(fsname, 0);
1173         if (conf == NULL) {
1174                 CERROR("missing sptlrpc config log\n");
1175                 GOTO(out, rc);
1176         }
1177
1178         if (conf->sc_updated  == 0) {
1179                 /*
1180                  * always read from local copy. here another option is
1181                  * if we already have a local copy (read from another
1182                  * target device hosted on the same node) we simply use that.
1183                  */
1184                 if (conf->sc_local)
1185                         sptlrpc_conf_free_rsets(conf);
1186
1187                 sptlrpc_target_local_read_conf(obd, conf);
1188         } else {
1189                 LASSERT(conf->sc_local == 0);
1190
1191                 /* write a local copy */
1192                 if (initial || conf->sc_modified)
1193                         sptlrpc_target_local_copy_conf(obd, conf);
1194                 else
1195                         CDEBUG(D_SEC, "unchanged, skip updating local copy\n");
1196         }
1197
1198         /* extract rule set for this target */
1199         conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
1200
1201         rc = sptlrpc_rule_set_extract(&conf->sc_rset,
1202                                       conf_tgt ? &conf_tgt->sct_rset: NULL,
1203                                       LUSTRE_SP_ANY, sp_dst, rset);
1204 out:
1205         mutex_unlock(&sptlrpc_conf_lock);
1206         return rc;
1207 }
1208 EXPORT_SYMBOL(sptlrpc_conf_target_get_rules);
1209
1210 int  sptlrpc_conf_init(void)
1211 {
1212         mutex_init(&sptlrpc_conf_lock);
1213         return 0;
1214 }
1215
1216 void sptlrpc_conf_fini(void)
1217 {
1218         struct sptlrpc_conf  *conf, *conf_next;
1219
1220         mutex_lock(&sptlrpc_conf_lock);
1221         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
1222                 sptlrpc_conf_free(conf);
1223         }
1224         LASSERT(list_empty(&sptlrpc_confs));
1225         mutex_unlock(&sptlrpc_conf_lock);
1226 }