Whamcloud - gitweb
LU-3285 merge: 'dom' branch merging
[fs/lustre-release.git] / lustre / obdclass / obd_config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_config.c
33  *
34  * Config API
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <linux/string.h>
40
41 #include <llog_swab.h>
42 #include <lprocfs_status.h>
43 #include <lustre_disk.h>
44 #include <uapi/linux/lustre/lustre_ioctl.h>
45 #include <lustre_log.h>
46 #include <uapi/linux/lustre/lustre_param.h>
47 #include <obd_class.h>
48
49 #include "llog_internal.h"
50
51 static struct cfs_hash_ops uuid_hash_ops;
52 static struct cfs_hash_ops nid_hash_ops;
53 static struct cfs_hash_ops nid_stat_hash_ops;
54 static struct cfs_hash_ops gen_hash_ops;
55
56 /*********** string parsing utils *********/
57
58 /* returns 0 if we find this key in the buffer, else 1 */
59 int class_find_param(char *buf, char *key, char **valp)
60 {
61         char *ptr;
62
63         if (!buf)
64                 return 1;
65
66         if ((ptr = strstr(buf, key)) == NULL)
67                 return 1;
68
69         if (valp)
70                 *valp = ptr + strlen(key);
71
72         return 0;
73 }
74 EXPORT_SYMBOL(class_find_param);
75
76 /**
77  * Check whether the proc parameter \a param is an old parameter or not from
78  * the array \a ptr which contains the mapping from old parameters to new ones.
79  * If it's an old one, then return the pointer to the cfg_interop_param struc-
80  * ture which contains both the old and new parameters.
81  *
82  * \param param                 proc parameter
83  * \param ptr                   an array which contains the mapping from
84  *                              old parameters to new ones
85  *
86  * \retval valid-pointer        pointer to the cfg_interop_param structure
87  *                              which contains the old and new parameters
88  * \retval NULL                 \a param or \a ptr is NULL,
89  *                              or \a param is not an old parameter
90  */
91 struct cfg_interop_param *class_find_old_param(const char *param,
92                                                struct cfg_interop_param *ptr)
93 {
94         char *value = NULL;
95         int   name_len = 0;
96
97         if (param == NULL || ptr == NULL)
98                 RETURN(NULL);
99
100         value = strchr(param, '=');
101         if (value == NULL)
102                 name_len = strlen(param);
103         else
104                 name_len = value - param;
105
106         while (ptr->old_param != NULL) {
107                 if (strncmp(param, ptr->old_param, name_len) == 0 &&
108                     name_len == strlen(ptr->old_param))
109                         RETURN(ptr);
110                 ptr++;
111         }
112
113         RETURN(NULL);
114 }
115 EXPORT_SYMBOL(class_find_old_param);
116
117 /**
118  * Finds a parameter in \a params and copies it to \a copy.
119  *
120  * Leading spaces are skipped. Next space or end of string is the
121  * parameter terminator with the exception that spaces inside single or double
122  * quotes get included into a parameter. The parameter is copied into \a copy
123  * which has to be allocated big enough by a caller, quotes are stripped in
124  * the copy and the copy is terminated by 0.
125  *
126  * On return \a params is set to next parameter or to NULL if last
127  * parameter is returned.
128  *
129  * \retval 0 if parameter is returned in \a copy
130  * \retval 1 otherwise
131  * \retval -EINVAL if unbalanced quota is found
132  */
133 int class_get_next_param(char **params, char *copy)
134 {
135         char *q1, *q2, *str;
136         int len;
137
138         str = *params;
139         while (*str == ' ')
140                 str++;
141
142         if (*str == '\0') {
143                 *params = NULL;
144                 return 1;
145         }
146
147         while (1) {
148                 q1 = strpbrk(str, " '\"");
149                 if (q1 == NULL) {
150                         len = strlen(str);
151                         memcpy(copy, str, len);
152                         copy[len] = '\0';
153                         *params = NULL;
154                         return 0;
155                 }
156                 len = q1 - str;
157                 if (*q1 == ' ') {
158                         memcpy(copy, str, len);
159                         copy[len] = '\0';
160                         *params = str + len;
161                         return 0;
162                 }
163
164                 memcpy(copy, str, len);
165                 copy += len;
166
167                 /* search for the matching closing quote */
168                 str = q1 + 1;
169                 q2 = strchr(str, *q1);
170                 if (q2 == NULL) {
171                         CERROR("Unbalanced quota in parameters: \"%s\"\n",
172                                *params);
173                         return -EINVAL;
174                 }
175                 len = q2 - str;
176                 memcpy(copy, str, len);
177                 copy += len;
178                 str = q2 + 1;
179         }
180         return 1;
181 }
182 EXPORT_SYMBOL(class_get_next_param);
183
184 /* returns 0 if this is the first key in the buffer, else 1.
185    valp points to first char after key. */
186 int class_match_param(char *buf, const char *key, char **valp)
187 {
188         if (!buf)
189                 return 1;
190
191         if (memcmp(buf, key, strlen(key)) != 0)
192                 return 1;
193
194         if (valp)
195                 *valp = buf + strlen(key);
196
197         return 0;
198 }
199 EXPORT_SYMBOL(class_match_param);
200
201 static int parse_nid(char *buf, void *value, int quiet)
202 {
203         lnet_nid_t *nid = (lnet_nid_t *)value;
204
205         *nid = libcfs_str2nid(buf);
206         if (*nid != LNET_NID_ANY)
207                 return 0;
208
209         if (!quiet)
210                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
211         return -EINVAL;
212 }
213
214 static int parse_net(char *buf, void *value)
215 {
216         __u32 *net = (__u32 *)value;
217
218         *net = libcfs_str2net(buf);
219         CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
220         return 0;
221 }
222
223 enum {
224         CLASS_PARSE_NID = 1,
225         CLASS_PARSE_NET,
226 };
227
228 /* 0 is good nid,
229    1 not found
230    < 0 error
231    endh is set to next separator */
232 static int class_parse_value(char *buf, int opc, void *value, char **endh,
233                              int quiet)
234 {
235         char *endp;
236         char  tmp;
237         int   rc = 0;
238
239         if (!buf)
240                 return 1;
241         while (*buf == ',' || *buf == ':')
242                 buf++;
243         if (*buf == ' ' || *buf == '/' || *buf == '\0')
244                 return 1;
245
246         /* nid separators or end of nids */
247         endp = strpbrk(buf, ",: /");
248         if (endp == NULL)
249                 endp = buf + strlen(buf);
250
251         tmp = *endp;
252         *endp = '\0';
253         switch (opc) {
254         default:
255                 LBUG();
256         case CLASS_PARSE_NID:
257                 rc = parse_nid(buf, value, quiet);
258                 break;
259         case CLASS_PARSE_NET:
260                 rc = parse_net(buf, value);
261                 break;
262         }
263         *endp = tmp;
264         if (rc != 0)
265                 return rc;
266         if (endh)
267                 *endh = endp;
268         return 0;
269 }
270
271 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
272 {
273         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
274 }
275 EXPORT_SYMBOL(class_parse_nid);
276
277 int class_parse_nid_quiet(char *buf, lnet_nid_t *nid, char **endh)
278 {
279         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
280 }
281 EXPORT_SYMBOL(class_parse_nid_quiet);
282
283 int class_parse_net(char *buf, __u32 *net, char **endh)
284 {
285         return class_parse_value(buf, CLASS_PARSE_NET, (void *)net, endh, 0);
286 }
287
288 /* 1 param contains key and match
289  * 0 param contains key and not match
290  * -1 param does not contain key
291  */
292 int class_match_nid(char *buf, char *key, lnet_nid_t nid)
293 {
294         lnet_nid_t tmp;
295         int   rc = -1;
296
297         while (class_find_param(buf, key, &buf) == 0) {
298                 /* please restrict to the nids pertaining to
299                  * the specified nids */
300                 while (class_parse_nid(buf, &tmp, &buf) == 0) {
301                         if (tmp == nid)
302                                 return 1;
303                 }
304                 rc = 0;
305         }
306         return rc;
307 }
308
309 int class_match_net(char *buf, char *key, __u32 net)
310 {
311         __u32 tmp;
312         int   rc = -1;
313
314         while (class_find_param(buf, key, &buf) == 0) {
315                 /* please restrict to the nids pertaining to
316                  * the specified networks */
317                 while (class_parse_net(buf, &tmp, &buf) == 0) {
318                         if (tmp == net)
319                                 return 1;
320                 }
321                 rc = 0;
322         }
323         return rc;
324 }
325
326 char *lustre_cfg_string(struct lustre_cfg *lcfg, u32 index)
327 {
328         char *s;
329
330         if (!lcfg->lcfg_buflens[index])
331                 return NULL;
332
333         s = lustre_cfg_buf(lcfg, index);
334         if (!s)
335                 return NULL;
336
337         /*
338          * make sure it's NULL terminated, even if this kills a char
339          * of data.  Try to use the padding first though.
340          */
341         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
342                 size_t last = ALIGN(lcfg->lcfg_buflens[index], 8) - 1;
343                 char lost;
344
345                 /* Use the smaller value */
346                 if (last > lcfg->lcfg_buflens[index])
347                         last = lcfg->lcfg_buflens[index];
348
349                 lost = s[last];
350                 s[last] = '\0';
351                 if (lost != '\0') {
352                         CWARN("Truncated buf %d to '%s' (lost '%c'...)\n",
353                               index, s, lost);
354                 }
355         }
356         return s;
357 }
358 EXPORT_SYMBOL(lustre_cfg_string);
359
360 /********************** class fns **********************/
361
362 /**
363  * Create a new obd device and set the type, name and uuid.  If successful,
364  * the new device can be accessed by either name or uuid.
365  */
366 int class_attach(struct lustre_cfg *lcfg)
367 {
368         struct obd_export *exp;
369         struct obd_device *obd = NULL;
370         char *typename, *name, *uuid;
371         int rc, len;
372         ENTRY;
373
374         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
375                 CERROR("No type passed!\n");
376                 RETURN(-EINVAL);
377         }
378         typename = lustre_cfg_string(lcfg, 1);
379
380         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
381                 CERROR("No name passed!\n");
382                 RETURN(-EINVAL);
383         }
384         name = lustre_cfg_string(lcfg, 0);
385         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
386                 CERROR("No UUID passed!\n");
387                 RETURN(-EINVAL);
388         }
389
390         uuid = lustre_cfg_string(lcfg, 2);
391         len = strlen(uuid);
392         if (len >= sizeof(obd->obd_uuid)) {
393                 CERROR("%s: uuid must be < %d bytes long\n",
394                        name, (int)sizeof(obd->obd_uuid));
395                 RETURN(-EINVAL);
396         }
397
398         obd = class_newdev(typename, name, uuid);
399         if (IS_ERR(obd)) {
400                 /* Already exists or out of obds */
401                 rc = PTR_ERR(obd);
402                 CERROR("Cannot create device %s of type %s : %d\n",
403                        name, typename, rc);
404                 RETURN(rc);
405         }
406         LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
407                  name, typename);
408         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
409                  "obd %p obd_magic %08X != %08X\n",
410                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
411         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
412                  "%p obd_name %s != %s\n", obd, obd->obd_name, name);
413
414         exp = class_new_export_self(obd, &obd->obd_uuid);
415         if (IS_ERR(exp)) {
416                 /* force free */
417                 GOTO(out, rc = PTR_ERR(exp));
418                 RETURN(PTR_ERR(exp));
419         }
420
421         obd->obd_self_export = exp;
422         list_del_init(&exp->exp_obd_chain_timed);
423         class_export_put(exp);
424
425         rc = class_register_device(obd);
426         if (rc != 0)
427                 GOTO(out, rc);
428
429         obd->obd_attached = 1;
430         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
431                obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
432         RETURN(0);
433 out:
434         class_decref(obd, "newdev", obd);
435         class_free_dev(obd);
436
437         RETURN(rc);
438 }
439 EXPORT_SYMBOL(class_attach);
440
441 /** Create hashes, self-export, and call type-specific setup.
442  * Setup is effectively the "start this obd" call.
443  */
444 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
445 {
446         int err = 0;
447         ENTRY;
448
449         LASSERT(obd != NULL);
450         LASSERTF(obd == class_num2obd(obd->obd_minor),
451                  "obd %p != obd_devs[%d] %p\n",
452                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
453         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
454                  "obd %p obd_magic %08x != %08x\n",
455                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
456
457         /* have we attached a type to this device? */
458         if (!obd->obd_attached) {
459                 CERROR("Device %d not attached\n", obd->obd_minor);
460                 RETURN(-ENODEV);
461         }
462
463         if (obd->obd_set_up) {
464                 CERROR("Device %d already setup (type %s)\n",
465                        obd->obd_minor, obd->obd_type->typ_name);
466                 RETURN(-EEXIST);
467         }
468
469         /* is someone else setting us up right now? (attach inits spinlock) */
470         spin_lock(&obd->obd_dev_lock);
471         if (obd->obd_starting) {
472                 spin_unlock(&obd->obd_dev_lock);
473                 CERROR("Device %d setup in progress (type %s)\n",
474                        obd->obd_minor, obd->obd_type->typ_name);
475                 RETURN(-EEXIST);
476         }
477         /* just leave this on forever.  I can't use obd_set_up here because
478            other fns check that status, and we're not actually set up yet. */
479         obd->obd_starting = 1;
480         obd->obd_uuid_hash = NULL;
481         obd->obd_nid_hash = NULL;
482         obd->obd_nid_stats_hash = NULL;
483         obd->obd_gen_hash = NULL;
484         spin_unlock(&obd->obd_dev_lock);
485
486         /* create an uuid-export lustre hash */
487         obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
488                                              HASH_UUID_CUR_BITS,
489                                              HASH_UUID_MAX_BITS,
490                                              HASH_UUID_BKT_BITS, 0,
491                                              CFS_HASH_MIN_THETA,
492                                              CFS_HASH_MAX_THETA,
493                                              &uuid_hash_ops, CFS_HASH_DEFAULT);
494         if (!obd->obd_uuid_hash)
495                 GOTO(err_exit, err = -ENOMEM);
496
497         /* create a nid-export lustre hash */
498         obd->obd_nid_hash = cfs_hash_create("NID_HASH",
499                                             HASH_NID_CUR_BITS,
500                                             HASH_NID_MAX_BITS,
501                                             HASH_NID_BKT_BITS, 0,
502                                             CFS_HASH_MIN_THETA,
503                                             CFS_HASH_MAX_THETA,
504                                             &nid_hash_ops, CFS_HASH_DEFAULT);
505         if (!obd->obd_nid_hash)
506                 GOTO(err_exit, err = -ENOMEM);
507
508         /* create a nid-stats lustre hash */
509         obd->obd_nid_stats_hash = cfs_hash_create("NID_STATS",
510                                                   HASH_NID_STATS_CUR_BITS,
511                                                   HASH_NID_STATS_MAX_BITS,
512                                                   HASH_NID_STATS_BKT_BITS, 0,
513                                                   CFS_HASH_MIN_THETA,
514                                                   CFS_HASH_MAX_THETA,
515                                                   &nid_stat_hash_ops, CFS_HASH_DEFAULT);
516         if (!obd->obd_nid_stats_hash)
517                 GOTO(err_exit, err = -ENOMEM);
518
519         /* create a client_generation-export lustre hash */
520         obd->obd_gen_hash = cfs_hash_create("UUID_HASH",
521                                             HASH_GEN_CUR_BITS,
522                                             HASH_GEN_MAX_BITS,
523                                             HASH_GEN_BKT_BITS, 0,
524                                             CFS_HASH_MIN_THETA,
525                                             CFS_HASH_MAX_THETA,
526                                             &gen_hash_ops, CFS_HASH_DEFAULT);
527         if (!obd->obd_gen_hash)
528                 GOTO(err_exit, err = -ENOMEM);
529
530         err = obd_setup(obd, lcfg);
531         if (err)
532                 GOTO(err_exit, err);
533
534         obd->obd_set_up = 1;
535
536         spin_lock(&obd->obd_dev_lock);
537         /* cleanup drops this */
538         class_incref(obd, "setup", obd);
539         spin_unlock(&obd->obd_dev_lock);
540
541         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
542                obd->obd_name, obd->obd_uuid.uuid);
543
544         RETURN(0);
545 err_exit:
546         if (obd->obd_uuid_hash) {
547                 cfs_hash_putref(obd->obd_uuid_hash);
548                 obd->obd_uuid_hash = NULL;
549         }
550         if (obd->obd_nid_hash) {
551                 cfs_hash_putref(obd->obd_nid_hash);
552                 obd->obd_nid_hash = NULL;
553         }
554         if (obd->obd_nid_stats_hash) {
555                 cfs_hash_putref(obd->obd_nid_stats_hash);
556                 obd->obd_nid_stats_hash = NULL;
557         }
558         if (obd->obd_gen_hash) {
559                 cfs_hash_putref(obd->obd_gen_hash);
560                 obd->obd_gen_hash = NULL;
561         }
562         obd->obd_starting = 0;
563         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
564         return err;
565 }
566 EXPORT_SYMBOL(class_setup);
567
568 /** We have finished using this obd and are ready to destroy it.
569  * There can be no more references to this obd.
570  */
571 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
572 {
573         ENTRY;
574
575         if (obd->obd_set_up) {
576                 CERROR("OBD device %d still set up\n", obd->obd_minor);
577                 RETURN(-EBUSY);
578         }
579
580         spin_lock(&obd->obd_dev_lock);
581         if (!obd->obd_attached) {
582                 spin_unlock(&obd->obd_dev_lock);
583                 CERROR("OBD device %d not attached\n", obd->obd_minor);
584                 RETURN(-ENODEV);
585         }
586         obd->obd_attached = 0;
587         spin_unlock(&obd->obd_dev_lock);
588
589         /* cleanup in progress. we don't like to find this device after now */
590         class_unregister_device(obd);
591
592         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
593                obd->obd_name, obd->obd_uuid.uuid);
594
595         class_decref(obd, "newdev", obd);
596
597         RETURN(0);
598 }
599 EXPORT_SYMBOL(class_detach);
600
601 /** Start shutting down the obd.  There may be in-progess ops when
602  * this is called.  We tell them to start shutting down with a call
603  * to class_disconnect_exports().
604  */
605 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
606 {
607         int err = 0;
608         char *flag;
609         ENTRY;
610
611         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
612
613         if (!obd->obd_set_up) {
614                 CERROR("Device %d not setup\n", obd->obd_minor);
615                 RETURN(-ENODEV);
616         }
617
618         spin_lock(&obd->obd_dev_lock);
619         if (obd->obd_stopping) {
620                 spin_unlock(&obd->obd_dev_lock);
621                 CERROR("OBD %d already stopping\n", obd->obd_minor);
622                 RETURN(-ENODEV);
623         }
624         /* Leave this on forever */
625         obd->obd_stopping = 1;
626         /* function can't return error after that point, so clear setup flag
627          * as early as possible to avoid finding via obd_devs / hash */
628         obd->obd_set_up = 0;
629         spin_unlock(&obd->obd_dev_lock);
630
631         /* wait for already-arrived-connections to finish. */
632         while (obd->obd_conn_inprogress > 0)
633                 yield();
634         smp_rmb();
635
636         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
637                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
638                         switch (*flag) {
639                         case 'F':
640                                 obd->obd_force = 1;
641                                 break;
642                         case 'A':
643                                 LCONSOLE_WARN("Failing over %s\n",
644                                               obd->obd_name);
645                                 obd->obd_fail = 1;
646                                 obd->obd_no_transno = 1;
647                                 obd->obd_no_recov = 1;
648                                 if (OBP(obd, iocontrol)) {
649                                         obd_iocontrol(OBD_IOC_SYNC,
650                                                       obd->obd_self_export,
651                                                       0, NULL, NULL);
652                                 }
653                                 break;
654                         default:
655                                 CERROR("Unrecognised flag '%c'\n", *flag);
656                         }
657         }
658
659         LASSERT(obd->obd_self_export);
660
661         CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d/%d\n",
662                obd->obd_name, obd->obd_num_exports,
663                atomic_read(&obd->obd_refcount) - 2);
664         dump_exports(obd, 0, D_HA);
665         class_disconnect_exports(obd);
666
667         /* Precleanup, we must make sure all exports get destroyed. */
668         err = obd_precleanup(obd);
669         if (err)
670                 CERROR("Precleanup %s returned %d\n",
671                        obd->obd_name, err);
672
673         /* destroy an uuid-export hash body */
674         if (obd->obd_uuid_hash) {
675                 cfs_hash_putref(obd->obd_uuid_hash);
676                 obd->obd_uuid_hash = NULL;
677         }
678
679         /* destroy a nid-export hash body */
680         if (obd->obd_nid_hash) {
681                 cfs_hash_putref(obd->obd_nid_hash);
682                 obd->obd_nid_hash = NULL;
683         }
684
685         /* destroy a nid-stats hash body */
686         if (obd->obd_nid_stats_hash) {
687                 cfs_hash_putref(obd->obd_nid_stats_hash);
688                 obd->obd_nid_stats_hash = NULL;
689         }
690
691         /* destroy a client_generation-export hash body */
692         if (obd->obd_gen_hash) {
693                 cfs_hash_putref(obd->obd_gen_hash);
694                 obd->obd_gen_hash = NULL;
695         }
696
697         class_decref(obd, "setup", obd);
698         obd->obd_set_up = 0;
699
700         RETURN(0);
701 }
702
703 struct obd_device *class_incref(struct obd_device *obd,
704                                 const char *scope, const void *source)
705 {
706         lu_ref_add_atomic(&obd->obd_reference, scope, source);
707         atomic_inc(&obd->obd_refcount);
708         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
709                atomic_read(&obd->obd_refcount));
710
711         return obd;
712 }
713 EXPORT_SYMBOL(class_incref);
714
715 void class_decref(struct obd_device *obd, const char *scope, const void *source)
716 {
717         int last;
718
719         CDEBUG(D_INFO, "Decref %s (%p) now %d - %s\n", obd->obd_name, obd,
720                atomic_read(&obd->obd_refcount), scope);
721
722         LASSERT(obd->obd_num_exports >= 0);
723         last = atomic_dec_and_test(&obd->obd_refcount);
724         lu_ref_del(&obd->obd_reference, scope, source);
725
726         if (last) {
727                 struct obd_export *exp;
728
729                 LASSERT(!obd->obd_attached);
730                 /* All exports have been destroyed; there should
731                  * be no more in-progress ops by this point.*/
732                 exp = obd->obd_self_export;
733
734                 if (exp) {
735                         exp->exp_flags |= exp_flags_from_obd(obd);
736                         /*
737                          * note that we'll recurse into class_decref again
738                          * but it's not a problem because we was last user
739                          */
740                         class_unlink_export(exp);
741                 }
742         }
743 }
744 EXPORT_SYMBOL(class_decref);
745
746 /** Add a failover nid location.
747  * Client obd types contact server obd types using this nid list.
748  */
749 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
750 {
751         struct obd_import *imp;
752         struct obd_uuid uuid;
753         int rc;
754         ENTRY;
755
756         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
757             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
758                 CERROR("invalid conn_uuid\n");
759                 RETURN(-EINVAL);
760         }
761         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
762             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
763             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
764             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
765             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
766                 CERROR("can't add connection on non-client dev\n");
767                 RETURN(-EINVAL);
768         }
769
770         imp = obd->u.cli.cl_import;
771         if (!imp) {
772                 CERROR("try to add conn on immature client dev\n");
773                 RETURN(-EINVAL);
774         }
775
776         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
777         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
778
779         RETURN(rc);
780 }
781
782 /** Remove a failover nid location.
783  */
784 static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
785 {
786         struct obd_import *imp;
787         struct obd_uuid uuid;
788         int rc;
789         ENTRY;
790
791         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
792             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
793                 CERROR("invalid conn_uuid\n");
794                 RETURN(-EINVAL);
795         }
796         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
797             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
798                 CERROR("can't del connection on non-client dev\n");
799                 RETURN(-EINVAL);
800         }
801
802         imp = obd->u.cli.cl_import;
803         if (!imp) {
804                 CERROR("try to del conn on immature client dev\n");
805                 RETURN(-EINVAL);
806         }
807
808         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
809         rc = obd_del_conn(imp, &uuid);
810
811         RETURN(rc);
812 }
813
814 static LIST_HEAD(lustre_profile_list);
815 static DEFINE_SPINLOCK(lustre_profile_list_lock);
816
817 struct lustre_profile *class_get_profile(const char * prof)
818 {
819         struct lustre_profile *lprof;
820
821         ENTRY;
822         spin_lock(&lustre_profile_list_lock);
823         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
824                 if (!strcmp(lprof->lp_profile, prof)) {
825                         lprof->lp_refs++;
826                         spin_unlock(&lustre_profile_list_lock);
827                         RETURN(lprof);
828                 }
829         }
830         spin_unlock(&lustre_profile_list_lock);
831         RETURN(NULL);
832 }
833 EXPORT_SYMBOL(class_get_profile);
834
835 /** Create a named "profile".
836  * This defines the mdc and osc names to use for a client.
837  * This also is used to define the lov to be used by a mdt.
838  */
839 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
840                              int mdclen, char *mdc)
841 {
842         struct lustre_profile *lprof;
843         int err = 0;
844         ENTRY;
845
846         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
847
848         OBD_ALLOC(lprof, sizeof(*lprof));
849         if (lprof == NULL)
850                 RETURN(-ENOMEM);
851         INIT_LIST_HEAD(&lprof->lp_list);
852
853         LASSERT(proflen == (strlen(prof) + 1));
854         OBD_ALLOC(lprof->lp_profile, proflen);
855         if (lprof->lp_profile == NULL)
856                 GOTO(out, err = -ENOMEM);
857         memcpy(lprof->lp_profile, prof, proflen);
858
859         LASSERT(osclen == (strlen(osc) + 1));
860         OBD_ALLOC(lprof->lp_dt, osclen);
861         if (lprof->lp_dt == NULL)
862                 GOTO(out, err = -ENOMEM);
863         memcpy(lprof->lp_dt, osc, osclen);
864
865         if (mdclen > 0) {
866                 LASSERT(mdclen == (strlen(mdc) + 1));
867                 OBD_ALLOC(lprof->lp_md, mdclen);
868                 if (lprof->lp_md == NULL)
869                         GOTO(out, err = -ENOMEM);
870                 memcpy(lprof->lp_md, mdc, mdclen);
871         }
872
873         spin_lock(&lustre_profile_list_lock);
874         lprof->lp_refs = 1;
875         lprof->lp_list_deleted = false;
876
877         list_add(&lprof->lp_list, &lustre_profile_list);
878         spin_unlock(&lustre_profile_list_lock);
879         RETURN(err);
880
881 out:
882         if (lprof->lp_md)
883                 OBD_FREE(lprof->lp_md, mdclen);
884         if (lprof->lp_dt)
885                 OBD_FREE(lprof->lp_dt, osclen);
886         if (lprof->lp_profile)
887                 OBD_FREE(lprof->lp_profile, proflen);
888         OBD_FREE(lprof, sizeof(*lprof));
889         RETURN(err);
890 }
891
892 void class_del_profile(const char *prof)
893 {
894         struct lustre_profile *lprof;
895         ENTRY;
896
897         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
898
899         lprof = class_get_profile(prof);
900         if (lprof) {
901                 spin_lock(&lustre_profile_list_lock);
902                 /* because get profile increments the ref counter */
903                 lprof->lp_refs--;
904                 list_del(&lprof->lp_list);
905                 lprof->lp_list_deleted = true;
906                 spin_unlock(&lustre_profile_list_lock);
907
908                 class_put_profile(lprof);
909         }
910         EXIT;
911 }
912 EXPORT_SYMBOL(class_del_profile);
913
914 void class_put_profile(struct lustre_profile *lprof)
915 {
916         spin_lock(&lustre_profile_list_lock);
917         if ((--lprof->lp_refs) > 0) {
918                 LASSERT(lprof->lp_refs > 0);
919                 spin_unlock(&lustre_profile_list_lock);
920                 return;
921         }
922         spin_unlock(&lustre_profile_list_lock);
923
924         /* confirm not a negative number */
925         LASSERT(lprof->lp_refs == 0);
926
927         /* At least one class_del_profile/profiles must be called
928          * on the target profile or lustre_profile_list will corrupt */
929         LASSERT(lprof->lp_list_deleted);
930         OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
931         OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
932         if (lprof->lp_md != NULL)
933                 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
934         OBD_FREE(lprof, sizeof(*lprof));
935 }
936 EXPORT_SYMBOL(class_put_profile);
937
938 /* COMPAT_146 */
939 void class_del_profiles(void)
940 {
941         struct lustre_profile *lprof, *n;
942         ENTRY;
943
944         spin_lock(&lustre_profile_list_lock);
945         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
946                 list_del(&lprof->lp_list);
947                 lprof->lp_list_deleted = true;
948                 spin_unlock(&lustre_profile_list_lock);
949
950                 class_put_profile(lprof);
951
952                 spin_lock(&lustre_profile_list_lock);
953         }
954         spin_unlock(&lustre_profile_list_lock);
955         EXIT;
956 }
957 EXPORT_SYMBOL(class_del_profiles);
958
959 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
960 {
961         ENTRY;
962         if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
963                 at_min = val;
964         else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
965                 at_max = val;
966         else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
967                 at_extra = val;
968         else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
969                 at_early_margin = val;
970         else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
971                 at_history = val;
972         else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
973                 strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
974                         JOBSTATS_JOBID_VAR_MAX_LEN + 1);
975         else
976                 RETURN(-EINVAL);
977
978         CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
979         RETURN(0);
980 }
981
982
983 /* We can't call ll_process_config or lquota_process_config directly because
984  * it lives in a module that must be loaded after this one. */
985 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
986 static int (*quota_process_config)(struct lustre_cfg *lcfg) = NULL;
987
988 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
989 {
990         client_process_config = cpc;
991 }
992 EXPORT_SYMBOL(lustre_register_client_process_config);
993
994 /**
995  * Rename the proc parameter in \a cfg with a new name \a new_name.
996  *
997  * \param cfg      config structure which contains the proc parameter
998  * \param new_name new name of the proc parameter
999  *
1000  * \retval valid-pointer    pointer to the newly-allocated config structure
1001  *                          which contains the renamed proc parameter
1002  * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
1003  *                          not contain a proc parameter
1004  * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
1005  */
1006 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
1007                                      const char *new_name)
1008 {
1009         struct lustre_cfg_bufs  *bufs = NULL;
1010         struct lustre_cfg       *new_cfg = NULL;
1011         char                    *param = NULL;
1012         char                    *new_param = NULL;
1013         char                    *value = NULL;
1014         int                      name_len = 0;
1015         int                      new_len = 0;
1016         ENTRY;
1017
1018         if (!cfg || !new_name)
1019                 GOTO(out_nocfg, new_cfg = ERR_PTR(-EINVAL));
1020
1021         param = lustre_cfg_string(cfg, 1);
1022         if (!param)
1023                 GOTO(out_nocfg, new_cfg = ERR_PTR(-EINVAL));
1024
1025         value = strchr(param, '=');
1026         if (value == NULL)
1027                 name_len = strlen(param);
1028         else
1029                 name_len = value - param;
1030
1031         new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
1032
1033         OBD_ALLOC(new_param, new_len);
1034         if (!new_param)
1035                 GOTO(out_nocfg, new_cfg = ERR_PTR(-ENOMEM));
1036
1037         strcpy(new_param, new_name);
1038         if (value != NULL)
1039                 strcat(new_param, value);
1040
1041         OBD_ALLOC_PTR(bufs);
1042         if (!bufs)
1043                 GOTO(out_free_param, new_cfg = ERR_PTR(-ENOMEM));
1044
1045         lustre_cfg_bufs_reset(bufs, NULL);
1046         lustre_cfg_bufs_init(bufs, cfg);
1047         lustre_cfg_bufs_set_string(bufs, 1, new_param);
1048
1049         OBD_ALLOC(new_cfg, lustre_cfg_len(bufs->lcfg_bufcount,
1050                                           bufs->lcfg_buflen));
1051         if (!new_cfg)
1052                 GOTO(out_free_buf, new_cfg = ERR_PTR(-ENOMEM));
1053
1054         lustre_cfg_init(new_cfg, cfg->lcfg_command, bufs);
1055
1056         new_cfg->lcfg_num = cfg->lcfg_num;
1057         new_cfg->lcfg_flags = cfg->lcfg_flags;
1058         new_cfg->lcfg_nid = cfg->lcfg_nid;
1059         new_cfg->lcfg_nal = cfg->lcfg_nal;
1060 out_free_buf:
1061         OBD_FREE_PTR(bufs);
1062 out_free_param:
1063         OBD_FREE(new_param, new_len);
1064 out_nocfg:
1065         RETURN(new_cfg);
1066 }
1067 EXPORT_SYMBOL(lustre_cfg_rename);
1068
1069 static int process_param2_config(struct lustre_cfg *lcfg)
1070 {
1071         char *param = lustre_cfg_string(lcfg, 1);
1072         char *upcall = lustre_cfg_string(lcfg, 2);
1073         char *argv[] = {
1074                 [0] = "/usr/sbin/lctl",
1075                 [1] = "set_param",
1076                 [2] = param,
1077                 [3] = NULL
1078         };
1079         ktime_t start;
1080         ktime_t end;
1081         int             rc;
1082         ENTRY;
1083
1084         /* Add upcall processing here. Now only lctl is supported */
1085         if (strcmp(upcall, LCTL_UPCALL) != 0) {
1086                 CERROR("Unsupported upcall %s\n", upcall);
1087                 RETURN(-EINVAL);
1088         }
1089
1090         start = ktime_get();
1091         rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1092         end = ktime_get();
1093
1094         if (rc < 0) {
1095                 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1096                        "time %ldus\n", argv[0], argv[1], argv[2], rc,
1097                        (long)ktime_us_delta(end, start));
1098         } else {
1099                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1100                        argv[0], argv[1], argv[2],
1101                        (long)ktime_us_delta(end, start));
1102                        rc = 0;
1103         }
1104
1105         RETURN(rc);
1106 }
1107
1108 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1109 {
1110         quota_process_config = qpc;
1111 }
1112 EXPORT_SYMBOL(lustre_register_quota_process_config);
1113
1114 /** Process configuration commands given in lustre_cfg form.
1115  * These may come from direct calls (e.g. class_manual_cleanup)
1116  * or processing the config llog, or ioctl from lctl.
1117  */
1118 int class_process_config(struct lustre_cfg *lcfg)
1119 {
1120         struct obd_device *obd;
1121         int err;
1122
1123         LASSERT(lcfg && !IS_ERR(lcfg));
1124         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1125
1126         /* Commands that don't need a device */
1127         switch(lcfg->lcfg_command) {
1128         case LCFG_ATTACH: {
1129                 err = class_attach(lcfg);
1130                 GOTO(out, err);
1131         }
1132         case LCFG_ADD_UUID: {
1133                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx"
1134                        " (%s)\n", lustre_cfg_string(lcfg, 1),
1135                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1136
1137                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1138                 GOTO(out, err);
1139         }
1140         case LCFG_DEL_UUID: {
1141                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1142                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1143                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1144
1145                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1146                 GOTO(out, err);
1147         }
1148         case LCFG_MOUNTOPT: {
1149                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1150                        lustre_cfg_string(lcfg, 1),
1151                        lustre_cfg_string(lcfg, 2),
1152                        lustre_cfg_string(lcfg, 3));
1153                 /* set these mount options somewhere, so ll_fill_super
1154                  * can find them. */
1155                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1156                                         lustre_cfg_string(lcfg, 1),
1157                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1158                                         lustre_cfg_string(lcfg, 2),
1159                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1160                                         lustre_cfg_string(lcfg, 3));
1161                 GOTO(out, err);
1162         }
1163         case LCFG_DEL_MOUNTOPT: {
1164                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1165                        lustre_cfg_string(lcfg, 1));
1166                 class_del_profile(lustre_cfg_string(lcfg, 1));
1167                 GOTO(out, err = 0);
1168         }
1169         case LCFG_SET_TIMEOUT: {
1170                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1171                        obd_timeout, lcfg->lcfg_num);
1172                 obd_timeout = max(lcfg->lcfg_num, 1U);
1173                 obd_timeout_set = 1;
1174                 GOTO(out, err = 0);
1175         }
1176         case LCFG_SET_LDLM_TIMEOUT: {
1177                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1178                        ldlm_timeout, lcfg->lcfg_num);
1179                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1180                 if (ldlm_timeout >= obd_timeout)
1181                         ldlm_timeout = max(obd_timeout / 3, 1U);
1182                 ldlm_timeout_set = 1;
1183                 GOTO(out, err = 0);
1184         }
1185         case LCFG_SET_UPCALL: {
1186                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1187                 /* COMPAT_146 Don't fail on old configs */
1188                 GOTO(out, err = 0);
1189         }
1190         case LCFG_MARKER: {
1191                 struct cfg_marker *marker;
1192                 marker = lustre_cfg_buf(lcfg, 1);
1193                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1194                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1195                 GOTO(out, err = 0);
1196         }
1197         case LCFG_PARAM: {
1198                 char *tmp;
1199                 /* llite has no obd */
1200                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1201                                        PARAM_LLITE, NULL) == 0) &&
1202                     client_process_config) {
1203                         err = (*client_process_config)(lcfg);
1204                         GOTO(out, err);
1205                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1206                                               PARAM_SYS, &tmp) == 0)) {
1207                         /* Global param settings */
1208                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1209                         /*
1210                          * Client or server should not fail to mount if
1211                          * it hits an unknown configuration parameter.
1212                          */
1213                         if (err != 0)
1214                                 CWARN("Ignoring unknown param %s\n", tmp);
1215
1216                         GOTO(out, err = 0);
1217                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1218                                               PARAM_QUOTA, &tmp) == 0) &&
1219                            quota_process_config) {
1220                         err = (*quota_process_config)(lcfg);
1221                         GOTO(out, err);
1222                 }
1223
1224                 break;
1225         }
1226         case LCFG_SET_PARAM: {
1227                 err = process_param2_config(lcfg);
1228                 GOTO(out, err = 0);
1229         }
1230         }
1231         /* Commands that require a device */
1232         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1233         if (obd == NULL) {
1234                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1235                         CERROR("this lcfg command requires a device name\n");
1236                 else
1237                         CERROR("no device for: %s\n",
1238                                lustre_cfg_string(lcfg, 0));
1239
1240                 GOTO(out, err = -EINVAL);
1241         }
1242         switch(lcfg->lcfg_command) {
1243         case LCFG_SETUP: {
1244                 err = class_setup(obd, lcfg);
1245                 GOTO(out, err);
1246         }
1247         case LCFG_DETACH: {
1248                 err = class_detach(obd, lcfg);
1249                 GOTO(out, err = 0);
1250         }
1251         case LCFG_CLEANUP: {
1252                 err = class_cleanup(obd, lcfg);
1253                 GOTO(out, err = 0);
1254         }
1255         case LCFG_ADD_CONN: {
1256                 err = class_add_conn(obd, lcfg);
1257                 GOTO(out, err = 0);
1258         }
1259         case LCFG_DEL_CONN: {
1260                 err = class_del_conn(obd, lcfg);
1261                 GOTO(out, err = 0);
1262         }
1263         case LCFG_POOL_NEW: {
1264                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1265                 GOTO(out, err = 0);
1266         }
1267         case LCFG_POOL_ADD: {
1268                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1269                                    lustre_cfg_string(lcfg, 3));
1270                 GOTO(out, err = 0);
1271         }
1272         case LCFG_POOL_REM: {
1273                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1274                                    lustre_cfg_string(lcfg, 3));
1275                 GOTO(out, err = 0);
1276         }
1277         case LCFG_POOL_DEL: {
1278                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1279                 GOTO(out, err = 0);
1280         }
1281         /* Process config log ADD_MDC record twice to add MDC also to LOV
1282          * for Data-on-MDT:
1283          *
1284          * add 0:lustre-clilmv 1:lustre-MDT0000_UUID 2:0 3:1
1285          *     4:lustre-MDT0000-mdc_UUID
1286          */
1287         case LCFG_ADD_MDC: {
1288                 struct obd_device *lov_obd;
1289                 char *clilmv;
1290
1291                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1292                 if (err)
1293                         GOTO(out, err);
1294
1295                 /* make sure this is client LMV log entry */
1296                 clilmv = strstr(lustre_cfg_string(lcfg, 0), "clilmv");
1297                 if (!clilmv)
1298                         GOTO(out, err);
1299
1300                 /* replace 'lmv' with 'lov' name to address LOV device and
1301                  * process llog record to add MDC there. */
1302                 clilmv[4] = 'o';
1303                 lov_obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1304                 if (lov_obd == NULL) {
1305                         err = -ENOENT;
1306                         CERROR("%s: Cannot find LOV by %s name, rc = %d\n",
1307                                obd->obd_name, lustre_cfg_string(lcfg, 0), err);
1308                 } else {
1309                         err = obd_process_config(lov_obd, sizeof(*lcfg), lcfg);
1310                 }
1311                 /* restore 'lmv' name */
1312                 clilmv[4] = 'm';
1313                 GOTO(out, err);
1314         }
1315         default: {
1316                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1317                 GOTO(out, err);
1318
1319         }
1320         }
1321         EXIT;
1322 out:
1323         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1324                 CWARN("Ignoring error %d on optional command %#x\n", err,
1325                       lcfg->lcfg_command);
1326                 err = 0;
1327         }
1328         return err;
1329 }
1330 EXPORT_SYMBOL(class_process_config);
1331
1332 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1333                              struct lustre_cfg *lcfg, void *data)
1334 {
1335         struct lprocfs_vars *var;
1336         struct file fakefile;
1337         struct seq_file fake_seqfile;
1338         char *key, *sval;
1339         int i, keylen, vallen;
1340         int matched = 0, j = 0;
1341         int rc = 0;
1342         int skip = 0;
1343         ENTRY;
1344
1345         if (lcfg->lcfg_command != LCFG_PARAM) {
1346                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1347                 RETURN(-EINVAL);
1348         }
1349
1350         /* fake a seq file so that var->fops->write can work... */
1351         fakefile.private_data = &fake_seqfile;
1352         fake_seqfile.private = data;
1353         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1354            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1355            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1356         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1357                 key = lustre_cfg_buf(lcfg, i);
1358                 /* Strip off prefix */
1359                 if (class_match_param(key, prefix, &key))
1360                         /* If the prefix doesn't match, return error so we
1361                          * can pass it down the stack */
1362                         RETURN(-ENOSYS);
1363                 sval = strchr(key, '=');
1364                 if (!sval || *(sval + 1) == 0) {
1365                         CERROR("%s: can't parse param '%s' (missing '=')\n",
1366                                lustre_cfg_string(lcfg, 0),
1367                                lustre_cfg_string(lcfg, i));
1368                         /* rc = -EINVAL;        continue parsing other params */
1369                         continue;
1370                 }
1371                 keylen = sval - key;
1372                 sval++;
1373                 vallen = strlen(sval);
1374                 matched = 0;
1375                 j = 0;
1376                 /* Search proc entries */
1377                 while (lvars[j].name) {
1378                         var = &lvars[j];
1379                         if (class_match_param(key, var->name, NULL) == 0 &&
1380                             keylen == strlen(var->name)) {
1381                                 matched++;
1382                                 rc = -EROFS;
1383
1384                                 if (var->fops && var->fops->write) {
1385                                         mm_segment_t oldfs;
1386                                         oldfs = get_fs();
1387                                         set_fs(KERNEL_DS);
1388                                         rc = (var->fops->write)(&fakefile, sval,
1389                                                                 vallen, NULL);
1390                                         set_fs(oldfs);
1391                                 }
1392                                 break;
1393                         }
1394                         j++;
1395                 }
1396                 if (!matched) {
1397                         /* It was upgraded from old MDT/OST device,
1398                          * ignore the obsolete "sec_level" parameter. */
1399                         if (strncmp("sec_level", key, keylen) == 0)
1400                                 continue;
1401
1402                         CERROR("%s: unknown config parameter '%s'\n",
1403                                lustre_cfg_string(lcfg, 0),
1404                                lustre_cfg_string(lcfg, i));
1405                         /* rc = -EINVAL;        continue parsing other params */
1406                         skip++;
1407                 } else if (rc < 0) {
1408                         CERROR("%s: error writing parameter '%s': rc = %d\n",
1409                                lustre_cfg_string(lcfg, 0), key, rc);
1410                         rc = 0;
1411                 } else {
1412                         CDEBUG(D_CONFIG, "%s: set parameter '%s'\n",
1413                                lustre_cfg_string(lcfg, 0), key);
1414                 }
1415         }
1416
1417         if (rc > 0)
1418                 rc = 0;
1419         if (!rc && skip)
1420                 rc = skip;
1421         RETURN(rc);
1422 }
1423 EXPORT_SYMBOL(class_process_proc_param);
1424
1425 /*
1426  * Supplemental functions for config logs, it allocates lustre_cfg
1427  * buffers plus initialized llog record header at the beginning.
1428  */
1429 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1430 {
1431         struct llog_cfg_rec     *lcr;
1432         int                      reclen;
1433
1434         ENTRY;
1435
1436         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1437         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1438                  sizeof(struct llog_rec_tail);
1439
1440         OBD_ALLOC(lcr, reclen);
1441         if (lcr == NULL)
1442                 RETURN(NULL);
1443
1444         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1445
1446         lcr->lcr_hdr.lrh_len = reclen;
1447         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1448
1449         RETURN(lcr);
1450 }
1451 EXPORT_SYMBOL(lustre_cfg_rec_new);
1452
1453 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1454 {
1455         ENTRY;
1456         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1457         EXIT;
1458 }
1459 EXPORT_SYMBOL(lustre_cfg_rec_free);
1460
1461 /** Parse a configuration llog, doing various manipulations on them
1462  * for various reasons, (modifications for compatibility, skip obsolete
1463  * records, change uuids, etc), then class_process_config() resulting
1464  * net records.
1465  */
1466 int class_config_llog_handler(const struct lu_env *env,
1467                               struct llog_handle *handle,
1468                               struct llog_rec_hdr *rec, void *data)
1469 {
1470         struct config_llog_instance *cfg = data;
1471         int cfg_len = rec->lrh_len;
1472         char *cfg_buf = (char *) (rec + 1);
1473         int rc = 0;
1474         ENTRY;
1475
1476         /* class_config_dump_handler(handle, rec, data); */
1477
1478         switch (rec->lrh_type) {
1479         case OBD_CFG_REC: {
1480                 struct lustre_cfg *lcfg, *lcfg_new;
1481                 struct lustre_cfg_bufs bufs;
1482                 char *inst_name = NULL;
1483                 int inst_len = 0;
1484                 int swab = 0;
1485
1486                 lcfg = (struct lustre_cfg *)cfg_buf;
1487                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1488                         lustre_swab_lustre_cfg(lcfg);
1489                         swab = 1;
1490                 }
1491
1492                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1493                 if (rc)
1494                         GOTO(out, rc);
1495
1496                 /* Figure out config state info */
1497                 if (lcfg->lcfg_command == LCFG_MARKER) {
1498                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1499                         lustre_swab_cfg_marker(marker, swab,
1500                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1501                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1502                                cfg->cfg_flags, marker->cm_flags);
1503                         if (marker->cm_flags & CM_START) {
1504                                 /* all previous flags off */
1505                                 cfg->cfg_flags = CFG_F_MARKER;
1506                                 server_name2index(marker->cm_tgtname,
1507                                                   &cfg->cfg_lwp_idx, NULL);
1508                                 if (marker->cm_flags & CM_SKIP) {
1509                                         cfg->cfg_flags |= CFG_F_SKIP;
1510                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1511                                                marker->cm_step);
1512                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1513                                            (cfg->cfg_sb &&
1514                                            lustre_check_exclusion(cfg->cfg_sb,
1515                                                         marker->cm_tgtname))) {
1516                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1517                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1518                                                marker->cm_step);
1519                                 }
1520                         } else if (marker->cm_flags & CM_END) {
1521                                 cfg->cfg_flags = 0;
1522                         }
1523                 }
1524                 /* A config command without a start marker before it is
1525                    illegal (post 146) */
1526                 if (!(cfg->cfg_flags & CFG_F_COMPAT146) &&
1527                     !(cfg->cfg_flags & CFG_F_MARKER) &&
1528                     (lcfg->lcfg_command != LCFG_MARKER)) {
1529                         CWARN("Config not inside markers, ignoring! "
1530                               "(inst: %p, uuid: %s, flags: %#x)\n",
1531                                 cfg->cfg_instance,
1532                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1533                         cfg->cfg_flags |= CFG_F_SKIP;
1534                 }
1535                 if (cfg->cfg_flags & CFG_F_SKIP) {
1536                         CDEBUG(D_CONFIG, "skipping %#x\n",
1537                                cfg->cfg_flags);
1538                         rc = 0;
1539                         /* No processing! */
1540                         break;
1541                 }
1542
1543                 /*
1544                  * For interoperability between 1.8 and 2.0,
1545                  * rename "mds" obd device type to "mdt".
1546                  */
1547                 {
1548                         char *typename = lustre_cfg_string(lcfg, 1);
1549                         char *index = lustre_cfg_string(lcfg, 2);
1550
1551                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1552                             strcmp(typename, "mds") == 0)) {
1553                                 CWARN("For 1.8 interoperability, rename obd "
1554                                         "type from mds to mdt\n");
1555                                 typename[2] = 't';
1556                         }
1557                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1558                             strcmp(index, "type") == 0)) {
1559                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1560                                        "set this index to '0'\n");
1561                                 index[0] = '0';
1562                                 index[1] = 0;
1563                         }
1564                 }
1565
1566 #ifdef HAVE_SERVER_SUPPORT
1567                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1568                 {
1569                         char *typename = lustre_cfg_string(lcfg, 1);
1570
1571                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1572                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1573                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1574                                 CDEBUG(D_CONFIG,
1575                                        "For 2.x interoperability, rename obd "
1576                                        "type from lov to lod (%s)\n",
1577                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1578                                 strcpy(typename, LUSTRE_LOD_NAME);
1579                         }
1580                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1581                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1582                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1583                                 CDEBUG(D_CONFIG,
1584                                        "For 2.x interoperability, rename obd "
1585                                        "type from osc to osp (%s)\n",
1586                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1587                                 strcpy(typename, LUSTRE_OSP_NAME);
1588                         }
1589                 }
1590 #endif /* HAVE_SERVER_SUPPORT */
1591
1592                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1593                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1594                                lcfg->lcfg_command);
1595                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1596                                 /* Add inactive instead */
1597                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1598                 }
1599
1600                 lustre_cfg_bufs_reset(&bufs, NULL);
1601                 lustre_cfg_bufs_init(&bufs, lcfg);
1602
1603                 if (cfg->cfg_instance &&
1604                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1605                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1606                                    sizeof(cfg->cfg_instance) * 2 + 4;
1607                         OBD_ALLOC(inst_name, inst_len);
1608                         if (inst_name == NULL)
1609                                 GOTO(out, rc = -ENOMEM);
1610                         snprintf(inst_name, inst_len, "%s-%p",
1611                                 lustre_cfg_string(lcfg, 0),
1612                                 cfg->cfg_instance);
1613                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1614                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1615                                lcfg->lcfg_command, inst_name);
1616                 }
1617
1618                 /* we override the llog's uuid for clients, to insure they
1619                 are unique */
1620                 if (cfg->cfg_instance != NULL &&
1621                     lcfg->lcfg_command == LCFG_ATTACH) {
1622                         lustre_cfg_bufs_set_string(&bufs, 2,
1623                                                    cfg->cfg_uuid.uuid);
1624                 }
1625                 /*
1626                  * sptlrpc config record, we expect 2 data segments:
1627                  *  [0]: fs_name/target_name,
1628                  *  [1]: rule string
1629                  * moving them to index [1] and [2], and insert MGC's
1630                  * obdname at index [0].
1631                  */
1632                 if (cfg->cfg_instance == NULL &&
1633                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1634                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1635                                             bufs.lcfg_buflen[1]);
1636                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1637                                             bufs.lcfg_buflen[0]);
1638                         lustre_cfg_bufs_set_string(&bufs, 0,
1639                                                    cfg->cfg_obdname);
1640                 }
1641
1642                 /* Add net info to setup command
1643                  * if given on command line.
1644                  * So config log will be:
1645                  * [0]: client name
1646                  * [1]: client UUID
1647                  * [2]: server UUID
1648                  * [3]: inactive-on-startup
1649                  * [4]: restrictive net
1650                  */
1651                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1652                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1653                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1654                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1655
1656                         if (lcfg->lcfg_command == LCFG_SETUP &&
1657                             lcfg->lcfg_bufcount != 2 && nidnet) {
1658                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1659                                        "command for client %s\n", nidnet,
1660                                        lustre_cfg_string(lcfg, 0));
1661                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1662                         }
1663                 }
1664
1665                 /* Skip add_conn command if uuid is
1666                  * not on restricted net */
1667                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1668                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1669                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1670                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1671
1672                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1673                             lsi->lsi_lmd->lmd_nidnet &&
1674                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1675                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1676                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1677                                        uuid_str);
1678                                 rc = 0;
1679                                 /* No processing! */
1680                                 break;
1681                         }
1682                 }
1683
1684                 OBD_ALLOC(lcfg_new, lustre_cfg_len(bufs.lcfg_bufcount,
1685                                                    bufs.lcfg_buflen));
1686                 if (!lcfg_new)
1687                         GOTO(out, rc = -ENOMEM);
1688
1689                 lustre_cfg_init(lcfg_new, lcfg->lcfg_command, &bufs);
1690                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1691                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1692
1693                 /* XXX Hack to try to remain binary compatible with
1694                  * pre-newconfig logs */
1695                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1696                     (lcfg->lcfg_nid >> 32) == 0) {
1697                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1698
1699                         lcfg_new->lcfg_nid =
1700                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1701                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1702                               lcfg->lcfg_nal, addr,
1703                               libcfs_nid2str(lcfg_new->lcfg_nid));
1704                 } else {
1705                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1706                 }
1707
1708                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1709
1710                 rc = class_process_config(lcfg_new);
1711                 OBD_FREE(lcfg_new, lustre_cfg_len(lcfg_new->lcfg_bufcount,
1712                                                   lcfg_new->lcfg_buflens));
1713                 if (inst_name)
1714                         OBD_FREE(inst_name, inst_len);
1715                 break;
1716         }
1717         default:
1718                 CERROR("Unknown llog record type %#x encountered\n",
1719                        rec->lrh_type);
1720                 break;
1721         }
1722 out:
1723         if (rc) {
1724                 CERROR("%s: cfg command failed: rc = %d\n",
1725                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1726                 class_config_dump_handler(NULL, handle, rec, data);
1727         }
1728         RETURN(rc);
1729 }
1730 EXPORT_SYMBOL(class_config_llog_handler);
1731
1732 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1733                             char *name, struct config_llog_instance *cfg)
1734 {
1735         struct llog_process_cat_data cd = {
1736                 .lpcd_first_idx = 0,
1737         };
1738         struct llog_handle *llh;
1739         llog_cb_t callback;
1740         int rc;
1741         ENTRY;
1742
1743         CDEBUG(D_INFO, "looking up llog %s\n", name);
1744         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1745         if (rc)
1746                 RETURN(rc);
1747
1748         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1749         if (rc)
1750                 GOTO(parse_out, rc);
1751
1752         /* continue processing from where we last stopped to end-of-log */
1753         if (cfg) {
1754                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1755                 callback = cfg->cfg_callback;
1756                 LASSERT(callback != NULL);
1757         } else {
1758                 callback = class_config_llog_handler;
1759         }
1760
1761         cd.lpcd_last_idx = 0;
1762
1763         rc = llog_process(env, llh, callback, cfg, &cd);
1764
1765         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1766                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1767         if (cfg)
1768                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1769
1770 parse_out:
1771         llog_close(env, llh);
1772         RETURN(rc);
1773 }
1774 EXPORT_SYMBOL(class_config_parse_llog);
1775
1776 static struct lcfg_type_data {
1777         __u32    ltd_type;
1778         char    *ltd_name;
1779         char    *ltd_bufs[4];
1780 } lcfg_data_table[] = {
1781         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1782         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1783         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1784         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1785         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1786         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1787         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1788         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1789         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1790         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1791         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1792         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1793         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1794         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1795         { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1796         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1797         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1798         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1799         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1800         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1801         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1802         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1803         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1804         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1805         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1806         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1807         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1808           { "parameter", "2", "3", "4" } },
1809         { 0, NULL, { NULL, NULL, NULL, NULL } }
1810 };
1811
1812 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1813 {
1814         int i = 0;
1815
1816         while (lcfg_data_table[i].ltd_type != 0) {
1817                 if (lcfg_data_table[i].ltd_type == cmd)
1818                         return &lcfg_data_table[i];
1819                 i++;
1820         }
1821         return NULL;
1822 }
1823
1824 /**
1825  * Parse config record and output dump in supplied buffer.
1826  *
1827  * This is separated from class_config_dump_handler() to use
1828  * for ioctl needs as well
1829  *
1830  * Sample Output:
1831  * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
1832  *     UUID: lustrewt-clilov_UUID }
1833  */
1834 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1835 {
1836         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1837         char                    *ptr = buf;
1838         char                    *end = buf + size;
1839         int                      rc = 0, i;
1840         struct lcfg_type_data   *ldata;
1841
1842         LASSERT(rec->lrh_type == OBD_CFG_REC);
1843         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1844         if (rc < 0)
1845                 return rc;
1846
1847         ldata = lcfg_cmd2data(lcfg->lcfg_command);
1848         if (ldata == NULL)
1849                 return -ENOTTY;
1850
1851         if (lcfg->lcfg_command == LCFG_MARKER)
1852                 return 0;
1853
1854         /* form YAML entity */
1855         ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
1856                         rec->lrh_index, ldata->ltd_name);
1857
1858         if (lcfg->lcfg_flags)
1859                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1860                                 lcfg->lcfg_flags);
1861         if (lcfg->lcfg_num)
1862                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1863                                 lcfg->lcfg_num);
1864         if (lcfg->lcfg_nid) {
1865                 char nidstr[LNET_NIDSTR_SIZE];
1866
1867                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1868                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
1869                                 nidstr, lcfg->lcfg_nid);
1870         }
1871
1872         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1873                 ptr += snprintf(ptr, end - ptr, ", device: %s",
1874                                 lustre_cfg_string(lcfg, 0));
1875
1876         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1877                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1878                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
1879                                         ldata->ltd_bufs[i - 1],
1880                                         lustre_cfg_string(lcfg, i));
1881         }
1882
1883         ptr += snprintf(ptr, end - ptr, " }\n");
1884         /* return consumed bytes */
1885         rc = ptr - buf;
1886         return rc;
1887 }
1888
1889 /**
1890  * parse config record and output dump in supplied buffer.
1891  * This is separated from class_config_dump_handler() to use
1892  * for ioctl needs as well
1893  */
1894 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1895 {
1896         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1897         char                    *ptr = buf;
1898         char                    *end = buf + size;
1899         int                      rc = 0;
1900
1901         ENTRY;
1902
1903         LASSERT(rec->lrh_type == OBD_CFG_REC);
1904         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1905         if (rc < 0)
1906                 RETURN(rc);
1907
1908         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1909         if (lcfg->lcfg_flags)
1910                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1911                                 lcfg->lcfg_flags);
1912
1913         if (lcfg->lcfg_num)
1914                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1915
1916         if (lcfg->lcfg_nid) {
1917                 char nidstr[LNET_NIDSTR_SIZE];
1918
1919                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1920                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n     ",
1921                                 nidstr, lcfg->lcfg_nid);
1922         }
1923
1924         if (lcfg->lcfg_command == LCFG_MARKER) {
1925                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1926
1927                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1928                                 marker->cm_step, marker->cm_flags,
1929                                 marker->cm_tgtname, marker->cm_comment);
1930         } else {
1931                 int i;
1932
1933                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1934                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1935                                         lustre_cfg_string(lcfg, i));
1936                 }
1937         }
1938         ptr += snprintf(ptr, end - ptr, "\n");
1939         /* return consumed bytes */
1940         rc = ptr - buf;
1941         RETURN(rc);
1942 }
1943
1944 int class_config_dump_handler(const struct lu_env *env,
1945                               struct llog_handle *handle,
1946                               struct llog_rec_hdr *rec, void *data)
1947 {
1948         char    *outstr;
1949         int      rc = 0;
1950
1951         ENTRY;
1952
1953         OBD_ALLOC(outstr, 256);
1954         if (outstr == NULL)
1955                 RETURN(-ENOMEM);
1956
1957         if (rec->lrh_type == OBD_CFG_REC) {
1958                 class_config_parse_rec(rec, outstr, 256);
1959                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1960         } else {
1961                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1962                 rc = -EINVAL;
1963         }
1964
1965         OBD_FREE(outstr, 256);
1966         RETURN(rc);
1967 }
1968
1969 /** Call class_cleanup and class_detach.
1970  * "Manual" only in the sense that we're faking lcfg commands.
1971  */
1972 int class_manual_cleanup(struct obd_device *obd)
1973 {
1974         char                    flags[3] = "";
1975         struct lustre_cfg      *lcfg;
1976         struct lustre_cfg_bufs  bufs;
1977         int                     rc;
1978         ENTRY;
1979
1980         if (!obd) {
1981                 CERROR("empty cleanup\n");
1982                 RETURN(-EALREADY);
1983         }
1984
1985         if (obd->obd_force)
1986                 strcat(flags, "F");
1987         if (obd->obd_fail)
1988                 strcat(flags, "A");
1989
1990         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1991                obd->obd_name, flags);
1992
1993         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1994         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1995         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
1996         if (!lcfg)
1997                 RETURN(-ENOMEM);
1998         lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
1999
2000         rc = class_process_config(lcfg);
2001         if (rc) {
2002                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
2003                 GOTO(out, rc);
2004         }
2005
2006         /* the lcfg is almost the same for both ops */
2007         lcfg->lcfg_command = LCFG_DETACH;
2008         rc = class_process_config(lcfg);
2009         if (rc)
2010                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
2011 out:
2012         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
2013         RETURN(rc);
2014 }
2015 EXPORT_SYMBOL(class_manual_cleanup);
2016
2017 /*
2018  * uuid<->export lustre hash operations
2019  */
2020
2021 static unsigned
2022 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2023 {
2024         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2025                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
2026 }
2027
2028 static void *
2029 uuid_key(struct hlist_node *hnode)
2030 {
2031         struct obd_export *exp;
2032
2033         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2034
2035         return &exp->exp_client_uuid;
2036 }
2037
2038 /*
2039  * NOTE: It is impossible to find an export that is in failed
2040  *       state with this function
2041  */
2042 static int
2043 uuid_keycmp(const void *key, struct hlist_node *hnode)
2044 {
2045         struct obd_export *exp;
2046
2047         LASSERT(key);
2048         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2049
2050         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2051                !exp->exp_failed;
2052 }
2053
2054 static void *
2055 uuid_export_object(struct hlist_node *hnode)
2056 {
2057         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2058 }
2059
2060 static void
2061 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2062 {
2063         struct obd_export *exp;
2064
2065         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2066         class_export_get(exp);
2067 }
2068
2069 static void
2070 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2071 {
2072         struct obd_export *exp;
2073
2074         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2075         class_export_put(exp);
2076 }
2077
2078 static struct cfs_hash_ops uuid_hash_ops = {
2079         .hs_hash        = uuid_hash,
2080         .hs_key         = uuid_key,
2081         .hs_keycmp      = uuid_keycmp,
2082         .hs_object      = uuid_export_object,
2083         .hs_get         = uuid_export_get,
2084         .hs_put_locked  = uuid_export_put_locked,
2085 };
2086
2087
2088 /*
2089  * nid<->export hash operations
2090  */
2091
2092 static unsigned
2093 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2094 {
2095         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2096 }
2097
2098 static void *
2099 nid_key(struct hlist_node *hnode)
2100 {
2101         struct obd_export *exp;
2102
2103         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2104
2105         RETURN(&exp->exp_connection->c_peer.nid);
2106 }
2107
2108 /*
2109  * NOTE: It is impossible to find an export that is in failed
2110  *       state with this function
2111  */
2112 static int
2113 nid_kepcmp(const void *key, struct hlist_node *hnode)
2114 {
2115         struct obd_export *exp;
2116
2117         LASSERT(key);
2118         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2119
2120         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2121                !exp->exp_failed);
2122 }
2123
2124 static void *
2125 nid_export_object(struct hlist_node *hnode)
2126 {
2127         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2128 }
2129
2130 static void
2131 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2132 {
2133         struct obd_export *exp;
2134
2135         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2136         class_export_get(exp);
2137 }
2138
2139 static void
2140 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2141 {
2142         struct obd_export *exp;
2143
2144         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2145         class_export_put(exp);
2146 }
2147
2148 static struct cfs_hash_ops nid_hash_ops = {
2149         .hs_hash        = nid_hash,
2150         .hs_key         = nid_key,
2151         .hs_keycmp      = nid_kepcmp,
2152         .hs_object      = nid_export_object,
2153         .hs_get         = nid_export_get,
2154         .hs_put_locked  = nid_export_put_locked,
2155 };
2156
2157
2158 /*
2159  * nid<->nidstats hash operations
2160  */
2161
2162 static void *
2163 nidstats_key(struct hlist_node *hnode)
2164 {
2165         struct nid_stat *ns;
2166
2167         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2168
2169         return &ns->nid;
2170 }
2171
2172 static int
2173 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2174 {
2175         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2176 }
2177
2178 static void *
2179 nidstats_object(struct hlist_node *hnode)
2180 {
2181         return hlist_entry(hnode, struct nid_stat, nid_hash);
2182 }
2183
2184 static void
2185 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2186 {
2187         struct nid_stat *ns;
2188
2189         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2190         nidstat_getref(ns);
2191 }
2192
2193 static void
2194 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2195 {
2196         struct nid_stat *ns;
2197
2198         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2199         nidstat_putref(ns);
2200 }
2201
2202 static struct cfs_hash_ops nid_stat_hash_ops = {
2203         .hs_hash        = nid_hash,
2204         .hs_key         = nidstats_key,
2205         .hs_keycmp      = nidstats_keycmp,
2206         .hs_object      = nidstats_object,
2207         .hs_get         = nidstats_get,
2208         .hs_put_locked  = nidstats_put_locked,
2209 };
2210
2211
2212 /*
2213  * client_generation<->export hash operations
2214  */
2215
2216 static unsigned
2217 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2218 {
2219         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2220 }
2221
2222 static void *
2223 gen_key(struct hlist_node *hnode)
2224 {
2225         struct obd_export *exp;
2226
2227         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2228
2229         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2230 }
2231
2232 /*
2233  * NOTE: It is impossible to find an export that is in failed
2234  *       state with this function
2235  */
2236 static int
2237 gen_kepcmp(const void *key, struct hlist_node *hnode)
2238 {
2239         struct obd_export *exp;
2240
2241         LASSERT(key);
2242         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2243
2244         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2245                !exp->exp_failed);
2246 }
2247
2248 static void *
2249 gen_export_object(struct hlist_node *hnode)
2250 {
2251         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2252 }
2253
2254 static void
2255 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2256 {
2257         struct obd_export *exp;
2258
2259         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2260         class_export_get(exp);
2261 }
2262
2263 static void
2264 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2265 {
2266         struct obd_export *exp;
2267
2268         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2269         class_export_put(exp);
2270 }
2271
2272 static struct cfs_hash_ops gen_hash_ops = {
2273         .hs_hash        = gen_hash,
2274         .hs_key         = gen_key,
2275         .hs_keycmp      = gen_kepcmp,
2276         .hs_object      = gen_export_object,
2277         .hs_get         = gen_export_get,
2278         .hs_put_locked  = gen_export_put_locked,
2279 };