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