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