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