Whamcloud - gitweb
LU-9611 lov: allow lov.*.stripe{size,count}=-1 param
[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
1243         switch(lcfg->lcfg_command) {
1244         case LCFG_SETUP: {
1245                 err = class_setup(obd, lcfg);
1246                 GOTO(out, err);
1247         }
1248         case LCFG_DETACH: {
1249                 err = class_detach(obd, lcfg);
1250                 GOTO(out, err = 0);
1251         }
1252         case LCFG_CLEANUP: {
1253                 err = class_cleanup(obd, lcfg);
1254                 GOTO(out, err = 0);
1255         }
1256         case LCFG_ADD_CONN: {
1257                 err = class_add_conn(obd, lcfg);
1258                 GOTO(out, err = 0);
1259         }
1260         case LCFG_DEL_CONN: {
1261                 err = class_del_conn(obd, lcfg);
1262                 GOTO(out, err = 0);
1263         }
1264         case LCFG_POOL_NEW: {
1265                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1266                 GOTO(out, err = 0);
1267         }
1268         case LCFG_POOL_ADD: {
1269                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1270                                    lustre_cfg_string(lcfg, 3));
1271                 GOTO(out, err = 0);
1272         }
1273         case LCFG_POOL_REM: {
1274                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1275                                    lustre_cfg_string(lcfg, 3));
1276                 GOTO(out, err = 0);
1277         }
1278         case LCFG_POOL_DEL: {
1279                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1280                 GOTO(out, err = 0);
1281         }
1282         default: {
1283                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1284                 GOTO(out, err);
1285
1286         }
1287         }
1288 out:
1289         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1290                 CWARN("Ignoring error %d on optional command %#x\n", err,
1291                       lcfg->lcfg_command);
1292                 err = 0;
1293         }
1294         return err;
1295 }
1296 EXPORT_SYMBOL(class_process_config);
1297
1298 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1299                              struct lustre_cfg *lcfg, void *data)
1300 {
1301         struct lprocfs_vars *var;
1302         struct file fakefile;
1303         struct seq_file fake_seqfile;
1304         char *key, *sval;
1305         int i, keylen, vallen;
1306         int matched = 0, j = 0;
1307         int rc = 0;
1308         int skip = 0;
1309         ENTRY;
1310
1311         if (lcfg->lcfg_command != LCFG_PARAM) {
1312                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1313                 RETURN(-EINVAL);
1314         }
1315
1316         /* fake a seq file so that var->fops->write can work... */
1317         fakefile.private_data = &fake_seqfile;
1318         fake_seqfile.private = data;
1319         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1320            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1321            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1322         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1323                 key = lustre_cfg_buf(lcfg, i);
1324                 /* Strip off prefix */
1325                 if (class_match_param(key, prefix, &key))
1326                         /* If the prefix doesn't match, return error so we
1327                          * can pass it down the stack */
1328                         RETURN(-ENOSYS);
1329                 sval = strchr(key, '=');
1330                 if (!sval || *(sval + 1) == 0) {
1331                         CERROR("%s: can't parse param '%s' (missing '=')\n",
1332                                lustre_cfg_string(lcfg, 0),
1333                                lustre_cfg_string(lcfg, i));
1334                         /* rc = -EINVAL;        continue parsing other params */
1335                         continue;
1336                 }
1337                 keylen = sval - key;
1338                 sval++;
1339                 vallen = strlen(sval);
1340                 matched = 0;
1341                 j = 0;
1342                 /* Search proc entries */
1343                 while (lvars[j].name) {
1344                         var = &lvars[j];
1345                         if (class_match_param(key, var->name, NULL) == 0 &&
1346                             keylen == strlen(var->name)) {
1347                                 matched++;
1348                                 rc = -EROFS;
1349
1350                                 if (var->fops && var->fops->write) {
1351                                         mm_segment_t oldfs;
1352                                         oldfs = get_fs();
1353                                         set_fs(KERNEL_DS);
1354                                         rc = (var->fops->write)(&fakefile, sval,
1355                                                                 vallen, NULL);
1356                                         set_fs(oldfs);
1357                                 }
1358                                 break;
1359                         }
1360                         j++;
1361                 }
1362                 if (!matched) {
1363                         /* It was upgraded from old MDT/OST device,
1364                          * ignore the obsolete "sec_level" parameter. */
1365                         if (strncmp("sec_level", key, keylen) == 0)
1366                                 continue;
1367
1368                         CERROR("%s: unknown config parameter '%s'\n",
1369                                lustre_cfg_string(lcfg, 0),
1370                                lustre_cfg_string(lcfg, i));
1371                         /* rc = -EINVAL;        continue parsing other params */
1372                         skip++;
1373                 } else if (rc < 0) {
1374                         CERROR("%s: error writing parameter '%s': rc = %d\n",
1375                                lustre_cfg_string(lcfg, 0), key, rc);
1376                         rc = 0;
1377                 } else {
1378                         CDEBUG(D_CONFIG, "%s: set parameter '%s'\n",
1379                                lustre_cfg_string(lcfg, 0), key);
1380                 }
1381         }
1382
1383         if (rc > 0)
1384                 rc = 0;
1385         if (!rc && skip)
1386                 rc = skip;
1387         RETURN(rc);
1388 }
1389 EXPORT_SYMBOL(class_process_proc_param);
1390
1391 /*
1392  * Supplemental functions for config logs, it allocates lustre_cfg
1393  * buffers plus initialized llog record header at the beginning.
1394  */
1395 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1396 {
1397         struct llog_cfg_rec     *lcr;
1398         int                      reclen;
1399
1400         ENTRY;
1401
1402         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1403         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1404                  sizeof(struct llog_rec_tail);
1405
1406         OBD_ALLOC(lcr, reclen);
1407         if (lcr == NULL)
1408                 RETURN(NULL);
1409
1410         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1411
1412         lcr->lcr_hdr.lrh_len = reclen;
1413         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1414
1415         RETURN(lcr);
1416 }
1417 EXPORT_SYMBOL(lustre_cfg_rec_new);
1418
1419 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1420 {
1421         ENTRY;
1422         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1423         EXIT;
1424 }
1425 EXPORT_SYMBOL(lustre_cfg_rec_free);
1426
1427 /** Parse a configuration llog, doing various manipulations on them
1428  * for various reasons, (modifications for compatibility, skip obsolete
1429  * records, change uuids, etc), then class_process_config() resulting
1430  * net records.
1431  */
1432 int class_config_llog_handler(const struct lu_env *env,
1433                               struct llog_handle *handle,
1434                               struct llog_rec_hdr *rec, void *data)
1435 {
1436         struct config_llog_instance *cfg = data;
1437         int cfg_len = rec->lrh_len;
1438         char *cfg_buf = (char *) (rec + 1);
1439         int rc = 0;
1440         ENTRY;
1441
1442         /* class_config_dump_handler(handle, rec, data); */
1443
1444         switch (rec->lrh_type) {
1445         case OBD_CFG_REC: {
1446                 struct lustre_cfg *lcfg, *lcfg_new;
1447                 struct lustre_cfg_bufs bufs;
1448                 char *inst_name = NULL;
1449                 int inst_len = 0;
1450                 int swab = 0;
1451
1452                 lcfg = (struct lustre_cfg *)cfg_buf;
1453                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1454                         lustre_swab_lustre_cfg(lcfg);
1455                         swab = 1;
1456                 }
1457
1458                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1459                 if (rc)
1460                         GOTO(out, rc);
1461
1462                 /* Figure out config state info */
1463                 if (lcfg->lcfg_command == LCFG_MARKER) {
1464                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1465                         lustre_swab_cfg_marker(marker, swab,
1466                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1467                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1468                                cfg->cfg_flags, marker->cm_flags);
1469                         if (marker->cm_flags & CM_START) {
1470                                 /* all previous flags off */
1471                                 cfg->cfg_flags = CFG_F_MARKER;
1472                                 server_name2index(marker->cm_tgtname,
1473                                                   &cfg->cfg_lwp_idx, NULL);
1474                                 if (marker->cm_flags & CM_SKIP) {
1475                                         cfg->cfg_flags |= CFG_F_SKIP;
1476                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1477                                                marker->cm_step);
1478                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1479                                            (cfg->cfg_sb &&
1480                                            lustre_check_exclusion(cfg->cfg_sb,
1481                                                         marker->cm_tgtname))) {
1482                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1483                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1484                                                marker->cm_step);
1485                                 }
1486                         } else if (marker->cm_flags & CM_END) {
1487                                 cfg->cfg_flags = 0;
1488                         }
1489                 }
1490                 /* A config command without a start marker before it is
1491                    illegal (post 146) */
1492                 if (!(cfg->cfg_flags & CFG_F_COMPAT146) &&
1493                     !(cfg->cfg_flags & CFG_F_MARKER) &&
1494                     (lcfg->lcfg_command != LCFG_MARKER)) {
1495                         CWARN("Config not inside markers, ignoring! "
1496                               "(inst: %p, uuid: %s, flags: %#x)\n",
1497                                 cfg->cfg_instance,
1498                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1499                         cfg->cfg_flags |= CFG_F_SKIP;
1500                 }
1501                 if (cfg->cfg_flags & CFG_F_SKIP) {
1502                         CDEBUG(D_CONFIG, "skipping %#x\n",
1503                                cfg->cfg_flags);
1504                         rc = 0;
1505                         /* No processing! */
1506                         break;
1507                 }
1508
1509                 /*
1510                  * For interoperability between 1.8 and 2.0,
1511                  * rename "mds" obd device type to "mdt".
1512                  */
1513                 {
1514                         char *typename = lustre_cfg_string(lcfg, 1);
1515                         char *index = lustre_cfg_string(lcfg, 2);
1516
1517                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1518                             strcmp(typename, "mds") == 0)) {
1519                                 CWARN("For 1.8 interoperability, rename obd "
1520                                         "type from mds to mdt\n");
1521                                 typename[2] = 't';
1522                         }
1523                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1524                             strcmp(index, "type") == 0)) {
1525                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1526                                        "set this index to '0'\n");
1527                                 index[0] = '0';
1528                                 index[1] = 0;
1529                         }
1530                 }
1531
1532 #ifdef HAVE_SERVER_SUPPORT
1533                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1534                 {
1535                         char *typename = lustre_cfg_string(lcfg, 1);
1536
1537                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1538                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1539                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1540                                 CDEBUG(D_CONFIG,
1541                                        "For 2.x interoperability, rename obd "
1542                                        "type from lov to lod (%s)\n",
1543                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1544                                 strcpy(typename, LUSTRE_LOD_NAME);
1545                         }
1546                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1547                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1548                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1549                                 CDEBUG(D_CONFIG,
1550                                        "For 2.x interoperability, rename obd "
1551                                        "type from osc to osp (%s)\n",
1552                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1553                                 strcpy(typename, LUSTRE_OSP_NAME);
1554                         }
1555                 }
1556 #endif /* HAVE_SERVER_SUPPORT */
1557
1558                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1559                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1560                                lcfg->lcfg_command);
1561                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1562                                 /* Add inactive instead */
1563                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1564                 }
1565
1566                 lustre_cfg_bufs_reset(&bufs, NULL);
1567                 lustre_cfg_bufs_init(&bufs, lcfg);
1568
1569                 if (cfg->cfg_instance &&
1570                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1571                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1572                                    sizeof(cfg->cfg_instance) * 2 + 4;
1573                         OBD_ALLOC(inst_name, inst_len);
1574                         if (inst_name == NULL)
1575                                 GOTO(out, rc = -ENOMEM);
1576                         snprintf(inst_name, inst_len, "%s-%p",
1577                                 lustre_cfg_string(lcfg, 0),
1578                                 cfg->cfg_instance);
1579                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1580                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1581                                lcfg->lcfg_command, inst_name);
1582                 }
1583
1584                 /* we override the llog's uuid for clients, to insure they
1585                 are unique */
1586                 if (cfg->cfg_instance != NULL &&
1587                     lcfg->lcfg_command == LCFG_ATTACH) {
1588                         lustre_cfg_bufs_set_string(&bufs, 2,
1589                                                    cfg->cfg_uuid.uuid);
1590                 }
1591                 /*
1592                  * sptlrpc config record, we expect 2 data segments:
1593                  *  [0]: fs_name/target_name,
1594                  *  [1]: rule string
1595                  * moving them to index [1] and [2], and insert MGC's
1596                  * obdname at index [0].
1597                  */
1598                 if (cfg->cfg_instance == NULL &&
1599                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1600                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1601                                             bufs.lcfg_buflen[1]);
1602                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1603                                             bufs.lcfg_buflen[0]);
1604                         lustre_cfg_bufs_set_string(&bufs, 0,
1605                                                    cfg->cfg_obdname);
1606                 }
1607
1608                 /* Add net info to setup command
1609                  * if given on command line.
1610                  * So config log will be:
1611                  * [0]: client name
1612                  * [1]: client UUID
1613                  * [2]: server UUID
1614                  * [3]: inactive-on-startup
1615                  * [4]: restrictive net
1616                  */
1617                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1618                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1619                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1620                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1621
1622                         if (lcfg->lcfg_command == LCFG_SETUP &&
1623                             lcfg->lcfg_bufcount != 2 && nidnet) {
1624                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1625                                        "command for client %s\n", nidnet,
1626                                        lustre_cfg_string(lcfg, 0));
1627                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1628                         }
1629                 }
1630
1631                 /* Skip add_conn command if uuid is
1632                  * not on restricted net */
1633                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1634                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1635                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1636                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1637
1638                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1639                             lsi->lsi_lmd->lmd_nidnet &&
1640                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1641                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1642                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1643                                        uuid_str);
1644                                 rc = 0;
1645                                 /* No processing! */
1646                                 break;
1647                         }
1648                 }
1649
1650                 OBD_ALLOC(lcfg_new, lustre_cfg_len(bufs.lcfg_bufcount,
1651                                                    bufs.lcfg_buflen));
1652                 if (!lcfg_new)
1653                         GOTO(out, rc = -ENOMEM);
1654
1655                 lustre_cfg_init(lcfg_new, lcfg->lcfg_command, &bufs);
1656                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1657                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1658
1659                 /* XXX Hack to try to remain binary compatible with
1660                  * pre-newconfig logs */
1661                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1662                     (lcfg->lcfg_nid >> 32) == 0) {
1663                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1664
1665                         lcfg_new->lcfg_nid =
1666                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1667                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1668                               lcfg->lcfg_nal, addr,
1669                               libcfs_nid2str(lcfg_new->lcfg_nid));
1670                 } else {
1671                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1672                 }
1673
1674                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1675
1676                 rc = class_process_config(lcfg_new);
1677                 OBD_FREE(lcfg_new, lustre_cfg_len(lcfg_new->lcfg_bufcount,
1678                                                   lcfg_new->lcfg_buflens));
1679                 if (inst_name)
1680                         OBD_FREE(inst_name, inst_len);
1681                 break;
1682         }
1683         default:
1684                 CERROR("Unknown llog record type %#x encountered\n",
1685                        rec->lrh_type);
1686                 break;
1687         }
1688 out:
1689         if (rc) {
1690                 CERROR("%s: cfg command failed: rc = %d\n",
1691                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1692                 class_config_dump_handler(NULL, handle, rec, data);
1693         }
1694         RETURN(rc);
1695 }
1696 EXPORT_SYMBOL(class_config_llog_handler);
1697
1698 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1699                             char *name, struct config_llog_instance *cfg)
1700 {
1701         struct llog_process_cat_data cd = {
1702                 .lpcd_first_idx = 0,
1703         };
1704         struct llog_handle *llh;
1705         llog_cb_t callback;
1706         int rc;
1707         ENTRY;
1708
1709         CDEBUG(D_INFO, "looking up llog %s\n", name);
1710         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1711         if (rc)
1712                 RETURN(rc);
1713
1714         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1715         if (rc)
1716                 GOTO(parse_out, rc);
1717
1718         /* continue processing from where we last stopped to end-of-log */
1719         if (cfg) {
1720                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1721                 callback = cfg->cfg_callback;
1722                 LASSERT(callback != NULL);
1723         } else {
1724                 callback = class_config_llog_handler;
1725         }
1726
1727         cd.lpcd_last_idx = 0;
1728
1729         rc = llog_process(env, llh, callback, cfg, &cd);
1730
1731         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1732                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1733         if (cfg)
1734                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1735
1736 parse_out:
1737         llog_close(env, llh);
1738         RETURN(rc);
1739 }
1740 EXPORT_SYMBOL(class_config_parse_llog);
1741
1742 static struct lcfg_type_data {
1743         __u32    ltd_type;
1744         char    *ltd_name;
1745         char    *ltd_bufs[4];
1746 } lcfg_data_table[] = {
1747         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1748         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1749         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1750         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1751         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1752         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1753         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1754         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1755         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1756         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1757         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1758         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1759         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1760         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1761         { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1762         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1763         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1764         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1765         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1766         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1767         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1768         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1769         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1770         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1771         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1772         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1773         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1774           { "parameter", "2", "3", "4" } },
1775         { 0, NULL, { NULL, NULL, NULL, NULL } }
1776 };
1777
1778 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1779 {
1780         int i = 0;
1781
1782         while (lcfg_data_table[i].ltd_type != 0) {
1783                 if (lcfg_data_table[i].ltd_type == cmd)
1784                         return &lcfg_data_table[i];
1785                 i++;
1786         }
1787         return NULL;
1788 }
1789
1790 /**
1791  * Parse config record and output dump in supplied buffer.
1792  *
1793  * This is separated from class_config_dump_handler() to use
1794  * for ioctl needs as well
1795  *
1796  * Sample Output:
1797  * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
1798  *     UUID: lustrewt-clilov_UUID }
1799  */
1800 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1801 {
1802         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1803         char                    *ptr = buf;
1804         char                    *end = buf + size;
1805         int                      rc = 0, i;
1806         struct lcfg_type_data   *ldata;
1807
1808         LASSERT(rec->lrh_type == OBD_CFG_REC);
1809         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1810         if (rc < 0)
1811                 return rc;
1812
1813         ldata = lcfg_cmd2data(lcfg->lcfg_command);
1814         if (ldata == NULL)
1815                 return -ENOTTY;
1816
1817         if (lcfg->lcfg_command == LCFG_MARKER)
1818                 return 0;
1819
1820         /* form YAML entity */
1821         ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
1822                         rec->lrh_index, ldata->ltd_name);
1823
1824         if (lcfg->lcfg_flags)
1825                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1826                                 lcfg->lcfg_flags);
1827         if (lcfg->lcfg_num)
1828                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1829                                 lcfg->lcfg_num);
1830         if (lcfg->lcfg_nid) {
1831                 char nidstr[LNET_NIDSTR_SIZE];
1832
1833                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1834                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
1835                                 nidstr, lcfg->lcfg_nid);
1836         }
1837
1838         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1839                 ptr += snprintf(ptr, end - ptr, ", device: %s",
1840                                 lustre_cfg_string(lcfg, 0));
1841
1842         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1843                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1844                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
1845                                         ldata->ltd_bufs[i - 1],
1846                                         lustre_cfg_string(lcfg, i));
1847         }
1848
1849         ptr += snprintf(ptr, end - ptr, " }\n");
1850         /* return consumed bytes */
1851         rc = ptr - buf;
1852         return rc;
1853 }
1854
1855 /**
1856  * parse config record and output dump in supplied buffer.
1857  * This is separated from class_config_dump_handler() to use
1858  * for ioctl needs as well
1859  */
1860 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1861 {
1862         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1863         char                    *ptr = buf;
1864         char                    *end = buf + size;
1865         int                      rc = 0;
1866
1867         ENTRY;
1868
1869         LASSERT(rec->lrh_type == OBD_CFG_REC);
1870         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1871         if (rc < 0)
1872                 RETURN(rc);
1873
1874         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1875         if (lcfg->lcfg_flags)
1876                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1877                                 lcfg->lcfg_flags);
1878
1879         if (lcfg->lcfg_num)
1880                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1881
1882         if (lcfg->lcfg_nid) {
1883                 char nidstr[LNET_NIDSTR_SIZE];
1884
1885                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1886                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n     ",
1887                                 nidstr, lcfg->lcfg_nid);
1888         }
1889
1890         if (lcfg->lcfg_command == LCFG_MARKER) {
1891                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1892
1893                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1894                                 marker->cm_step, marker->cm_flags,
1895                                 marker->cm_tgtname, marker->cm_comment);
1896         } else {
1897                 int i;
1898
1899                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1900                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1901                                         lustre_cfg_string(lcfg, i));
1902                 }
1903         }
1904         ptr += snprintf(ptr, end - ptr, "\n");
1905         /* return consumed bytes */
1906         rc = ptr - buf;
1907         RETURN(rc);
1908 }
1909
1910 int class_config_dump_handler(const struct lu_env *env,
1911                               struct llog_handle *handle,
1912                               struct llog_rec_hdr *rec, void *data)
1913 {
1914         char    *outstr;
1915         int      rc = 0;
1916
1917         ENTRY;
1918
1919         OBD_ALLOC(outstr, 256);
1920         if (outstr == NULL)
1921                 RETURN(-ENOMEM);
1922
1923         if (rec->lrh_type == OBD_CFG_REC) {
1924                 class_config_parse_rec(rec, outstr, 256);
1925                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1926         } else {
1927                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1928                 rc = -EINVAL;
1929         }
1930
1931         OBD_FREE(outstr, 256);
1932         RETURN(rc);
1933 }
1934
1935 /** Call class_cleanup and class_detach.
1936  * "Manual" only in the sense that we're faking lcfg commands.
1937  */
1938 int class_manual_cleanup(struct obd_device *obd)
1939 {
1940         char                    flags[3] = "";
1941         struct lustre_cfg      *lcfg;
1942         struct lustre_cfg_bufs  bufs;
1943         int                     rc;
1944         ENTRY;
1945
1946         if (!obd) {
1947                 CERROR("empty cleanup\n");
1948                 RETURN(-EALREADY);
1949         }
1950
1951         if (obd->obd_force)
1952                 strcat(flags, "F");
1953         if (obd->obd_fail)
1954                 strcat(flags, "A");
1955
1956         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1957                obd->obd_name, flags);
1958
1959         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1960         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1961         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
1962         if (!lcfg)
1963                 RETURN(-ENOMEM);
1964         lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
1965
1966         rc = class_process_config(lcfg);
1967         if (rc) {
1968                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1969                 GOTO(out, rc);
1970         }
1971
1972         /* the lcfg is almost the same for both ops */
1973         lcfg->lcfg_command = LCFG_DETACH;
1974         rc = class_process_config(lcfg);
1975         if (rc)
1976                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1977 out:
1978         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
1979         RETURN(rc);
1980 }
1981 EXPORT_SYMBOL(class_manual_cleanup);
1982
1983 /*
1984  * uuid<->export lustre hash operations
1985  */
1986
1987 static unsigned
1988 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
1989 {
1990         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
1991                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
1992 }
1993
1994 static void *
1995 uuid_key(struct hlist_node *hnode)
1996 {
1997         struct obd_export *exp;
1998
1999         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2000
2001         return &exp->exp_client_uuid;
2002 }
2003
2004 /*
2005  * NOTE: It is impossible to find an export that is in failed
2006  *       state with this function
2007  */
2008 static int
2009 uuid_keycmp(const void *key, struct hlist_node *hnode)
2010 {
2011         struct obd_export *exp;
2012
2013         LASSERT(key);
2014         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2015
2016         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2017                !exp->exp_failed;
2018 }
2019
2020 static void *
2021 uuid_export_object(struct hlist_node *hnode)
2022 {
2023         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2024 }
2025
2026 static void
2027 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2028 {
2029         struct obd_export *exp;
2030
2031         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2032         class_export_get(exp);
2033 }
2034
2035 static void
2036 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2037 {
2038         struct obd_export *exp;
2039
2040         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2041         class_export_put(exp);
2042 }
2043
2044 static struct cfs_hash_ops uuid_hash_ops = {
2045         .hs_hash        = uuid_hash,
2046         .hs_key         = uuid_key,
2047         .hs_keycmp      = uuid_keycmp,
2048         .hs_object      = uuid_export_object,
2049         .hs_get         = uuid_export_get,
2050         .hs_put_locked  = uuid_export_put_locked,
2051 };
2052
2053
2054 /*
2055  * nid<->export hash operations
2056  */
2057
2058 static unsigned
2059 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2060 {
2061         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2062 }
2063
2064 static void *
2065 nid_key(struct hlist_node *hnode)
2066 {
2067         struct obd_export *exp;
2068
2069         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2070
2071         RETURN(&exp->exp_connection->c_peer.nid);
2072 }
2073
2074 /*
2075  * NOTE: It is impossible to find an export that is in failed
2076  *       state with this function
2077  */
2078 static int
2079 nid_kepcmp(const void *key, struct hlist_node *hnode)
2080 {
2081         struct obd_export *exp;
2082
2083         LASSERT(key);
2084         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2085
2086         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2087                !exp->exp_failed);
2088 }
2089
2090 static void *
2091 nid_export_object(struct hlist_node *hnode)
2092 {
2093         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2094 }
2095
2096 static void
2097 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2098 {
2099         struct obd_export *exp;
2100
2101         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2102         class_export_get(exp);
2103 }
2104
2105 static void
2106 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2107 {
2108         struct obd_export *exp;
2109
2110         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2111         class_export_put(exp);
2112 }
2113
2114 static struct cfs_hash_ops nid_hash_ops = {
2115         .hs_hash        = nid_hash,
2116         .hs_key         = nid_key,
2117         .hs_keycmp      = nid_kepcmp,
2118         .hs_object      = nid_export_object,
2119         .hs_get         = nid_export_get,
2120         .hs_put_locked  = nid_export_put_locked,
2121 };
2122
2123
2124 /*
2125  * nid<->nidstats hash operations
2126  */
2127
2128 static void *
2129 nidstats_key(struct hlist_node *hnode)
2130 {
2131         struct nid_stat *ns;
2132
2133         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2134
2135         return &ns->nid;
2136 }
2137
2138 static int
2139 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2140 {
2141         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2142 }
2143
2144 static void *
2145 nidstats_object(struct hlist_node *hnode)
2146 {
2147         return hlist_entry(hnode, struct nid_stat, nid_hash);
2148 }
2149
2150 static void
2151 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2152 {
2153         struct nid_stat *ns;
2154
2155         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2156         nidstat_getref(ns);
2157 }
2158
2159 static void
2160 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2161 {
2162         struct nid_stat *ns;
2163
2164         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2165         nidstat_putref(ns);
2166 }
2167
2168 static struct cfs_hash_ops nid_stat_hash_ops = {
2169         .hs_hash        = nid_hash,
2170         .hs_key         = nidstats_key,
2171         .hs_keycmp      = nidstats_keycmp,
2172         .hs_object      = nidstats_object,
2173         .hs_get         = nidstats_get,
2174         .hs_put_locked  = nidstats_put_locked,
2175 };
2176
2177
2178 /*
2179  * client_generation<->export hash operations
2180  */
2181
2182 static unsigned
2183 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2184 {
2185         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2186 }
2187
2188 static void *
2189 gen_key(struct hlist_node *hnode)
2190 {
2191         struct obd_export *exp;
2192
2193         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2194
2195         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2196 }
2197
2198 /*
2199  * NOTE: It is impossible to find an export that is in failed
2200  *       state with this function
2201  */
2202 static int
2203 gen_kepcmp(const void *key, struct hlist_node *hnode)
2204 {
2205         struct obd_export *exp;
2206
2207         LASSERT(key);
2208         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2209
2210         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2211                !exp->exp_failed);
2212 }
2213
2214 static void *
2215 gen_export_object(struct hlist_node *hnode)
2216 {
2217         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2218 }
2219
2220 static void
2221 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2222 {
2223         struct obd_export *exp;
2224
2225         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2226         class_export_get(exp);
2227 }
2228
2229 static void
2230 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2231 {
2232         struct obd_export *exp;
2233
2234         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2235         class_export_put(exp);
2236 }
2237
2238 static struct cfs_hash_ops gen_hash_ops = {
2239         .hs_hash        = gen_hash,
2240         .hs_key         = gen_key,
2241         .hs_keycmp      = gen_kepcmp,
2242         .hs_object      = gen_export_object,
2243         .hs_get         = gen_export_get,
2244         .hs_put_locked  = gen_export_put_locked,
2245 };