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