Whamcloud - gitweb
LU-12254 lnet: correct discovery LNetEQFree()
[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 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1500                              struct lustre_cfg *lcfg, void *data)
1501 {
1502         struct lprocfs_vars *var;
1503         struct file fakefile;
1504         struct seq_file fake_seqfile;
1505         char *key, *sval;
1506         int i, keylen, vallen;
1507         int matched = 0, j = 0;
1508         int rc = 0;
1509         int skip = 0;
1510
1511         ENTRY;
1512
1513         if (lcfg->lcfg_command != LCFG_PARAM) {
1514                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1515                 RETURN(-EINVAL);
1516         }
1517
1518         /* fake a seq file so that var->fops->write can work... */
1519         fakefile.private_data = &fake_seqfile;
1520         fake_seqfile.private = data;
1521         /*
1522          * e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1523          * or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1524          * or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1525          */
1526         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1527                 key = lustre_cfg_buf(lcfg, i);
1528                 /* Strip off prefix */
1529                 if (class_match_param(key, prefix, &key))
1530                         /*
1531                          * If the prefix doesn't match, return error so we
1532                          * can pass it down the stack
1533                          */
1534                         RETURN(-ENOSYS);
1535                 sval = strchr(key, '=');
1536                 if (!sval || *(sval + 1) == 0) {
1537                         CERROR("%s: can't parse param '%s' (missing '=')\n",
1538                                lustre_cfg_string(lcfg, 0),
1539                                lustre_cfg_string(lcfg, i));
1540                         /* rc = -EINVAL;        continue parsing other params */
1541                         continue;
1542                 }
1543                 keylen = sval - key;
1544                 sval++;
1545                 vallen = strlen(sval);
1546                 matched = 0;
1547                 j = 0;
1548                 /* Search proc entries */
1549                 while (lvars[j].name) {
1550                         var = &lvars[j];
1551                         if (class_match_param(key, var->name, NULL) == 0 &&
1552                             keylen == strlen(var->name)) {
1553                                 matched++;
1554                                 rc = -EROFS;
1555
1556                                 if (var->fops && var->fops->write) {
1557                                         mm_segment_t oldfs;
1558                                         oldfs = get_fs();
1559                                         set_fs(KERNEL_DS);
1560                                         rc = (var->fops->write)(&fakefile, sval,
1561                                                                 vallen, NULL);
1562                                         set_fs(oldfs);
1563                                 }
1564                                 break;
1565                         }
1566                         j++;
1567                 }
1568                 if (!matched) {
1569                         /*
1570                          * It was upgraded from old MDT/OST device,
1571                          * ignore the obsolete "sec_level" parameter.
1572                          */
1573                         if (strncmp("sec_level", key, keylen) == 0)
1574                                 continue;
1575
1576                         CERROR("%s: unknown config parameter '%s'\n",
1577                                lustre_cfg_string(lcfg, 0),
1578                                lustre_cfg_string(lcfg, i));
1579                         /* rc = -EINVAL;        continue parsing other params */
1580                         skip++;
1581                 } else if (rc < 0) {
1582                         CERROR("%s: error writing parameter '%s': rc = %d\n",
1583                                lustre_cfg_string(lcfg, 0), key, rc);
1584                         rc = 0;
1585                 } else {
1586                         CDEBUG(D_CONFIG, "%s: set parameter '%s'\n",
1587                                lustre_cfg_string(lcfg, 0), key);
1588                 }
1589         }
1590
1591         if (rc > 0)
1592                 rc = 0;
1593         if (!rc && skip)
1594                 rc = skip;
1595         RETURN(rc);
1596 }
1597 EXPORT_SYMBOL(class_process_proc_param);
1598
1599 /*
1600  * Supplemental functions for config logs, it allocates lustre_cfg
1601  * buffers plus initialized llog record header at the beginning.
1602  */
1603 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1604 {
1605         struct llog_cfg_rec *lcr;
1606         int reclen;
1607
1608         ENTRY;
1609
1610         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1611         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1612                  sizeof(struct llog_rec_tail);
1613
1614         OBD_ALLOC(lcr, reclen);
1615         if (!lcr)
1616                 RETURN(NULL);
1617
1618         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1619
1620         lcr->lcr_hdr.lrh_len = reclen;
1621         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1622
1623         RETURN(lcr);
1624 }
1625 EXPORT_SYMBOL(lustre_cfg_rec_new);
1626
1627 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1628 {
1629         ENTRY;
1630         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1631         EXIT;
1632 }
1633 EXPORT_SYMBOL(lustre_cfg_rec_free);
1634
1635 /**
1636  * Parse a configuration llog, doing various manipulations on them
1637  * for various reasons, (modifications for compatibility, skip obsolete
1638  * records, change uuids, etc), then class_process_config() resulting
1639  * net records.
1640  */
1641 int class_config_llog_handler(const struct lu_env *env,
1642                               struct llog_handle *handle,
1643                               struct llog_rec_hdr *rec, void *data)
1644 {
1645         struct config_llog_instance *cfg = data;
1646         int cfg_len = rec->lrh_len;
1647         char *cfg_buf = (char *) (rec + 1);
1648         int rc = 0;
1649         ENTRY;
1650
1651         /* class_config_dump_handler(handle, rec, data); */
1652
1653         switch (rec->lrh_type) {
1654         case OBD_CFG_REC: {
1655                 struct lustre_cfg *lcfg, *lcfg_new;
1656                 struct lustre_cfg_bufs bufs;
1657                 char *inst_name = NULL;
1658                 int inst_len = 0;
1659                 int swab = 0;
1660
1661                 lcfg = (struct lustre_cfg *)cfg_buf;
1662                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1663                         lustre_swab_lustre_cfg(lcfg);
1664                         swab = 1;
1665                 }
1666
1667                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1668                 if (rc)
1669                         GOTO(out, rc);
1670
1671                 /* Figure out config state info */
1672                 if (lcfg->lcfg_command == LCFG_MARKER) {
1673                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1674                         lustre_swab_cfg_marker(marker, swab,
1675                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1676                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1677                                cfg->cfg_flags, marker->cm_flags);
1678                         if (marker->cm_flags & CM_START) {
1679                                 /* all previous flags off */
1680                                 cfg->cfg_flags = CFG_F_MARKER;
1681                                 server_name2index(marker->cm_tgtname,
1682                                                   &cfg->cfg_lwp_idx, NULL);
1683                                 if (marker->cm_flags & CM_SKIP) {
1684                                         cfg->cfg_flags |= CFG_F_SKIP;
1685                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1686                                                marker->cm_step);
1687                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1688                                            (cfg->cfg_sb &&
1689                                            lustre_check_exclusion(cfg->cfg_sb,
1690                                                         marker->cm_tgtname))) {
1691                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1692                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1693                                                marker->cm_step);
1694                                 }
1695                         } else if (marker->cm_flags & CM_END) {
1696                                 cfg->cfg_flags = 0;
1697                         }
1698                 }
1699                 /*
1700                  * A config command without a start marker before it is
1701                  * illegal
1702                  */
1703                 if (!(cfg->cfg_flags & CFG_F_MARKER) &&
1704                     (lcfg->lcfg_command != LCFG_MARKER)) {
1705                         CWARN("Skip config outside markers, (inst: %016lx, uuid: %s, flags: %#x)\n",
1706                                 cfg->cfg_instance,
1707                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1708                         cfg->cfg_flags |= CFG_F_SKIP;
1709                 }
1710                 if (cfg->cfg_flags & CFG_F_SKIP) {
1711                         CDEBUG(D_CONFIG, "skipping %#x\n",
1712                                cfg->cfg_flags);
1713                         rc = 0;
1714                         /* No processing! */
1715                         break;
1716                 }
1717
1718                 /*
1719                  * For interoperability between 1.8 and 2.0,
1720                  * rename "mds" OBD device type to "mdt".
1721                  */
1722                 {
1723                         char *typename = lustre_cfg_string(lcfg, 1);
1724                         char *index = lustre_cfg_string(lcfg, 2);
1725
1726                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1727                             strcmp(typename, "mds") == 0)) {
1728                                 CWARN("For 1.8 interoperability, rename obd "
1729                                         "type from mds to mdt\n");
1730                                 typename[2] = 't';
1731                         }
1732                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1733                             strcmp(index, "type") == 0)) {
1734                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1735                                        "set this index to '0'\n");
1736                                 index[0] = '0';
1737                                 index[1] = 0;
1738                         }
1739                 }
1740
1741 #ifdef HAVE_SERVER_SUPPORT
1742                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1743                 {
1744                         char *typename = lustre_cfg_string(lcfg, 1);
1745
1746                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1747                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1748                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1749                                 CDEBUG(D_CONFIG,
1750                                        "For 2.x interoperability, rename obd "
1751                                        "type from lov to lod (%s)\n",
1752                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1753                                 strcpy(typename, LUSTRE_LOD_NAME);
1754                         }
1755                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1756                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1757                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1758                                 CDEBUG(D_CONFIG,
1759                                        "For 2.x interoperability, rename obd "
1760                                        "type from osc to osp (%s)\n",
1761                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1762                                 strcpy(typename, LUSTRE_OSP_NAME);
1763                         }
1764                 }
1765 #endif /* HAVE_SERVER_SUPPORT */
1766
1767                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1768                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1769                                lcfg->lcfg_command);
1770                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1771                                 /* Add inactive instead */
1772                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1773                 }
1774
1775                 lustre_cfg_bufs_reset(&bufs, NULL);
1776                 lustre_cfg_bufs_init(&bufs, lcfg);
1777
1778                 if (cfg->cfg_instance &&
1779                     lcfg->lcfg_command != LCFG_SPTLRPC_CONF &&
1780                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1781                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) + 16 + 4;
1782                         OBD_ALLOC(inst_name, inst_len);
1783                         if (!inst_name)
1784                                 GOTO(out, rc = -ENOMEM);
1785                         snprintf(inst_name, inst_len, "%s-%016lx",
1786                                 lustre_cfg_string(lcfg, 0),
1787                                 cfg->cfg_instance);
1788                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1789                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1790                                lcfg->lcfg_command, inst_name);
1791                 }
1792
1793                 /* override llog UUID for clients, to insure they are unique */
1794                 if (cfg->cfg_instance && lcfg->lcfg_command == LCFG_ATTACH)
1795                         lustre_cfg_bufs_set_string(&bufs, 2,
1796                                                    cfg->cfg_uuid.uuid);
1797                 /*
1798                  * sptlrpc config record, we expect 2 data segments:
1799                  *  [0]: fs_name/target_name,
1800                  *  [1]: rule string
1801                  * moving them to index [1] and [2], and insert MGC's
1802                  * obdname at index [0].
1803                  */
1804                 if (cfg->cfg_instance &&
1805                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1806                         /* After ASLR changes cfg_instance this needs fixing */
1807                         /* "obd" is set in config_log_find_or_add() */
1808                         struct obd_device *obd = (void *)cfg->cfg_instance;
1809
1810                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1811                                             bufs.lcfg_buflen[1]);
1812                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1813                                             bufs.lcfg_buflen[0]);
1814                         lustre_cfg_bufs_set_string(&bufs, 0,
1815                                                    obd->obd_name);
1816                 }
1817
1818                 /*
1819                  * Add net info to setup command
1820                  * if given on command line.
1821                  * So config log will be:
1822                  * [0]: client name
1823                  * [1]: client UUID
1824                  * [2]: server UUID
1825                  * [3]: inactive-on-startup
1826                  * [4]: restrictive net
1827                  */
1828                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1829                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1830                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1831                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1832
1833                         if (lcfg->lcfg_command == LCFG_SETUP &&
1834                             lcfg->lcfg_bufcount != 2 && nidnet) {
1835                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1836                                        "command for client %s\n", nidnet,
1837                                        lustre_cfg_string(lcfg, 0));
1838                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1839                         }
1840                 }
1841
1842                 /*
1843                  * Skip add_conn command if uuid is
1844                  * not on restricted net
1845                  */
1846                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1847                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1848                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1849                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1850
1851                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1852                             lsi->lsi_lmd->lmd_nidnet &&
1853                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1854                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1855                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1856                                        uuid_str);
1857                                 rc = 0;
1858                                 /* No processing! */
1859                                 break;
1860                         }
1861                 }
1862
1863                 OBD_ALLOC(lcfg_new, lustre_cfg_len(bufs.lcfg_bufcount,
1864                                                    bufs.lcfg_buflen));
1865                 if (!lcfg_new)
1866                         GOTO(out, rc = -ENOMEM);
1867
1868                 lustre_cfg_init(lcfg_new, lcfg->lcfg_command, &bufs);
1869                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1870                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1871
1872                 /*
1873                  * XXX Hack to try to remain binary compatible with
1874                  * pre-newconfig logs
1875                  */
1876                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1877                     (lcfg->lcfg_nid >> 32) == 0) {
1878                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1879
1880                         lcfg_new->lcfg_nid =
1881                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1882                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1883                               lcfg->lcfg_nal, addr,
1884                               libcfs_nid2str(lcfg_new->lcfg_nid));
1885                 } else {
1886                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1887                 }
1888
1889                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1890
1891                 rc = class_process_config(lcfg_new);
1892                 OBD_FREE(lcfg_new, lustre_cfg_len(lcfg_new->lcfg_bufcount,
1893                                                   lcfg_new->lcfg_buflens));
1894                 if (inst_name)
1895                         OBD_FREE(inst_name, inst_len);
1896                 break;
1897         }
1898         default:
1899                 CERROR("Unknown llog record type %#x encountered\n",
1900                        rec->lrh_type);
1901                 break;
1902         }
1903 out:
1904         if (rc) {
1905                 CERROR("%s: cfg command failed: rc = %d\n",
1906                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1907                 class_config_dump_handler(NULL, handle, rec, data);
1908         }
1909         RETURN(rc);
1910 }
1911 EXPORT_SYMBOL(class_config_llog_handler);
1912
1913 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1914                             char *name, struct config_llog_instance *cfg)
1915 {
1916         struct llog_process_cat_data cd = {
1917                 .lpcd_first_idx = 0,
1918         };
1919         struct llog_handle *llh;
1920         llog_cb_t callback;
1921         int rc;
1922         ENTRY;
1923
1924         CDEBUG(D_INFO, "looking up llog %s\n", name);
1925         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1926         if (rc)
1927                 RETURN(rc);
1928
1929         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1930         if (rc)
1931                 GOTO(parse_out, rc);
1932
1933         /* continue processing from where we last stopped to end-of-log */
1934         if (cfg) {
1935                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1936                 callback = cfg->cfg_callback;
1937                 LASSERT(callback != NULL);
1938         } else {
1939                 callback = class_config_llog_handler;
1940         }
1941
1942         cd.lpcd_last_idx = 0;
1943
1944         rc = llog_process(env, llh, callback, cfg, &cd);
1945
1946         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1947                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1948         if (cfg)
1949                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1950
1951 parse_out:
1952         llog_close(env, llh);
1953         RETURN(rc);
1954 }
1955 EXPORT_SYMBOL(class_config_parse_llog);
1956
1957 static struct lcfg_type_data {
1958         __u32    ltd_type;
1959         char    *ltd_name;
1960         char    *ltd_bufs[4];
1961 } lcfg_data_table[] = {
1962         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1963         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1964         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1965         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1966         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1967         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1968         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1969         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1970         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1971         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1972         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1973         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1974         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1975         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1976         { LCFG_PARAM, "conf_param", { "parameter", "value", "3", "4" } },
1977         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1978         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1979         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1980         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1981         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1982         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1983         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1984         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1985         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1986         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1987         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1988         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1989           { "parameter", "2", "3", "4" } },
1990         { LCFG_SET_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1991         { 0, NULL, { NULL, NULL, NULL, NULL } }
1992 };
1993
1994 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1995 {
1996         int i = 0;
1997
1998         while (lcfg_data_table[i].ltd_type != 0) {
1999                 if (lcfg_data_table[i].ltd_type == cmd)
2000                         return &lcfg_data_table[i];
2001                 i++;
2002         }
2003         return NULL;
2004 }
2005
2006 /**
2007  * Parse config record and output dump in supplied buffer.
2008  *
2009  * This is separated from class_config_dump_handler() to use
2010  * for ioctl needs as well
2011  *
2012  * Sample Output:
2013  * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
2014  *     UUID: lustrewt-clilov_UUID }
2015  */
2016 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
2017 {
2018         struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
2019         char *ptr = buf;
2020         char *end = buf + size;
2021         int rc = 0, i;
2022         struct lcfg_type_data *ldata;
2023
2024         LASSERT(rec->lrh_type == OBD_CFG_REC);
2025         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2026         if (rc < 0)
2027                 return rc;
2028
2029         ldata = lcfg_cmd2data(lcfg->lcfg_command);
2030         if (!ldata)
2031                 return -ENOTTY;
2032
2033         if (lcfg->lcfg_command == LCFG_MARKER)
2034                 return 0;
2035
2036         /* form YAML entity */
2037         ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
2038                         rec->lrh_index, ldata->ltd_name);
2039         if (end - ptr <= 0)
2040                 goto out_overflow;
2041
2042         if (lcfg->lcfg_flags) {
2043                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
2044                                 lcfg->lcfg_flags);
2045                 if (end - ptr <= 0)
2046                         goto out_overflow;
2047         }
2048         if (lcfg->lcfg_num) {
2049                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
2050                                 lcfg->lcfg_num);
2051                 if (end - ptr <= 0)
2052                         goto out_overflow;
2053         }
2054         if (lcfg->lcfg_nid) {
2055                 char nidstr[LNET_NIDSTR_SIZE];
2056
2057                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2058                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
2059                                 nidstr, lcfg->lcfg_nid);
2060                 if (end - ptr <= 0)
2061                         goto out_overflow;
2062         }
2063
2064         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
2065                 ptr += snprintf(ptr, end - ptr, ", device: %s",
2066                                 lustre_cfg_string(lcfg, 0));
2067                 if (end - ptr <= 0)
2068                         goto out_overflow;
2069         }
2070
2071         if (lcfg->lcfg_command == LCFG_SET_PARAM) {
2072                 /*
2073                  * set_param -P parameters have param=val here, separate
2074                  * them through pointer magic and print them out in
2075                  * native yamlese
2076                  */
2077                 char *cfg_str = lustre_cfg_string(lcfg, 1);
2078                 char *tmp = strchr(cfg_str, '=');
2079                 size_t len;
2080
2081                 if (!tmp)
2082                         goto out_done;
2083
2084                 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[0]);
2085                 len = tmp - cfg_str + 1;
2086                 snprintf(ptr, len, "%s", cfg_str);
2087                 ptr += len - 1;
2088
2089                 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[1]);
2090                 ptr += snprintf(ptr, end - ptr, "%s", tmp + 1);
2091
2092                 goto out_done;
2093         }
2094
2095         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
2096                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0) {
2097                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
2098                                         ldata->ltd_bufs[i - 1],
2099                                         lustre_cfg_string(lcfg, i));
2100                         if (end - ptr <= 0)
2101                                 goto out_overflow;
2102                 }
2103         }
2104
2105 out_done:
2106         ptr += snprintf(ptr, end - ptr, " }\n");
2107 out_overflow:
2108         /* Return consumed bytes.  If the buffer overflowed, zero last byte */
2109         rc = ptr - buf;
2110         if (rc > size) {
2111                 rc = -EOVERFLOW;
2112                 *(end - 1) = '\0';
2113         }
2114
2115         return rc;
2116 }
2117
2118 /**
2119  * parse config record and output dump in supplied buffer.
2120  * This is separated from class_config_dump_handler() to use
2121  * for ioctl needs as well
2122  */
2123 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
2124 {
2125         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
2126         char                    *ptr = buf;
2127         char                    *end = buf + size;
2128         int                      rc = 0;
2129
2130         ENTRY;
2131
2132         LASSERT(rec->lrh_type == OBD_CFG_REC);
2133         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2134         if (rc < 0)
2135                 RETURN(rc);
2136
2137         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
2138         if (lcfg->lcfg_flags)
2139                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
2140                                 lcfg->lcfg_flags);
2141
2142         if (lcfg->lcfg_num)
2143                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
2144
2145         if (lcfg->lcfg_nid) {
2146                 char nidstr[LNET_NIDSTR_SIZE];
2147
2148                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2149                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)    ",
2150                                 nidstr, lcfg->lcfg_nid);
2151         }
2152
2153         if (lcfg->lcfg_command == LCFG_MARKER) {
2154                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
2155
2156                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
2157                                 marker->cm_step, marker->cm_flags,
2158                                 marker->cm_tgtname, marker->cm_comment);
2159         } else {
2160                 int i;
2161
2162                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
2163                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
2164                                         lustre_cfg_string(lcfg, i));
2165                 }
2166         }
2167         ptr += snprintf(ptr, end - ptr, "\n");
2168         /* return consumed bytes */
2169         rc = ptr - buf;
2170         RETURN(rc);
2171 }
2172
2173 int class_config_dump_handler(const struct lu_env *env,
2174                               struct llog_handle *handle,
2175                               struct llog_rec_hdr *rec, void *data)
2176 {
2177         char *outstr;
2178         int rc = 0;
2179
2180         ENTRY;
2181
2182         OBD_ALLOC(outstr, 256);
2183         if (!outstr)
2184                 RETURN(-ENOMEM);
2185
2186         if (rec->lrh_type == OBD_CFG_REC) {
2187                 class_config_parse_rec(rec, outstr, 256);
2188                 LCONSOLE(D_WARNING, "   %s\n", outstr);
2189         } else {
2190                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
2191                 rc = -EINVAL;
2192         }
2193
2194         OBD_FREE(outstr, 256);
2195         RETURN(rc);
2196 }
2197
2198 /**
2199  * Call class_cleanup and class_detach.
2200  * "Manual" only in the sense that we're faking lcfg commands.
2201  */
2202 int class_manual_cleanup(struct obd_device *obd)
2203 {
2204         char flags[3] = "";
2205         struct lustre_cfg *lcfg;
2206         struct lustre_cfg_bufs bufs;
2207         int rc;
2208
2209         ENTRY;
2210
2211         if (!obd) {
2212                 CERROR("empty cleanup\n");
2213                 RETURN(-EALREADY);
2214         }
2215
2216         if (obd->obd_force)
2217                 strlcat(flags, "F", sizeof(flags));
2218         if (obd->obd_fail)
2219                 strlcat(flags, "A", sizeof(flags));
2220
2221         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
2222                obd->obd_name, flags);
2223
2224         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
2225         lustre_cfg_bufs_set_string(&bufs, 1, flags);
2226         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
2227         if (!lcfg)
2228                 RETURN(-ENOMEM);
2229         lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
2230
2231         rc = class_process_config(lcfg);
2232         if (rc) {
2233                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
2234                 GOTO(out, rc);
2235         }
2236
2237         /* the lcfg is almost the same for both ops */
2238         lcfg->lcfg_command = LCFG_DETACH;
2239         rc = class_process_config(lcfg);
2240         if (rc)
2241                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
2242 out:
2243         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
2244         RETURN(rc);
2245 }
2246 EXPORT_SYMBOL(class_manual_cleanup);
2247
2248 /*
2249  * uuid<->export lustre hash operations
2250  */
2251
2252 static unsigned
2253 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2254 {
2255         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2256                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
2257 }
2258
2259 static void *
2260 uuid_key(struct hlist_node *hnode)
2261 {
2262         struct obd_export *exp;
2263
2264         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2265
2266         return &exp->exp_client_uuid;
2267 }
2268
2269 /*
2270  * NOTE: It is impossible to find an export that is in failed
2271  *       state with this function
2272  */
2273 static int
2274 uuid_keycmp(const void *key, struct hlist_node *hnode)
2275 {
2276         struct obd_export *exp;
2277
2278         LASSERT(key);
2279         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2280
2281         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2282                !exp->exp_failed;
2283 }
2284
2285 static void *
2286 uuid_export_object(struct hlist_node *hnode)
2287 {
2288         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2289 }
2290
2291 static void
2292 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2293 {
2294         struct obd_export *exp;
2295
2296         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2297         class_export_get(exp);
2298 }
2299
2300 static void
2301 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2302 {
2303         struct obd_export *exp;
2304
2305         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2306         class_export_put(exp);
2307 }
2308
2309 static struct cfs_hash_ops uuid_hash_ops = {
2310         .hs_hash        = uuid_hash,
2311         .hs_key         = uuid_key,
2312         .hs_keycmp      = uuid_keycmp,
2313         .hs_object      = uuid_export_object,
2314         .hs_get         = uuid_export_get,
2315         .hs_put_locked  = uuid_export_put_locked,
2316 };
2317
2318
2319 /*
2320  * nid<->export hash operations
2321  */
2322
2323 static unsigned
2324 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2325 {
2326         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2327 }
2328
2329 static void *
2330 nid_key(struct hlist_node *hnode)
2331 {
2332         struct obd_export *exp;
2333
2334         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2335
2336         RETURN(&exp->exp_connection->c_peer.nid);
2337 }
2338
2339 /*
2340  * NOTE: It is impossible to find an export that is in failed
2341  *       state with this function
2342  */
2343 static int
2344 nid_kepcmp(const void *key, struct hlist_node *hnode)
2345 {
2346         struct obd_export *exp;
2347
2348         LASSERT(key);
2349         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2350
2351         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2352                !exp->exp_failed);
2353 }
2354
2355 static void *
2356 nid_export_object(struct hlist_node *hnode)
2357 {
2358         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2359 }
2360
2361 static void
2362 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2363 {
2364         struct obd_export *exp;
2365
2366         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2367         class_export_get(exp);
2368 }
2369
2370 static void
2371 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2372 {
2373         struct obd_export *exp;
2374
2375         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2376         class_export_put(exp);
2377 }
2378
2379 static struct cfs_hash_ops nid_hash_ops = {
2380         .hs_hash        = nid_hash,
2381         .hs_key         = nid_key,
2382         .hs_keycmp      = nid_kepcmp,
2383         .hs_object      = nid_export_object,
2384         .hs_get         = nid_export_get,
2385         .hs_put_locked  = nid_export_put_locked,
2386 };
2387
2388
2389 /*
2390  * nid<->nidstats hash operations
2391  */
2392
2393 static void *
2394 nidstats_key(struct hlist_node *hnode)
2395 {
2396         struct nid_stat *ns;
2397
2398         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2399
2400         return &ns->nid;
2401 }
2402
2403 static int
2404 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2405 {
2406         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2407 }
2408
2409 static void *
2410 nidstats_object(struct hlist_node *hnode)
2411 {
2412         return hlist_entry(hnode, struct nid_stat, nid_hash);
2413 }
2414
2415 static void
2416 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2417 {
2418         struct nid_stat *ns;
2419
2420         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2421         nidstat_getref(ns);
2422 }
2423
2424 static void
2425 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2426 {
2427         struct nid_stat *ns;
2428
2429         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2430         nidstat_putref(ns);
2431 }
2432
2433 static struct cfs_hash_ops nid_stat_hash_ops = {
2434         .hs_hash        = nid_hash,
2435         .hs_key         = nidstats_key,
2436         .hs_keycmp      = nidstats_keycmp,
2437         .hs_object      = nidstats_object,
2438         .hs_get         = nidstats_get,
2439         .hs_put_locked  = nidstats_put_locked,
2440 };
2441
2442
2443 /*
2444  * client_generation<->export hash operations
2445  */
2446
2447 static unsigned
2448 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2449 {
2450         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2451 }
2452
2453 static void *
2454 gen_key(struct hlist_node *hnode)
2455 {
2456         struct obd_export *exp;
2457
2458         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2459
2460         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2461 }
2462
2463 /*
2464  * NOTE: It is impossible to find an export that is in failed
2465  *       state with this function
2466  */
2467 static int
2468 gen_kepcmp(const void *key, struct hlist_node *hnode)
2469 {
2470         struct obd_export *exp;
2471
2472         LASSERT(key);
2473         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2474
2475         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2476                !exp->exp_failed);
2477 }
2478
2479 static void *
2480 gen_export_object(struct hlist_node *hnode)
2481 {
2482         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2483 }
2484
2485 static void
2486 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2487 {
2488         struct obd_export *exp;
2489
2490         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2491         class_export_get(exp);
2492 }
2493
2494 static void
2495 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2496 {
2497         struct obd_export *exp;
2498
2499         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2500         class_export_put(exp);
2501 }
2502
2503 static struct cfs_hash_ops gen_hash_ops = {
2504         .hs_hash        = gen_hash,
2505         .hs_key         = gen_key,
2506         .hs_keycmp      = gen_kepcmp,
2507         .hs_object      = gen_export_object,
2508         .hs_get         = gen_export_get,
2509         .hs_put_locked  = gen_export_put_locked,
2510 };