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