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