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