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