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