Whamcloud - gitweb
LU-11566 utils: improve usage/docs for lctl llog commands
[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[3];
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] = NULL;
1108
1109                 rc = kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
1110                 for (i = 0; i < ARRAY_SIZE(envp); i++)
1111                         kfree(envp[i]);
1112
1113                 kobject_put(kobj);
1114
1115                 RETURN(rc);
1116         }
1117
1118         /* Add upcall processing here. Now only lctl is supported */
1119         if (strcmp(upcall, LCTL_UPCALL) != 0) {
1120                 CERROR("Unsupported upcall %s\n", upcall);
1121                 RETURN(-EINVAL);
1122         }
1123
1124         start = ktime_get();
1125         rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1126         end = ktime_get();
1127
1128         if (rc < 0) {
1129                 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1130                        "time %ldus\n", argv[0], argv[1], argv[2], rc,
1131                        (long)ktime_us_delta(end, start));
1132         } else {
1133                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1134                        argv[0], argv[1], argv[2],
1135                        (long)ktime_us_delta(end, start));
1136                        rc = 0;
1137         }
1138
1139         RETURN(rc);
1140 }
1141
1142 #ifdef HAVE_SERVER_SUPPORT
1143 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1144 {
1145         quota_process_config = qpc;
1146 }
1147 EXPORT_SYMBOL(lustre_register_quota_process_config);
1148 #endif /* HAVE_SERVER_SUPPORT */
1149
1150 /**
1151  * Process configuration commands given in lustre_cfg form.
1152  * These may come from direct calls (e.g. class_manual_cleanup)
1153  * or processing the config llog, or ioctl from lctl.
1154  */
1155 int class_process_config(struct lustre_cfg *lcfg)
1156 {
1157         struct obd_device *obd;
1158         int err;
1159
1160         LASSERT(lcfg && !IS_ERR(lcfg));
1161         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1162
1163         /* Commands that don't need a device */
1164         switch (lcfg->lcfg_command) {
1165         case LCFG_ATTACH: {
1166                 err = class_attach(lcfg);
1167                 GOTO(out, err);
1168         }
1169         case LCFG_ADD_UUID: {
1170                 CDEBUG(D_IOCTL,
1171                        "adding mapping from uuid %s to nid %#llx (%s)\n",
1172                        lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid,
1173                        libcfs_nid2str(lcfg->lcfg_nid));
1174
1175                 err = class_add_uuid(lustre_cfg_string(lcfg, 1),
1176                                      lcfg->lcfg_nid);
1177                 GOTO(out, err);
1178         }
1179         case LCFG_DEL_UUID: {
1180                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1181                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) ==
1182                         0) ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1183
1184                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1185                 GOTO(out, err);
1186         }
1187         case LCFG_MOUNTOPT: {
1188                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1189                        lustre_cfg_string(lcfg, 1),
1190                        lustre_cfg_string(lcfg, 2),
1191                        lustre_cfg_string(lcfg, 3));
1192                 /*
1193                  * set these mount options somewhere, so ll_fill_super
1194                  * can find them.
1195                  */
1196                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1197                                         lustre_cfg_string(lcfg, 1),
1198                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1199                                         lustre_cfg_string(lcfg, 2),
1200                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1201                                         lustre_cfg_string(lcfg, 3));
1202                 GOTO(out, err);
1203         }
1204         case LCFG_DEL_MOUNTOPT: {
1205                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1206                        lustre_cfg_string(lcfg, 1));
1207                 class_del_profile(lustre_cfg_string(lcfg, 1));
1208                 GOTO(out, err = 0);
1209         }
1210         case LCFG_SET_TIMEOUT: {
1211                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1212                        obd_timeout, lcfg->lcfg_num);
1213                 obd_timeout = max(lcfg->lcfg_num, 1U);
1214                 obd_timeout_set = 1;
1215                 GOTO(out, err = 0);
1216         }
1217         case LCFG_SET_LDLM_TIMEOUT: {
1218                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1219                        ldlm_timeout, lcfg->lcfg_num);
1220                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1221                 if (ldlm_timeout >= obd_timeout)
1222                         ldlm_timeout = max(obd_timeout / 3, 1U);
1223                 ldlm_timeout_set = 1;
1224                 GOTO(out, err = 0);
1225         }
1226         case LCFG_SET_UPCALL: {
1227                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1228                 /* COMPAT_146 Don't fail on old configs */
1229                 GOTO(out, err = 0);
1230         }
1231         case LCFG_MARKER: {
1232                 struct cfg_marker *marker;
1233
1234                 marker = lustre_cfg_buf(lcfg, 1);
1235                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1236                        marker->cm_flags, marker->cm_tgtname,
1237                        marker->cm_comment);
1238                 GOTO(out, err = 0);
1239         }
1240         case LCFG_PARAM: {
1241                 char *tmp;
1242
1243                 /* llite has no OBD */
1244                 if (class_match_param(lustre_cfg_string(lcfg, 1),
1245                                       PARAM_LLITE, NULL) == 0) {
1246                         struct lustre_sb_info *lsi;
1247                         unsigned long addr;
1248                         ssize_t count;
1249
1250                         /*
1251                          * The instance name contains the sb:
1252                          * lustre-client-aacfe000
1253                          */
1254                         tmp = strrchr(lustre_cfg_string(lcfg, 0), '-');
1255                         if (!tmp || !*(++tmp))
1256                                 GOTO(out, err = -EINVAL);
1257
1258                         if (sscanf(tmp, "%lx", &addr) != 1)
1259                                 GOTO(out, err = -EINVAL);
1260
1261                         lsi = s2lsi((struct super_block *)addr);
1262                         /* This better be a real Lustre superblock! */
1263                         LASSERT(lsi->lsi_lmd->lmd_magic == LMD_MAGIC);
1264
1265                         count = class_modify_config(lcfg, PARAM_LLITE,
1266                                                     lsi->lsi_kobj);
1267                         err = count < 0 ? count : 0;
1268                         GOTO(out, err);
1269                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1270                                               PARAM_SYS, &tmp) == 0)) {
1271                         /* Global param settings */
1272                         err = class_set_global(tmp);
1273                         /*
1274                          * Client or server should not fail to mount if
1275                          * it hits an unknown configuration parameter.
1276                          */
1277                         if (err < 0)
1278                                 CWARN("Ignoring unknown param %s\n", tmp);
1279
1280                         GOTO(out, err = 0);
1281 #ifdef HAVE_SERVER_SUPPORT
1282                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1283                                               PARAM_QUOTA, &tmp) == 0) &&
1284                            quota_process_config) {
1285                         err = (*quota_process_config)(lcfg);
1286                         GOTO(out, err);
1287 #endif /* HAVE_SERVER_SUPPORT */
1288                 }
1289
1290                 break;
1291         }
1292         case LCFG_SET_PARAM: {
1293                 err = process_param2_config(lcfg);
1294                 GOTO(out, err = 0);
1295         }
1296         }
1297         /* Commands that require a device */
1298         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1299         if (!obd) {
1300                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1301                         CERROR("this lcfg command requires a device name\n");
1302                 else
1303                         CERROR("no device for: %s\n",
1304                                lustre_cfg_string(lcfg, 0));
1305
1306                 GOTO(out, err = -EINVAL);
1307         }
1308         switch(lcfg->lcfg_command) {
1309         case LCFG_SETUP: {
1310                 err = class_setup(obd, lcfg);
1311                 GOTO(out, err);
1312         }
1313         case LCFG_DETACH: {
1314                 err = class_detach(obd, lcfg);
1315                 GOTO(out, err = 0);
1316         }
1317         case LCFG_CLEANUP: {
1318                 err = class_cleanup(obd, lcfg);
1319                 GOTO(out, err = 0);
1320         }
1321         case LCFG_ADD_CONN: {
1322                 err = class_add_conn(obd, lcfg);
1323                 GOTO(out, err = 0);
1324         }
1325         case LCFG_DEL_CONN: {
1326                 err = class_del_conn(obd, lcfg);
1327                 GOTO(out, err = 0);
1328         }
1329         case LCFG_POOL_NEW: {
1330                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1331                 GOTO(out, err = 0);
1332         }
1333         case LCFG_POOL_ADD: {
1334                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1335                                    lustre_cfg_string(lcfg, 3));
1336                 GOTO(out, err = 0);
1337         }
1338         case LCFG_POOL_REM: {
1339                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1340                                    lustre_cfg_string(lcfg, 3));
1341                 GOTO(out, err = 0);
1342         }
1343         case LCFG_POOL_DEL: {
1344                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1345                 GOTO(out, err = 0);
1346         }
1347         /*
1348          * Process config log ADD_MDC record twice to add MDC also to LOV
1349          * for Data-on-MDT:
1350          *
1351          * add 0:lustre-clilmv 1:lustre-MDT0000_UUID 2:0 3:1
1352          *     4:lustre-MDT0000-mdc_UUID
1353          */
1354         case LCFG_ADD_MDC: {
1355                 struct obd_device *lov_obd;
1356                 char *clilmv;
1357
1358                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1359                 if (err)
1360                         GOTO(out, err);
1361
1362                 /* make sure this is client LMV log entry */
1363                 clilmv = strstr(lustre_cfg_string(lcfg, 0), "clilmv");
1364                 if (!clilmv)
1365                         GOTO(out, err);
1366
1367                 /*
1368                  * replace 'lmv' with 'lov' name to address LOV device and
1369                  * process llog record to add MDC there.
1370                  */
1371                 clilmv[4] = 'o';
1372                 lov_obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1373                 if (lov_obd) {
1374                         err = obd_process_config(lov_obd, sizeof(*lcfg), lcfg);
1375                 } else {
1376                         err = -ENOENT;
1377                         CERROR("%s: Cannot find LOV by %s name, rc = %d\n",
1378                                obd->obd_name, lustre_cfg_string(lcfg, 0), err);
1379                 }
1380                 /* restore 'lmv' name */
1381                 clilmv[4] = 'm';
1382                 GOTO(out, err);
1383         }
1384         default: {
1385                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1386                 GOTO(out, err);
1387         }
1388         }
1389         EXIT;
1390 out:
1391         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1392                 CWARN("Ignoring error %d on optional command %#x\n", err,
1393                       lcfg->lcfg_command);
1394                 err = 0;
1395         }
1396         return err;
1397 }
1398 EXPORT_SYMBOL(class_process_config);
1399
1400 ssize_t class_modify_config(struct lustre_cfg *lcfg, const char *prefix,
1401                             struct kobject *kobj)
1402 {
1403         struct kobj_type *typ;
1404         ssize_t count = 0;
1405         int i;
1406
1407         if (lcfg->lcfg_command != LCFG_PARAM) {
1408                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1409                 return -EINVAL;
1410         }
1411
1412         typ = get_ktype(kobj);
1413         if (!typ || !typ->default_attrs)
1414                 return -ENODEV;
1415
1416         print_lustre_cfg(lcfg);
1417
1418         /*
1419          * e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1420          * or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1421          * or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1422          */
1423         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1424                 struct attribute *attr;
1425                 size_t keylen;
1426                 char *value;
1427                 char *key;
1428                 int j;
1429
1430                 key = lustre_cfg_buf(lcfg, i);
1431                 /* Strip off prefix */
1432                 if (class_match_param(key, prefix, &key))
1433                         /*
1434                          * If the prefix doesn't match, return error so we
1435                          * can pass it down the stack
1436                          */
1437                         return -EINVAL;
1438
1439                 value = strchr(key, '=');
1440                 if (!value || *(value + 1) == 0) {
1441                         CERROR("%s: can't parse param '%s' (missing '=')\n",
1442                                lustre_cfg_string(lcfg, 0),
1443                                lustre_cfg_string(lcfg, i));
1444                         /* continue parsing other params */
1445                         continue;
1446                 }
1447                 keylen = value - key;
1448                 value++;
1449
1450                 attr = NULL;
1451                 for (j = 0; typ->default_attrs[j]; j++) {
1452                         if (!strncmp(typ->default_attrs[j]->name, key,
1453                                      keylen)) {
1454                                 attr = typ->default_attrs[j];
1455                                 break;
1456                         }
1457                 }
1458
1459                 if (!attr) {
1460                         char *envp[3];
1461
1462                         envp[0] = kasprintf(GFP_KERNEL, "PARAM=%s.%s.%.*s",
1463                                             kobject_name(kobj->parent),
1464                                             kobject_name(kobj),
1465                                             (int) keylen, key);
1466                         envp[1] = kasprintf(GFP_KERNEL, "SETTING=%s", value);
1467                         envp[2] = NULL;
1468
1469                         if (kobject_uevent_env(kobj, KOBJ_CHANGE, envp)) {
1470                                 CERROR("%s: failed to send uevent %s\n",
1471                                        kobject_name(kobj), key);
1472                         }
1473
1474                         for (i = 0; i < ARRAY_SIZE(envp); i++)
1475                                 kfree(envp[i]);
1476                 } else {
1477                         count += lustre_attr_store(kobj, attr, value,
1478                                                    strlen(value));
1479                 }
1480         }
1481         return count;
1482 }
1483 EXPORT_SYMBOL(class_modify_config);
1484
1485 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1486                              struct lustre_cfg *lcfg, void *data)
1487 {
1488         struct lprocfs_vars *var;
1489         struct file fakefile;
1490         struct seq_file fake_seqfile;
1491         char *key, *sval;
1492         int i, keylen, vallen;
1493         int matched = 0, j = 0;
1494         int rc = 0;
1495         int skip = 0;
1496
1497         ENTRY;
1498
1499         if (lcfg->lcfg_command != LCFG_PARAM) {
1500                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1501                 RETURN(-EINVAL);
1502         }
1503
1504         /* fake a seq file so that var->fops->write can work... */
1505         fakefile.private_data = &fake_seqfile;
1506         fake_seqfile.private = data;
1507         /*
1508          * e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1509          * or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1510          * or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1511          */
1512         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1513                 key = lustre_cfg_buf(lcfg, i);
1514                 /* Strip off prefix */
1515                 if (class_match_param(key, prefix, &key))
1516                         /*
1517                          * If the prefix doesn't match, return error so we
1518                          * can pass it down the stack
1519                          */
1520                         RETURN(-ENOSYS);
1521                 sval = strchr(key, '=');
1522                 if (!sval || *(sval + 1) == 0) {
1523                         CERROR("%s: can't parse param '%s' (missing '=')\n",
1524                                lustre_cfg_string(lcfg, 0),
1525                                lustre_cfg_string(lcfg, i));
1526                         /* rc = -EINVAL;        continue parsing other params */
1527                         continue;
1528                 }
1529                 keylen = sval - key;
1530                 sval++;
1531                 vallen = strlen(sval);
1532                 matched = 0;
1533                 j = 0;
1534                 /* Search proc entries */
1535                 while (lvars[j].name) {
1536                         var = &lvars[j];
1537                         if (class_match_param(key, var->name, NULL) == 0 &&
1538                             keylen == strlen(var->name)) {
1539                                 matched++;
1540                                 rc = -EROFS;
1541
1542                                 if (var->fops && var->fops->write) {
1543                                         mm_segment_t oldfs;
1544                                         oldfs = get_fs();
1545                                         set_fs(KERNEL_DS);
1546                                         rc = (var->fops->write)(&fakefile, sval,
1547                                                                 vallen, NULL);
1548                                         set_fs(oldfs);
1549                                 }
1550                                 break;
1551                         }
1552                         j++;
1553                 }
1554                 if (!matched) {
1555                         /*
1556                          * It was upgraded from old MDT/OST device,
1557                          * ignore the obsolete "sec_level" parameter.
1558                          */
1559                         if (strncmp("sec_level", key, keylen) == 0)
1560                                 continue;
1561
1562                         CERROR("%s: unknown config parameter '%s'\n",
1563                                lustre_cfg_string(lcfg, 0),
1564                                lustre_cfg_string(lcfg, i));
1565                         /* rc = -EINVAL;        continue parsing other params */
1566                         skip++;
1567                 } else if (rc < 0) {
1568                         CERROR("%s: error writing parameter '%s': rc = %d\n",
1569                                lustre_cfg_string(lcfg, 0), key, rc);
1570                         rc = 0;
1571                 } else {
1572                         CDEBUG(D_CONFIG, "%s: set parameter '%s'\n",
1573                                lustre_cfg_string(lcfg, 0), key);
1574                 }
1575         }
1576
1577         if (rc > 0)
1578                 rc = 0;
1579         if (!rc && skip)
1580                 rc = skip;
1581         RETURN(rc);
1582 }
1583 EXPORT_SYMBOL(class_process_proc_param);
1584
1585 /*
1586  * Supplemental functions for config logs, it allocates lustre_cfg
1587  * buffers plus initialized llog record header at the beginning.
1588  */
1589 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1590 {
1591         struct llog_cfg_rec *lcr;
1592         int reclen;
1593
1594         ENTRY;
1595
1596         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1597         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1598                  sizeof(struct llog_rec_tail);
1599
1600         OBD_ALLOC(lcr, reclen);
1601         if (!lcr)
1602                 RETURN(NULL);
1603
1604         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1605
1606         lcr->lcr_hdr.lrh_len = reclen;
1607         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1608
1609         RETURN(lcr);
1610 }
1611 EXPORT_SYMBOL(lustre_cfg_rec_new);
1612
1613 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1614 {
1615         ENTRY;
1616         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1617         EXIT;
1618 }
1619 EXPORT_SYMBOL(lustre_cfg_rec_free);
1620
1621 /**
1622  * Parse a configuration llog, doing various manipulations on them
1623  * for various reasons, (modifications for compatibility, skip obsolete
1624  * records, change uuids, etc), then class_process_config() resulting
1625  * net records.
1626  */
1627 int class_config_llog_handler(const struct lu_env *env,
1628                               struct llog_handle *handle,
1629                               struct llog_rec_hdr *rec, void *data)
1630 {
1631         struct config_llog_instance *cfg = data;
1632         int cfg_len = rec->lrh_len;
1633         char *cfg_buf = (char *) (rec + 1);
1634         int rc = 0;
1635         ENTRY;
1636
1637         /* class_config_dump_handler(handle, rec, data); */
1638
1639         switch (rec->lrh_type) {
1640         case OBD_CFG_REC: {
1641                 struct lustre_cfg *lcfg, *lcfg_new;
1642                 struct lustre_cfg_bufs bufs;
1643                 char *inst_name = NULL;
1644                 int inst_len = 0;
1645                 int swab = 0;
1646
1647                 lcfg = (struct lustre_cfg *)cfg_buf;
1648                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1649                         lustre_swab_lustre_cfg(lcfg);
1650                         swab = 1;
1651                 }
1652
1653                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1654                 if (rc)
1655                         GOTO(out, rc);
1656
1657                 /* Figure out config state info */
1658                 if (lcfg->lcfg_command == LCFG_MARKER) {
1659                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1660                         lustre_swab_cfg_marker(marker, swab,
1661                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1662                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1663                                cfg->cfg_flags, marker->cm_flags);
1664                         if (marker->cm_flags & CM_START) {
1665                                 /* all previous flags off */
1666                                 cfg->cfg_flags = CFG_F_MARKER;
1667                                 server_name2index(marker->cm_tgtname,
1668                                                   &cfg->cfg_lwp_idx, NULL);
1669                                 if (marker->cm_flags & CM_SKIP) {
1670                                         cfg->cfg_flags |= CFG_F_SKIP;
1671                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1672                                                marker->cm_step);
1673                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1674                                            (cfg->cfg_sb &&
1675                                            lustre_check_exclusion(cfg->cfg_sb,
1676                                                         marker->cm_tgtname))) {
1677                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1678                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1679                                                marker->cm_step);
1680                                 }
1681                         } else if (marker->cm_flags & CM_END) {
1682                                 cfg->cfg_flags = 0;
1683                         }
1684                 }
1685                 /*
1686                  * A config command without a start marker before it is
1687                  * illegal
1688                  */
1689                 if (!(cfg->cfg_flags & CFG_F_MARKER) &&
1690                     (lcfg->lcfg_command != LCFG_MARKER)) {
1691                         CWARN("Skip config outside markers, (inst: %016lx, uuid: %s, flags: %#x)\n",
1692                                 cfg->cfg_instance,
1693                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1694                         cfg->cfg_flags |= CFG_F_SKIP;
1695                 }
1696                 if (cfg->cfg_flags & CFG_F_SKIP) {
1697                         CDEBUG(D_CONFIG, "skipping %#x\n",
1698                                cfg->cfg_flags);
1699                         rc = 0;
1700                         /* No processing! */
1701                         break;
1702                 }
1703
1704                 /*
1705                  * For interoperability between 1.8 and 2.0,
1706                  * rename "mds" OBD device type to "mdt".
1707                  */
1708                 {
1709                         char *typename = lustre_cfg_string(lcfg, 1);
1710                         char *index = lustre_cfg_string(lcfg, 2);
1711
1712                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1713                             strcmp(typename, "mds") == 0)) {
1714                                 CWARN("For 1.8 interoperability, rename obd "
1715                                         "type from mds to mdt\n");
1716                                 typename[2] = 't';
1717                         }
1718                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1719                             strcmp(index, "type") == 0)) {
1720                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1721                                        "set this index to '0'\n");
1722                                 index[0] = '0';
1723                                 index[1] = 0;
1724                         }
1725                 }
1726
1727 #ifdef HAVE_SERVER_SUPPORT
1728                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1729                 {
1730                         char *typename = lustre_cfg_string(lcfg, 1);
1731
1732                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1733                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1734                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1735                                 CDEBUG(D_CONFIG,
1736                                        "For 2.x interoperability, rename obd "
1737                                        "type from lov to lod (%s)\n",
1738                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1739                                 strcpy(typename, LUSTRE_LOD_NAME);
1740                         }
1741                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1742                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1743                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1744                                 CDEBUG(D_CONFIG,
1745                                        "For 2.x interoperability, rename obd "
1746                                        "type from osc to osp (%s)\n",
1747                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1748                                 strcpy(typename, LUSTRE_OSP_NAME);
1749                         }
1750                 }
1751 #endif /* HAVE_SERVER_SUPPORT */
1752
1753                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1754                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1755                                lcfg->lcfg_command);
1756                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1757                                 /* Add inactive instead */
1758                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1759                 }
1760
1761                 lustre_cfg_bufs_reset(&bufs, NULL);
1762                 lustre_cfg_bufs_init(&bufs, lcfg);
1763
1764                 if (cfg->cfg_instance &&
1765                     lcfg->lcfg_command != LCFG_SPTLRPC_CONF &&
1766                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1767                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) + 16 + 4;
1768                         OBD_ALLOC(inst_name, inst_len);
1769                         if (!inst_name)
1770                                 GOTO(out, rc = -ENOMEM);
1771                         snprintf(inst_name, inst_len, "%s-%016lx",
1772                                 lustre_cfg_string(lcfg, 0),
1773                                 cfg->cfg_instance);
1774                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1775                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1776                                lcfg->lcfg_command, inst_name);
1777                 }
1778
1779                 /* override llog UUID for clients, to insure they are unique */
1780                 if (cfg->cfg_instance && lcfg->lcfg_command == LCFG_ATTACH)
1781                         lustre_cfg_bufs_set_string(&bufs, 2,
1782                                                    cfg->cfg_uuid.uuid);
1783                 /*
1784                  * sptlrpc config record, we expect 2 data segments:
1785                  *  [0]: fs_name/target_name,
1786                  *  [1]: rule string
1787                  * moving them to index [1] and [2], and insert MGC's
1788                  * obdname at index [0].
1789                  */
1790                 if (cfg->cfg_instance &&
1791                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1792                         /* After ASLR changes cfg_instance this needs fixing */
1793                         /* "obd" is set in config_log_find_or_add() */
1794                         struct obd_device *obd = (void *)cfg->cfg_instance;
1795
1796                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1797                                             bufs.lcfg_buflen[1]);
1798                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1799                                             bufs.lcfg_buflen[0]);
1800                         lustre_cfg_bufs_set_string(&bufs, 0,
1801                                                    obd->obd_name);
1802                 }
1803
1804                 /*
1805                  * Add net info to setup command
1806                  * if given on command line.
1807                  * So config log will be:
1808                  * [0]: client name
1809                  * [1]: client UUID
1810                  * [2]: server UUID
1811                  * [3]: inactive-on-startup
1812                  * [4]: restrictive net
1813                  */
1814                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1815                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1816                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1817                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1818
1819                         if (lcfg->lcfg_command == LCFG_SETUP &&
1820                             lcfg->lcfg_bufcount != 2 && nidnet) {
1821                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1822                                        "command for client %s\n", nidnet,
1823                                        lustre_cfg_string(lcfg, 0));
1824                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1825                         }
1826                 }
1827
1828                 /*
1829                  * Skip add_conn command if uuid is
1830                  * not on restricted net
1831                  */
1832                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1833                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1834                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1835                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1836
1837                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1838                             lsi->lsi_lmd->lmd_nidnet &&
1839                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1840                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1841                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1842                                        uuid_str);
1843                                 rc = 0;
1844                                 /* No processing! */
1845                                 break;
1846                         }
1847                 }
1848
1849                 OBD_ALLOC(lcfg_new, lustre_cfg_len(bufs.lcfg_bufcount,
1850                                                    bufs.lcfg_buflen));
1851                 if (!lcfg_new)
1852                         GOTO(out, rc = -ENOMEM);
1853
1854                 lustre_cfg_init(lcfg_new, lcfg->lcfg_command, &bufs);
1855                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1856                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1857
1858                 /*
1859                  * XXX Hack to try to remain binary compatible with
1860                  * pre-newconfig logs
1861                  */
1862                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1863                     (lcfg->lcfg_nid >> 32) == 0) {
1864                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1865
1866                         lcfg_new->lcfg_nid =
1867                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1868                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1869                               lcfg->lcfg_nal, addr,
1870                               libcfs_nid2str(lcfg_new->lcfg_nid));
1871                 } else {
1872                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1873                 }
1874
1875                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1876
1877                 rc = class_process_config(lcfg_new);
1878                 OBD_FREE(lcfg_new, lustre_cfg_len(lcfg_new->lcfg_bufcount,
1879                                                   lcfg_new->lcfg_buflens));
1880                 if (inst_name)
1881                         OBD_FREE(inst_name, inst_len);
1882                 break;
1883         }
1884         default:
1885                 CERROR("Unknown llog record type %#x encountered\n",
1886                        rec->lrh_type);
1887                 break;
1888         }
1889 out:
1890         if (rc) {
1891                 CERROR("%s: cfg command failed: rc = %d\n",
1892                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1893                 class_config_dump_handler(NULL, handle, rec, data);
1894         }
1895         RETURN(rc);
1896 }
1897 EXPORT_SYMBOL(class_config_llog_handler);
1898
1899 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1900                             char *name, struct config_llog_instance *cfg)
1901 {
1902         struct llog_process_cat_data cd = {
1903                 .lpcd_first_idx = 0,
1904         };
1905         struct llog_handle *llh;
1906         llog_cb_t callback;
1907         int rc;
1908         ENTRY;
1909
1910         CDEBUG(D_INFO, "looking up llog %s\n", name);
1911         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1912         if (rc)
1913                 RETURN(rc);
1914
1915         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1916         if (rc)
1917                 GOTO(parse_out, rc);
1918
1919         /* continue processing from where we last stopped to end-of-log */
1920         if (cfg) {
1921                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1922                 callback = cfg->cfg_callback;
1923                 LASSERT(callback != NULL);
1924         } else {
1925                 callback = class_config_llog_handler;
1926         }
1927
1928         cd.lpcd_last_idx = 0;
1929
1930         rc = llog_process(env, llh, callback, cfg, &cd);
1931
1932         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1933                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1934         if (cfg)
1935                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1936
1937 parse_out:
1938         llog_close(env, llh);
1939         RETURN(rc);
1940 }
1941 EXPORT_SYMBOL(class_config_parse_llog);
1942
1943 static struct lcfg_type_data {
1944         __u32    ltd_type;
1945         char    *ltd_name;
1946         char    *ltd_bufs[4];
1947 } lcfg_data_table[] = {
1948         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1949         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1950         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1951         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1952         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1953         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1954         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1955         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1956         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1957         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1958         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1959         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1960         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1961         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1962         { LCFG_PARAM, "conf_param", { "parameter", "value", "3", "4" } },
1963         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1964         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1965         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1966         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1967         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1968         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1969         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1970         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1971         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1972         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1973         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1974         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1975           { "parameter", "2", "3", "4" } },
1976         { LCFG_SET_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1977         { 0, NULL, { NULL, NULL, NULL, NULL } }
1978 };
1979
1980 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1981 {
1982         int i = 0;
1983
1984         while (lcfg_data_table[i].ltd_type != 0) {
1985                 if (lcfg_data_table[i].ltd_type == cmd)
1986                         return &lcfg_data_table[i];
1987                 i++;
1988         }
1989         return NULL;
1990 }
1991
1992 /**
1993  * Parse config record and output dump in supplied buffer.
1994  *
1995  * This is separated from class_config_dump_handler() to use
1996  * for ioctl needs as well
1997  *
1998  * Sample Output:
1999  * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
2000  *     UUID: lustrewt-clilov_UUID }
2001  */
2002 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
2003 {
2004         struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
2005         char *ptr = buf;
2006         char *end = buf + size;
2007         int rc = 0, i;
2008         struct lcfg_type_data *ldata;
2009
2010         LASSERT(rec->lrh_type == OBD_CFG_REC);
2011         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2012         if (rc < 0)
2013                 return rc;
2014
2015         ldata = lcfg_cmd2data(lcfg->lcfg_command);
2016         if (!ldata)
2017                 return -ENOTTY;
2018
2019         if (lcfg->lcfg_command == LCFG_MARKER)
2020                 return 0;
2021
2022         /* form YAML entity */
2023         ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
2024                         rec->lrh_index, ldata->ltd_name);
2025         if (end - ptr <= 0)
2026                 goto out_overflow;
2027
2028         if (lcfg->lcfg_flags) {
2029                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
2030                                 lcfg->lcfg_flags);
2031                 if (end - ptr <= 0)
2032                         goto out_overflow;
2033         }
2034         if (lcfg->lcfg_num) {
2035                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
2036                                 lcfg->lcfg_num);
2037                 if (end - ptr <= 0)
2038                         goto out_overflow;
2039         }
2040         if (lcfg->lcfg_nid) {
2041                 char nidstr[LNET_NIDSTR_SIZE];
2042
2043                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2044                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
2045                                 nidstr, lcfg->lcfg_nid);
2046                 if (end - ptr <= 0)
2047                         goto out_overflow;
2048         }
2049
2050         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
2051                 ptr += snprintf(ptr, end - ptr, ", device: %s",
2052                                 lustre_cfg_string(lcfg, 0));
2053                 if (end - ptr <= 0)
2054                         goto out_overflow;
2055         }
2056
2057         if (lcfg->lcfg_command == LCFG_SET_PARAM) {
2058                 /*
2059                  * set_param -P parameters have param=val here, separate
2060                  * them through pointer magic and print them out in
2061                  * native yamlese
2062                  */
2063                 char *cfg_str = lustre_cfg_string(lcfg, 1);
2064                 char *tmp = strchr(cfg_str, '=');
2065                 size_t len;
2066
2067                 if (!tmp)
2068                         goto out_done;
2069
2070                 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[0]);
2071                 len = tmp - cfg_str + 1;
2072                 snprintf(ptr, len, "%s", cfg_str);
2073                 ptr += len - 1;
2074
2075                 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[1]);
2076                 ptr += snprintf(ptr, end - ptr, "%s", tmp + 1);
2077
2078                 goto out_done;
2079         }
2080
2081         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
2082                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0) {
2083                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
2084                                         ldata->ltd_bufs[i - 1],
2085                                         lustre_cfg_string(lcfg, i));
2086                         if (end - ptr <= 0)
2087                                 goto out_overflow;
2088                 }
2089         }
2090
2091 out_done:
2092         ptr += snprintf(ptr, end - ptr, " }\n");
2093 out_overflow:
2094         /* Return consumed bytes.  If the buffer overflowed, zero last byte */
2095         rc = ptr - buf;
2096         if (rc > size) {
2097                 rc = -EOVERFLOW;
2098                 *(end - 1) = '\0';
2099         }
2100
2101         return rc;
2102 }
2103
2104 /**
2105  * parse config record and output dump in supplied buffer.
2106  * This is separated from class_config_dump_handler() to use
2107  * for ioctl needs as well
2108  */
2109 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
2110 {
2111         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
2112         char                    *ptr = buf;
2113         char                    *end = buf + size;
2114         int                      rc = 0;
2115
2116         ENTRY;
2117
2118         LASSERT(rec->lrh_type == OBD_CFG_REC);
2119         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2120         if (rc < 0)
2121                 RETURN(rc);
2122
2123         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
2124         if (lcfg->lcfg_flags)
2125                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
2126                                 lcfg->lcfg_flags);
2127
2128         if (lcfg->lcfg_num)
2129                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
2130
2131         if (lcfg->lcfg_nid) {
2132                 char nidstr[LNET_NIDSTR_SIZE];
2133
2134                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2135                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)    ",
2136                                 nidstr, lcfg->lcfg_nid);
2137         }
2138
2139         if (lcfg->lcfg_command == LCFG_MARKER) {
2140                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
2141
2142                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
2143                                 marker->cm_step, marker->cm_flags,
2144                                 marker->cm_tgtname, marker->cm_comment);
2145         } else {
2146                 int i;
2147
2148                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
2149                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
2150                                         lustre_cfg_string(lcfg, i));
2151                 }
2152         }
2153         ptr += snprintf(ptr, end - ptr, "\n");
2154         /* return consumed bytes */
2155         rc = ptr - buf;
2156         RETURN(rc);
2157 }
2158
2159 int class_config_dump_handler(const struct lu_env *env,
2160                               struct llog_handle *handle,
2161                               struct llog_rec_hdr *rec, void *data)
2162 {
2163         char *outstr;
2164         int rc = 0;
2165
2166         ENTRY;
2167
2168         OBD_ALLOC(outstr, 256);
2169         if (!outstr)
2170                 RETURN(-ENOMEM);
2171
2172         if (rec->lrh_type == OBD_CFG_REC) {
2173                 class_config_parse_rec(rec, outstr, 256);
2174                 LCONSOLE(D_WARNING, "   %s\n", outstr);
2175         } else {
2176                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
2177                 rc = -EINVAL;
2178         }
2179
2180         OBD_FREE(outstr, 256);
2181         RETURN(rc);
2182 }
2183
2184 /**
2185  * Call class_cleanup and class_detach.
2186  * "Manual" only in the sense that we're faking lcfg commands.
2187  */
2188 int class_manual_cleanup(struct obd_device *obd)
2189 {
2190         char flags[3] = "";
2191         struct lustre_cfg *lcfg;
2192         struct lustre_cfg_bufs bufs;
2193         int rc;
2194
2195         ENTRY;
2196
2197         if (!obd) {
2198                 CERROR("empty cleanup\n");
2199                 RETURN(-EALREADY);
2200         }
2201
2202         if (obd->obd_force)
2203                 strlcat(flags, "F", sizeof(flags));
2204         if (obd->obd_fail)
2205                 strlcat(flags, "A", sizeof(flags));
2206
2207         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
2208                obd->obd_name, flags);
2209
2210         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
2211         lustre_cfg_bufs_set_string(&bufs, 1, flags);
2212         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
2213         if (!lcfg)
2214                 RETURN(-ENOMEM);
2215         lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
2216
2217         rc = class_process_config(lcfg);
2218         if (rc) {
2219                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
2220                 GOTO(out, rc);
2221         }
2222
2223         /* the lcfg is almost the same for both ops */
2224         lcfg->lcfg_command = LCFG_DETACH;
2225         rc = class_process_config(lcfg);
2226         if (rc)
2227                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
2228 out:
2229         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
2230         RETURN(rc);
2231 }
2232 EXPORT_SYMBOL(class_manual_cleanup);
2233
2234 /*
2235  * uuid<->export lustre hash operations
2236  */
2237
2238 static unsigned
2239 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2240 {
2241         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2242                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
2243 }
2244
2245 static void *
2246 uuid_key(struct hlist_node *hnode)
2247 {
2248         struct obd_export *exp;
2249
2250         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2251
2252         return &exp->exp_client_uuid;
2253 }
2254
2255 /*
2256  * NOTE: It is impossible to find an export that is in failed
2257  *       state with this function
2258  */
2259 static int
2260 uuid_keycmp(const void *key, struct hlist_node *hnode)
2261 {
2262         struct obd_export *exp;
2263
2264         LASSERT(key);
2265         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2266
2267         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2268                !exp->exp_failed;
2269 }
2270
2271 static void *
2272 uuid_export_object(struct hlist_node *hnode)
2273 {
2274         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2275 }
2276
2277 static void
2278 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2279 {
2280         struct obd_export *exp;
2281
2282         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2283         class_export_get(exp);
2284 }
2285
2286 static void
2287 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2288 {
2289         struct obd_export *exp;
2290
2291         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2292         class_export_put(exp);
2293 }
2294
2295 static struct cfs_hash_ops uuid_hash_ops = {
2296         .hs_hash        = uuid_hash,
2297         .hs_key         = uuid_key,
2298         .hs_keycmp      = uuid_keycmp,
2299         .hs_object      = uuid_export_object,
2300         .hs_get         = uuid_export_get,
2301         .hs_put_locked  = uuid_export_put_locked,
2302 };
2303
2304
2305 /*
2306  * nid<->export hash operations
2307  */
2308
2309 static unsigned
2310 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2311 {
2312         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2313 }
2314
2315 static void *
2316 nid_key(struct hlist_node *hnode)
2317 {
2318         struct obd_export *exp;
2319
2320         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2321
2322         RETURN(&exp->exp_connection->c_peer.nid);
2323 }
2324
2325 /*
2326  * NOTE: It is impossible to find an export that is in failed
2327  *       state with this function
2328  */
2329 static int
2330 nid_kepcmp(const void *key, struct hlist_node *hnode)
2331 {
2332         struct obd_export *exp;
2333
2334         LASSERT(key);
2335         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2336
2337         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2338                !exp->exp_failed);
2339 }
2340
2341 static void *
2342 nid_export_object(struct hlist_node *hnode)
2343 {
2344         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2345 }
2346
2347 static void
2348 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2349 {
2350         struct obd_export *exp;
2351
2352         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2353         class_export_get(exp);
2354 }
2355
2356 static void
2357 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2358 {
2359         struct obd_export *exp;
2360
2361         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2362         class_export_put(exp);
2363 }
2364
2365 static struct cfs_hash_ops nid_hash_ops = {
2366         .hs_hash        = nid_hash,
2367         .hs_key         = nid_key,
2368         .hs_keycmp      = nid_kepcmp,
2369         .hs_object      = nid_export_object,
2370         .hs_get         = nid_export_get,
2371         .hs_put_locked  = nid_export_put_locked,
2372 };
2373
2374
2375 /*
2376  * nid<->nidstats hash operations
2377  */
2378
2379 static void *
2380 nidstats_key(struct hlist_node *hnode)
2381 {
2382         struct nid_stat *ns;
2383
2384         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2385
2386         return &ns->nid;
2387 }
2388
2389 static int
2390 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2391 {
2392         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2393 }
2394
2395 static void *
2396 nidstats_object(struct hlist_node *hnode)
2397 {
2398         return hlist_entry(hnode, struct nid_stat, nid_hash);
2399 }
2400
2401 static void
2402 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2403 {
2404         struct nid_stat *ns;
2405
2406         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2407         nidstat_getref(ns);
2408 }
2409
2410 static void
2411 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2412 {
2413         struct nid_stat *ns;
2414
2415         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2416         nidstat_putref(ns);
2417 }
2418
2419 static struct cfs_hash_ops nid_stat_hash_ops = {
2420         .hs_hash        = nid_hash,
2421         .hs_key         = nidstats_key,
2422         .hs_keycmp      = nidstats_keycmp,
2423         .hs_object      = nidstats_object,
2424         .hs_get         = nidstats_get,
2425         .hs_put_locked  = nidstats_put_locked,
2426 };
2427
2428
2429 /*
2430  * client_generation<->export hash operations
2431  */
2432
2433 static unsigned
2434 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2435 {
2436         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2437 }
2438
2439 static void *
2440 gen_key(struct hlist_node *hnode)
2441 {
2442         struct obd_export *exp;
2443
2444         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2445
2446         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2447 }
2448
2449 /*
2450  * NOTE: It is impossible to find an export that is in failed
2451  *       state with this function
2452  */
2453 static int
2454 gen_kepcmp(const void *key, struct hlist_node *hnode)
2455 {
2456         struct obd_export *exp;
2457
2458         LASSERT(key);
2459         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2460
2461         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2462                !exp->exp_failed);
2463 }
2464
2465 static void *
2466 gen_export_object(struct hlist_node *hnode)
2467 {
2468         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2469 }
2470
2471 static void
2472 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2473 {
2474         struct obd_export *exp;
2475
2476         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2477         class_export_get(exp);
2478 }
2479
2480 static void
2481 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2482 {
2483         struct obd_export *exp;
2484
2485         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2486         class_export_put(exp);
2487 }
2488
2489 static struct cfs_hash_ops gen_hash_ops = {
2490         .hs_hash        = gen_hash,
2491         .hs_key         = gen_key,
2492         .hs_keycmp      = gen_kepcmp,
2493         .hs_object      = gen_export_object,
2494         .hs_get         = gen_export_get,
2495         .hs_put_locked  = gen_export_put_locked,
2496 };