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