Whamcloud - gitweb
LU-1399 config: check lustre_cfg_new() return
[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 static 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 static 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 static 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         OBD_FREE(new_param, new_len);
1020         OBD_FREE_PTR(bufs);
1021         if (new_cfg == NULL)
1022                 RETURN(ERR_PTR(-ENOMEM));
1023
1024         new_cfg->lcfg_num = cfg->lcfg_num;
1025         new_cfg->lcfg_flags = cfg->lcfg_flags;
1026         new_cfg->lcfg_nid = cfg->lcfg_nid;
1027         new_cfg->lcfg_nal = cfg->lcfg_nal;
1028
1029         RETURN(new_cfg);
1030 }
1031 EXPORT_SYMBOL(lustre_cfg_rename);
1032
1033 static int process_param2_config(struct lustre_cfg *lcfg)
1034 {
1035         char *param = lustre_cfg_string(lcfg, 1);
1036         char *upcall = lustre_cfg_string(lcfg, 2);
1037         char *argv[] = {
1038                 [0] = "/usr/sbin/lctl",
1039                 [1] = "set_param",
1040                 [2] = param,
1041                 [3] = NULL
1042         };
1043         struct timeval  start;
1044         struct timeval  end;
1045         int             rc;
1046         ENTRY;
1047
1048         /* Add upcall processing here. Now only lctl is supported */
1049         if (strcmp(upcall, LCTL_UPCALL) != 0) {
1050                 CERROR("Unsupported upcall %s\n", upcall);
1051                 RETURN(-EINVAL);
1052         }
1053
1054         do_gettimeofday(&start);
1055         rc = call_usermodehelper(argv[0], argv, NULL, 0);
1056         do_gettimeofday(&end);
1057
1058         if (rc < 0) {
1059                 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1060                        "time %ldus\n", argv[0], argv[1], argv[2], rc,
1061                        cfs_timeval_sub(&end, &start, NULL));
1062         } else {
1063                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1064                        argv[0], argv[1], argv[2],
1065                        cfs_timeval_sub(&end, &start, NULL));
1066                        rc = 0;
1067         }
1068
1069         RETURN(rc);
1070 }
1071
1072 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1073 {
1074         quota_process_config = qpc;
1075 }
1076 EXPORT_SYMBOL(lustre_register_quota_process_config);
1077
1078 /** Process configuration commands given in lustre_cfg form.
1079  * These may come from direct calls (e.g. class_manual_cleanup)
1080  * or processing the config llog, or ioctl from lctl.
1081  */
1082 int class_process_config(struct lustre_cfg *lcfg)
1083 {
1084         struct obd_device *obd;
1085         int err;
1086
1087         LASSERT(lcfg && !IS_ERR(lcfg));
1088         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1089
1090         /* Commands that don't need a device */
1091         switch(lcfg->lcfg_command) {
1092         case LCFG_ATTACH: {
1093                 err = class_attach(lcfg);
1094                 GOTO(out, err);
1095         }
1096         case LCFG_ADD_UUID: {
1097                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
1098                        " (%s)\n", lustre_cfg_string(lcfg, 1),
1099                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1100
1101                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1102                 GOTO(out, err);
1103         }
1104         case LCFG_DEL_UUID: {
1105                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1106                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1107                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1108
1109                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1110                 GOTO(out, err);
1111         }
1112         case LCFG_MOUNTOPT: {
1113                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1114                        lustre_cfg_string(lcfg, 1),
1115                        lustre_cfg_string(lcfg, 2),
1116                        lustre_cfg_string(lcfg, 3));
1117                 /* set these mount options somewhere, so ll_fill_super
1118                  * can find them. */
1119                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1120                                         lustre_cfg_string(lcfg, 1),
1121                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1122                                         lustre_cfg_string(lcfg, 2),
1123                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1124                                         lustre_cfg_string(lcfg, 3));
1125                 GOTO(out, err);
1126         }
1127         case LCFG_DEL_MOUNTOPT: {
1128                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1129                        lustre_cfg_string(lcfg, 1));
1130                 class_del_profile(lustre_cfg_string(lcfg, 1));
1131                 GOTO(out, err = 0);
1132         }
1133         case LCFG_SET_TIMEOUT: {
1134                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1135                        obd_timeout, lcfg->lcfg_num);
1136                 obd_timeout = max(lcfg->lcfg_num, 1U);
1137                 obd_timeout_set = 1;
1138                 GOTO(out, err = 0);
1139         }
1140         case LCFG_SET_LDLM_TIMEOUT: {
1141                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1142                        ldlm_timeout, lcfg->lcfg_num);
1143                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1144                 if (ldlm_timeout >= obd_timeout)
1145                         ldlm_timeout = max(obd_timeout / 3, 1U);
1146                 ldlm_timeout_set = 1;
1147                 GOTO(out, err = 0);
1148         }
1149         case LCFG_SET_UPCALL: {
1150                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1151                 /* COMPAT_146 Don't fail on old configs */
1152                 GOTO(out, err = 0);
1153         }
1154         case LCFG_MARKER: {
1155                 struct cfg_marker *marker;
1156                 marker = lustre_cfg_buf(lcfg, 1);
1157                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1158                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1159                 GOTO(out, err = 0);
1160         }
1161         case LCFG_PARAM: {
1162                 char *tmp;
1163                 /* llite has no obd */
1164                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1165                                        PARAM_LLITE, 0) == 0) &&
1166                     client_process_config) {
1167                         err = (*client_process_config)(lcfg);
1168                         GOTO(out, err);
1169                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1170                                               PARAM_SYS, &tmp) == 0)) {
1171                         /* Global param settings */
1172                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1173                         /*
1174                          * Client or server should not fail to mount if
1175                          * it hits an unknown configuration parameter.
1176                          */
1177                         if (err != 0)
1178                                 CWARN("Ignoring unknown param %s\n", tmp);
1179
1180                         GOTO(out, err = 0);
1181                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1182                                               PARAM_QUOTA, &tmp) == 0) &&
1183                            quota_process_config) {
1184                         err = (*quota_process_config)(lcfg);
1185                         GOTO(out, err);
1186                 }
1187
1188                 break;
1189         }
1190         case LCFG_SET_PARAM: {
1191                 err = process_param2_config(lcfg);
1192                 GOTO(out, err = 0);
1193         }
1194         }
1195         /* Commands that require a device */
1196         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1197         if (obd == NULL) {
1198                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1199                         CERROR("this lcfg command requires a device name\n");
1200                 else
1201                         CERROR("no device for: %s\n",
1202                                lustre_cfg_string(lcfg, 0));
1203
1204                 GOTO(out, err = -EINVAL);
1205         }
1206
1207         switch(lcfg->lcfg_command) {
1208         case LCFG_SETUP: {
1209                 err = class_setup(obd, lcfg);
1210                 GOTO(out, err);
1211         }
1212         case LCFG_DETACH: {
1213                 err = class_detach(obd, lcfg);
1214                 GOTO(out, err = 0);
1215         }
1216         case LCFG_CLEANUP: {
1217                 err = class_cleanup(obd, lcfg);
1218                 GOTO(out, err = 0);
1219         }
1220         case LCFG_ADD_CONN: {
1221                 err = class_add_conn(obd, lcfg);
1222                 GOTO(out, err = 0);
1223         }
1224         case LCFG_DEL_CONN: {
1225                 err = class_del_conn(obd, lcfg);
1226                 GOTO(out, err = 0);
1227         }
1228         case LCFG_POOL_NEW: {
1229                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1230                 GOTO(out, err = 0);
1231         }
1232         case LCFG_POOL_ADD: {
1233                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1234                                    lustre_cfg_string(lcfg, 3));
1235                 GOTO(out, err = 0);
1236         }
1237         case LCFG_POOL_REM: {
1238                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1239                                    lustre_cfg_string(lcfg, 3));
1240                 GOTO(out, err = 0);
1241         }
1242         case LCFG_POOL_DEL: {
1243                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1244                 GOTO(out, err = 0);
1245         }
1246         default: {
1247                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1248                 GOTO(out, err);
1249
1250         }
1251         }
1252 out:
1253         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1254                 CWARN("Ignoring error %d on optional command %#x\n", err,
1255                       lcfg->lcfg_command);
1256                 err = 0;
1257         }
1258         return err;
1259 }
1260 EXPORT_SYMBOL(class_process_config);
1261
1262 int class_process_proc_param(char *prefix, struct lprocfs_seq_vars *lvars,
1263                              struct lustre_cfg *lcfg, void *data)
1264 {
1265         struct lprocfs_seq_vars *var;
1266         struct file fakefile;
1267         struct seq_file fake_seqfile;
1268         char *key, *sval;
1269         int i, keylen, vallen;
1270         int matched = 0, j = 0;
1271         int rc = 0;
1272         int skip = 0;
1273         ENTRY;
1274
1275         if (lcfg->lcfg_command != LCFG_PARAM) {
1276                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1277                 RETURN(-EINVAL);
1278         }
1279
1280         /* fake a seq file so that var->fops->write can work... */
1281         fakefile.private_data = &fake_seqfile;
1282         fake_seqfile.private = data;
1283         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1284            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1285            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1286         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1287                 key = lustre_cfg_buf(lcfg, i);
1288                 /* Strip off prefix */
1289                 if (class_match_param(key, prefix, &key))
1290                         /* If the prefix doesn't match, return error so we
1291                          * can pass it down the stack */
1292                         RETURN(-ENOSYS);
1293                 sval = strchr(key, '=');
1294                 if (!sval || (*(sval + 1) == 0)) {
1295                         CERROR("Can't parse param %s (missing '=')\n", key);
1296                         /* rc = -EINVAL;        continue parsing other params */
1297                         continue;
1298                 }
1299                 keylen = sval - key;
1300                 sval++;
1301                 vallen = strlen(sval);
1302                 matched = 0;
1303                 j = 0;
1304                 /* Search proc entries */
1305                 while (lvars[j].name) {
1306                         var = &lvars[j];
1307                         if (class_match_param(key, (char *)var->name, 0) == 0 &&
1308                             keylen == strlen(var->name)) {
1309                                 matched++;
1310                                 rc = -EROFS;
1311
1312                                 if (var->fops && var->fops->write) {
1313                                         mm_segment_t oldfs;
1314                                         oldfs = get_fs();
1315                                         set_fs(KERNEL_DS);
1316                                         rc = (var->fops->write)(&fakefile, sval,
1317                                                                 vallen, NULL);
1318                                         set_fs(oldfs);
1319                                 }
1320                                 break;
1321                         }
1322                         j++;
1323                 }
1324                 if (!matched) {
1325                         CERROR("%.*s: %s unknown param %s\n",
1326                                (int)strlen(prefix) - 1, prefix,
1327                                (char *)lustre_cfg_string(lcfg, 0), key);
1328                         /* rc = -EINVAL;        continue parsing other params */
1329                         skip++;
1330                 } else if (rc < 0) {
1331                         CERROR("%s: error writing proc entry '%s': rc = %d\n",
1332                                prefix, var->name, rc);
1333                         rc = 0;
1334                 } else {
1335                         CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1336                                          lustre_cfg_string(lcfg, 0),
1337                                          (int)strlen(prefix) - 1, prefix,
1338                                          (int)(sval - key - 1), key, sval);
1339                 }
1340         }
1341
1342         if (rc > 0)
1343                 rc = 0;
1344         if (!rc && skip)
1345                 rc = skip;
1346         RETURN(rc);
1347 }
1348 EXPORT_SYMBOL(class_process_proc_param);
1349
1350 /*
1351  * Supplemental functions for config logs, it allocates lustre_cfg
1352  * buffers plus initialized llog record header at the beginning.
1353  */
1354 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1355 {
1356         struct llog_cfg_rec     *lcr;
1357         int                      reclen;
1358
1359         ENTRY;
1360
1361         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1362         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1363                  sizeof(struct llog_rec_tail);
1364
1365         OBD_ALLOC(lcr, reclen);
1366         if (lcr == NULL)
1367                 RETURN(NULL);
1368
1369         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1370
1371         lcr->lcr_hdr.lrh_len = reclen;
1372         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1373
1374         RETURN(lcr);
1375 }
1376 EXPORT_SYMBOL(lustre_cfg_rec_new);
1377
1378 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1379 {
1380         ENTRY;
1381         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1382         EXIT;
1383 }
1384 EXPORT_SYMBOL(lustre_cfg_rec_free);
1385
1386 /** Parse a configuration llog, doing various manipulations on them
1387  * for various reasons, (modifications for compatibility, skip obsolete
1388  * records, change uuids, etc), then class_process_config() resulting
1389  * net records.
1390  */
1391 int class_config_llog_handler(const struct lu_env *env,
1392                               struct llog_handle *handle,
1393                               struct llog_rec_hdr *rec, void *data)
1394 {
1395         struct config_llog_instance *clli = data;
1396         int cfg_len = rec->lrh_len;
1397         char *cfg_buf = (char*) (rec + 1);
1398         int rc = 0;
1399         ENTRY;
1400
1401         //class_config_dump_handler(handle, rec, data);
1402
1403         switch (rec->lrh_type) {
1404         case OBD_CFG_REC: {
1405                 struct lustre_cfg *lcfg, *lcfg_new;
1406                 struct lustre_cfg_bufs bufs;
1407                 char *inst_name = NULL;
1408                 int inst_len = 0;
1409                 int inst = 0, swab = 0;
1410
1411                 lcfg = (struct lustre_cfg *)cfg_buf;
1412                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1413                         lustre_swab_lustre_cfg(lcfg);
1414                         swab = 1;
1415                 }
1416
1417                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1418                 if (rc)
1419                         GOTO(out, rc);
1420
1421                 /* Figure out config state info */
1422                 if (lcfg->lcfg_command == LCFG_MARKER) {
1423                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1424                         lustre_swab_cfg_marker(marker, swab,
1425                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1426                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1427                                clli->cfg_flags, marker->cm_flags);
1428                         if (marker->cm_flags & CM_START) {
1429                                 /* all previous flags off */
1430                                 clli->cfg_flags = CFG_F_MARKER;
1431                                 server_name2index(marker->cm_tgtname,
1432                                                   &clli->cfg_lwp_idx, NULL);
1433                                 if (marker->cm_flags & CM_SKIP) {
1434                                         clli->cfg_flags |= CFG_F_SKIP;
1435                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1436                                                marker->cm_step);
1437                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1438                                            (clli->cfg_sb &&
1439                                             lustre_check_exclusion(clli->cfg_sb,
1440                                                          marker->cm_tgtname))) {
1441                                         clli->cfg_flags |= CFG_F_EXCLUDE;
1442                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1443                                                marker->cm_step);
1444                                 }
1445                         } else if (marker->cm_flags & CM_END) {
1446                                 clli->cfg_flags = 0;
1447                         }
1448                 }
1449                 /* A config command without a start marker before it is
1450                    illegal (post 146) */
1451                 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
1452                     !(clli->cfg_flags & CFG_F_MARKER) &&
1453                     (lcfg->lcfg_command != LCFG_MARKER)) {
1454                         CWARN("Config not inside markers, ignoring! "
1455                               "(inst: %p, uuid: %s, flags: %#x)\n",
1456                               clli->cfg_instance,
1457                               clli->cfg_uuid.uuid, clli->cfg_flags);
1458                         clli->cfg_flags |= CFG_F_SKIP;
1459                 }
1460                 if (clli->cfg_flags & CFG_F_SKIP) {
1461                         CDEBUG(D_CONFIG, "skipping %#x\n",
1462                                clli->cfg_flags);
1463                         rc = 0;
1464                         /* No processing! */
1465                         break;
1466                 }
1467
1468                 /*
1469                  * For interoperability between 1.8 and 2.0,
1470                  * rename "mds" obd device type to "mdt".
1471                  */
1472                 {
1473                         char *typename = lustre_cfg_string(lcfg, 1);
1474                         char *index = lustre_cfg_string(lcfg, 2);
1475
1476                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1477                              strcmp(typename, "mds") == 0)) {
1478                                 CWARN("For 1.8 interoperability, rename obd "
1479                                        "type from mds to mdt\n");
1480                                 typename[2] = 't';
1481                         }
1482                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1483                              strcmp(index, "type") == 0)) {
1484                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1485                                        "set this index to '0'\n");
1486                                 index[0] = '0';
1487                                 index[1] = 0;
1488                         }
1489                 }
1490
1491 #ifdef HAVE_SERVER_SUPPORT
1492                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1493                 {
1494                         char *typename = lustre_cfg_string(lcfg, 1);
1495
1496                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1497                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1498                             IS_MDT(s2lsi(clli->cfg_sb))) {
1499                                 CDEBUG(D_CONFIG,
1500                                        "For 2.x interoperability, rename obd "
1501                                        "type from lov to lod (%s)\n",
1502                                        s2lsi(clli->cfg_sb)->lsi_svname);
1503                                 strcpy(typename, LUSTRE_LOD_NAME);
1504                         }
1505                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1506                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1507                             IS_MDT(s2lsi(clli->cfg_sb))) {
1508                                 CDEBUG(D_CONFIG,
1509                                        "For 2.x interoperability, rename obd "
1510                                        "type from osc to osp (%s)\n",
1511                                        s2lsi(clli->cfg_sb)->lsi_svname);
1512                                 strcpy(typename, LUSTRE_OSP_NAME);
1513                         }
1514                 }
1515 #endif /* HAVE_SERVER_SUPPORT */
1516
1517                 if (clli->cfg_flags & CFG_F_EXCLUDE) {
1518                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1519                                lcfg->lcfg_command);
1520                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1521                                 /* Add inactive instead */
1522                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1523                 }
1524
1525                 lustre_cfg_bufs_init(&bufs, lcfg);
1526
1527                 if (clli && clli->cfg_instance &&
1528                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0){
1529                         inst = 1;
1530                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1531                                    sizeof(clli->cfg_instance) * 2 + 4;
1532                         OBD_ALLOC(inst_name, inst_len);
1533                         if (inst_name == NULL)
1534                                 GOTO(out, rc = -ENOMEM);
1535                         sprintf(inst_name, "%s-%p",
1536                                 lustre_cfg_string(lcfg, 0),
1537                                 clli->cfg_instance);
1538                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1539                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1540                                lcfg->lcfg_command, inst_name);
1541                 }
1542
1543                 /* we override the llog's uuid for clients, to insure they
1544                 are unique */
1545                 if (clli && clli->cfg_instance != NULL &&
1546                     lcfg->lcfg_command == LCFG_ATTACH) {
1547                         lustre_cfg_bufs_set_string(&bufs, 2,
1548                                                    clli->cfg_uuid.uuid);
1549                 }
1550                 /*
1551                  * sptlrpc config record, we expect 2 data segments:
1552                  *  [0]: fs_name/target_name,
1553                  *  [1]: rule string
1554                  * moving them to index [1] and [2], and insert MGC's
1555                  * obdname at index [0].
1556                  */
1557                 if (clli && clli->cfg_instance == NULL &&
1558                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1559                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1560                                             bufs.lcfg_buflen[1]);
1561                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1562                                             bufs.lcfg_buflen[0]);
1563                         lustre_cfg_bufs_set_string(&bufs, 0,
1564                                                    clli->cfg_obdname);
1565                 }
1566
1567                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1568                 if (lcfg_new == NULL)
1569                         GOTO(out, rc = -ENOMEM);
1570
1571                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1572                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1573
1574                 /* XXX Hack to try to remain binary compatible with
1575                  * pre-newconfig logs */
1576                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1577                     (lcfg->lcfg_nid >> 32) == 0) {
1578                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1579
1580                         lcfg_new->lcfg_nid =
1581                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1582                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1583                               lcfg->lcfg_nal, addr,
1584                               libcfs_nid2str(lcfg_new->lcfg_nid));
1585                 } else {
1586                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1587                 }
1588
1589                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1590
1591                 rc = class_process_config(lcfg_new);
1592                 lustre_cfg_free(lcfg_new);
1593
1594                 if (inst)
1595                         OBD_FREE(inst_name, inst_len);
1596                 break;
1597         }
1598         default:
1599                 CERROR("Unknown llog record type %#x encountered\n",
1600                        rec->lrh_type);
1601                 break;
1602         }
1603 out:
1604         if (rc) {
1605                 CERROR("%s: cfg command failed: rc = %d\n",
1606                        handle->lgh_ctxt->loc_obd->obd_name, rc);
1607                 class_config_dump_handler(NULL, handle, rec, data);
1608         }
1609         RETURN(rc);
1610 }
1611 EXPORT_SYMBOL(class_config_llog_handler);
1612
1613 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1614                             char *name, struct config_llog_instance *cfg)
1615 {
1616         struct llog_process_cat_data     cd = {0, 0};
1617         struct llog_handle              *llh;
1618         llog_cb_t                        callback;
1619         int                              rc;
1620         ENTRY;
1621
1622         CDEBUG(D_INFO, "looking up llog %s\n", name);
1623         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1624         if (rc)
1625                 RETURN(rc);
1626
1627         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1628         if (rc)
1629                 GOTO(parse_out, rc);
1630
1631         /* continue processing from where we last stopped to end-of-log */
1632         if (cfg) {
1633                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1634                 callback = cfg->cfg_callback;
1635                 LASSERT(callback != NULL);
1636         } else {
1637                 callback = class_config_llog_handler;
1638         }
1639
1640         cd.lpcd_last_idx = 0;
1641
1642         rc = llog_process(env, llh, callback, cfg, &cd);
1643
1644         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1645                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1646         if (cfg)
1647                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1648
1649 parse_out:
1650         llog_close(env, llh);
1651         RETURN(rc);
1652 }
1653 EXPORT_SYMBOL(class_config_parse_llog);
1654
1655 static struct lcfg_type_data {
1656         __u32    ltd_type;
1657         char    *ltd_name;
1658         char    *ltd_bufs[4];
1659 } lcfg_data_table[] = {
1660         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1661         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1662         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1663         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1664         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1665         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1666         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1667         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1668         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1669         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1670         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1671         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1672         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1673         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1674         { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1675         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1676         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1677         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1678         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1679         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1680         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1681         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1682         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1683         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1684         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1685         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1686         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1687           { "parameter", "2", "3", "4" } },
1688         { 0, NULL, { NULL, NULL, NULL, NULL } }
1689 };
1690
1691 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1692 {
1693         int i = 0;
1694
1695         while (lcfg_data_table[i].ltd_type != 0) {
1696                 if (lcfg_data_table[i].ltd_type == cmd)
1697                         return &lcfg_data_table[i];
1698                 i++;
1699         }
1700         return NULL;
1701 }
1702
1703 /**
1704  * parse config record and output dump in supplied buffer.
1705  * This is separated from class_config_dump_handler() to use
1706  * for ioctl needs as well
1707  *
1708  * Sample Output:
1709  * - { event: attach, device: lustrewt-clilov, type: lov, UUID:
1710  *     lustrewt-clilov_UUID }
1711  */
1712 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1713 {
1714         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1715         char                    *ptr = buf;
1716         char                    *end = buf + size;
1717         int                      rc = 0, i;
1718         struct lcfg_type_data   *ldata;
1719
1720         LASSERT(rec->lrh_type == OBD_CFG_REC);
1721         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1722         if (rc < 0)
1723                 return rc;
1724
1725         ldata = lcfg_cmd2data(lcfg->lcfg_command);
1726         if (ldata == NULL)
1727                 return -ENOTTY;
1728
1729         if (lcfg->lcfg_command == LCFG_MARKER)
1730                 return 0;
1731
1732         /* form YAML entity */
1733         ptr += snprintf(ptr, end - ptr, "- { event: %s", ldata->ltd_name);
1734
1735         if (lcfg->lcfg_flags)
1736                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1737                                 lcfg->lcfg_flags);
1738         if (lcfg->lcfg_num)
1739                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1740                                 lcfg->lcfg_num);
1741         if (lcfg->lcfg_nid)
1742                 ptr += snprintf(ptr, end - ptr, ", nid: %s("LPX64")",
1743                                 libcfs_nid2str(lcfg->lcfg_nid),
1744                                 lcfg->lcfg_nid);
1745
1746         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1747                 ptr += snprintf(ptr, end - ptr, ", device: %s",
1748                                 lustre_cfg_string(lcfg, 0));
1749
1750         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1751                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1752                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
1753                                         ldata->ltd_bufs[i - 1],
1754                                         lustre_cfg_string(lcfg, i));
1755         }
1756
1757         ptr += snprintf(ptr, end - ptr, " }\n");
1758         /* return consumed bytes */
1759         rc = ptr - buf;
1760         return rc;
1761 }
1762
1763 /**
1764  * parse config record and output dump in supplied buffer.
1765  * This is separated from class_config_dump_handler() to use
1766  * for ioctl needs as well
1767  */
1768 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1769 {
1770         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1771         char                    *ptr = buf;
1772         char                    *end = buf + size;
1773         int                      rc = 0;
1774
1775         ENTRY;
1776
1777         LASSERT(rec->lrh_type == OBD_CFG_REC);
1778         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1779         if (rc < 0)
1780                 RETURN(rc);
1781
1782         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1783         if (lcfg->lcfg_flags)
1784                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1785                                 lcfg->lcfg_flags);
1786
1787         if (lcfg->lcfg_num)
1788                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1789
1790         if (lcfg->lcfg_nid)
1791                 ptr += snprintf(ptr, end-ptr, "nid=%s("LPX64")\n     ",
1792                                 libcfs_nid2str(lcfg->lcfg_nid),
1793                                 lcfg->lcfg_nid);
1794
1795         if (lcfg->lcfg_command == LCFG_MARKER) {
1796                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1797
1798                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1799                                 marker->cm_step, marker->cm_flags,
1800                                 marker->cm_tgtname, marker->cm_comment);
1801         } else {
1802                 int i;
1803
1804                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1805                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1806                                         lustre_cfg_string(lcfg, i));
1807                 }
1808         }
1809         ptr += snprintf(ptr, end - ptr, "\n");
1810         /* return consumed bytes */
1811         rc = ptr - buf;
1812         RETURN(rc);
1813 }
1814
1815 int class_config_dump_handler(const struct lu_env *env,
1816                               struct llog_handle *handle,
1817                               struct llog_rec_hdr *rec, void *data)
1818 {
1819         char    *outstr;
1820         int      rc = 0;
1821
1822         ENTRY;
1823
1824         OBD_ALLOC(outstr, 256);
1825         if (outstr == NULL)
1826                 RETURN(-ENOMEM);
1827
1828         if (rec->lrh_type == OBD_CFG_REC) {
1829                 class_config_parse_rec(rec, outstr, 256);
1830                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1831         } else {
1832                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1833                 rc = -EINVAL;
1834         }
1835
1836         OBD_FREE(outstr, 256);
1837         RETURN(rc);
1838 }
1839
1840 int class_config_dump_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1841                            char *name, struct config_llog_instance *cfg)
1842 {
1843         struct llog_handle      *llh;
1844         int                      rc;
1845
1846         ENTRY;
1847
1848         LCONSOLE_INFO("Dumping config log %s\n", name);
1849
1850         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1851         if (rc)
1852                 RETURN(rc);
1853
1854         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1855         if (rc)
1856                 GOTO(parse_out, rc);
1857
1858         rc = llog_process(env, llh, class_config_dump_handler, cfg, NULL);
1859 parse_out:
1860         llog_close(env, llh);
1861
1862         LCONSOLE_INFO("End config log %s\n", name);
1863         RETURN(rc);
1864 }
1865 EXPORT_SYMBOL(class_config_dump_llog);
1866
1867 /** Call class_cleanup and class_detach.
1868  * "Manual" only in the sense that we're faking lcfg commands.
1869  */
1870 int class_manual_cleanup(struct obd_device *obd)
1871 {
1872         char                    flags[3] = "";
1873         struct lustre_cfg      *lcfg;
1874         struct lustre_cfg_bufs  bufs;
1875         int                     rc;
1876         ENTRY;
1877
1878         if (!obd) {
1879                 CERROR("empty cleanup\n");
1880                 RETURN(-EALREADY);
1881         }
1882
1883         if (obd->obd_force)
1884                 strcat(flags, "F");
1885         if (obd->obd_fail)
1886                 strcat(flags, "A");
1887
1888         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1889                obd->obd_name, flags);
1890
1891         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1892         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1893         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1894         if (lcfg == NULL)
1895                 RETURN(-ENOMEM);
1896
1897         rc = class_process_config(lcfg);
1898         if (rc) {
1899                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1900                 GOTO(out, rc);
1901         }
1902
1903         /* the lcfg is almost the same for both ops */
1904         lcfg->lcfg_command = LCFG_DETACH;
1905         rc = class_process_config(lcfg);
1906         if (rc)
1907                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1908 out:
1909         lustre_cfg_free(lcfg);
1910         RETURN(rc);
1911 }
1912 EXPORT_SYMBOL(class_manual_cleanup);
1913
1914 /*
1915  * uuid<->export lustre hash operations
1916  */
1917
1918 static unsigned
1919 uuid_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1920 {
1921         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
1922                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
1923 }
1924
1925 static void *
1926 uuid_key(struct hlist_node *hnode)
1927 {
1928         struct obd_export *exp;
1929
1930         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1931
1932         return &exp->exp_client_uuid;
1933 }
1934
1935 /*
1936  * NOTE: It is impossible to find an export that is in failed
1937  *       state with this function
1938  */
1939 static int
1940 uuid_keycmp(const void *key, struct hlist_node *hnode)
1941 {
1942         struct obd_export *exp;
1943
1944         LASSERT(key);
1945         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1946
1947         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
1948                !exp->exp_failed;
1949 }
1950
1951 static void *
1952 uuid_export_object(struct hlist_node *hnode)
1953 {
1954         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1955 }
1956
1957 static void
1958 uuid_export_get(cfs_hash_t *hs, struct hlist_node *hnode)
1959 {
1960         struct obd_export *exp;
1961
1962         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1963         class_export_get(exp);
1964 }
1965
1966 static void
1967 uuid_export_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
1968 {
1969         struct obd_export *exp;
1970
1971         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1972         class_export_put(exp);
1973 }
1974
1975 static cfs_hash_ops_t uuid_hash_ops = {
1976         .hs_hash        = uuid_hash,
1977         .hs_key         = uuid_key,
1978         .hs_keycmp      = uuid_keycmp,
1979         .hs_object      = uuid_export_object,
1980         .hs_get         = uuid_export_get,
1981         .hs_put_locked  = uuid_export_put_locked,
1982 };
1983
1984
1985 /*
1986  * nid<->export hash operations
1987  */
1988
1989 static unsigned
1990 nid_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1991 {
1992         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1993 }
1994
1995 static void *
1996 nid_key(struct hlist_node *hnode)
1997 {
1998         struct obd_export *exp;
1999
2000         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2001
2002         RETURN(&exp->exp_connection->c_peer.nid);
2003 }
2004
2005 /*
2006  * NOTE: It is impossible to find an export that is in failed
2007  *       state with this function
2008  */
2009 static int
2010 nid_kepcmp(const void *key, struct hlist_node *hnode)
2011 {
2012         struct obd_export *exp;
2013
2014         LASSERT(key);
2015         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2016
2017         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2018                !exp->exp_failed);
2019 }
2020
2021 static void *
2022 nid_export_object(struct hlist_node *hnode)
2023 {
2024         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2025 }
2026
2027 static void
2028 nid_export_get(cfs_hash_t *hs, struct hlist_node *hnode)
2029 {
2030         struct obd_export *exp;
2031
2032         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2033         class_export_get(exp);
2034 }
2035
2036 static void
2037 nid_export_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
2038 {
2039         struct obd_export *exp;
2040
2041         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2042         class_export_put(exp);
2043 }
2044
2045 static cfs_hash_ops_t nid_hash_ops = {
2046         .hs_hash        = nid_hash,
2047         .hs_key         = nid_key,
2048         .hs_keycmp      = nid_kepcmp,
2049         .hs_object      = nid_export_object,
2050         .hs_get         = nid_export_get,
2051         .hs_put_locked  = nid_export_put_locked,
2052 };
2053
2054
2055 /*
2056  * nid<->nidstats hash operations
2057  */
2058
2059 static void *
2060 nidstats_key(struct hlist_node *hnode)
2061 {
2062         struct nid_stat *ns;
2063
2064         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2065
2066         return &ns->nid;
2067 }
2068
2069 static int
2070 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2071 {
2072         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2073 }
2074
2075 static void *
2076 nidstats_object(struct hlist_node *hnode)
2077 {
2078         return hlist_entry(hnode, struct nid_stat, nid_hash);
2079 }
2080
2081 static void
2082 nidstats_get(cfs_hash_t *hs, struct hlist_node *hnode)
2083 {
2084         struct nid_stat *ns;
2085
2086         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2087         nidstat_getref(ns);
2088 }
2089
2090 static void
2091 nidstats_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
2092 {
2093         struct nid_stat *ns;
2094
2095         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2096         nidstat_putref(ns);
2097 }
2098
2099 static cfs_hash_ops_t nid_stat_hash_ops = {
2100         .hs_hash        = nid_hash,
2101         .hs_key         = nidstats_key,
2102         .hs_keycmp      = nidstats_keycmp,
2103         .hs_object      = nidstats_object,
2104         .hs_get         = nidstats_get,
2105         .hs_put_locked  = nidstats_put_locked,
2106 };