]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/obdclass/obd_config.c
staging: lustre: checking for NULL instead if IS_ERR
[karo-tx-linux.git] / drivers / staging / lustre / lustre / obdclass / obd_config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_config.c
33  *
34  * Config API
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <linux/string.h>
40
41 #include "../include/lustre/lustre_ioctl.h"
42 #include "../include/llog_swab.h"
43 #include "../include/lprocfs_status.h"
44 #include "../include/lustre_log.h"
45 #include "../include/lustre_param.h"
46 #include "../include/obd_class.h"
47
48 #include "llog_internal.h"
49
50 static struct cfs_hash_ops uuid_hash_ops;
51
52 /*********** string parsing utils *********/
53
54 /* returns 0 if we find this key in the buffer, else 1 */
55 int class_find_param(char *buf, char *key, char **valp)
56 {
57         char *ptr;
58
59         if (!buf)
60                 return 1;
61
62         ptr = strstr(buf, key);
63         if (!ptr)
64                 return 1;
65
66         if (valp)
67                 *valp = ptr + strlen(key);
68
69         return 0;
70 }
71 EXPORT_SYMBOL(class_find_param);
72
73 /* returns 0 if this is the first key in the buffer, else 1.
74  * valp points to first char after key.
75  */
76 static int class_match_param(char *buf, const char *key, char **valp)
77 {
78         if (!buf)
79                 return 1;
80
81         if (memcmp(buf, key, strlen(key)) != 0)
82                 return 1;
83
84         if (valp)
85                 *valp = buf + strlen(key);
86
87         return 0;
88 }
89
90 static int parse_nid(char *buf, void *value, int quiet)
91 {
92         lnet_nid_t *nid = value;
93
94         *nid = libcfs_str2nid(buf);
95         if (*nid != LNET_NID_ANY)
96                 return 0;
97
98         if (!quiet)
99                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
100         return -EINVAL;
101 }
102
103 static int parse_net(char *buf, void *value)
104 {
105         __u32 *net = value;
106
107         *net = libcfs_str2net(buf);
108         CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
109         return 0;
110 }
111
112 enum {
113         CLASS_PARSE_NID = 1,
114         CLASS_PARSE_NET,
115 };
116
117 /* 0 is good nid,
118  * 1 not found
119  * < 0 error
120  * endh is set to next separator
121  */
122 static int class_parse_value(char *buf, int opc, void *value, char **endh,
123                              int quiet)
124 {
125         char *endp;
126         char  tmp;
127         int   rc = 0;
128
129         if (!buf)
130                 return 1;
131         while (*buf == ',' || *buf == ':')
132                 buf++;
133         if (*buf == ' ' || *buf == '/' || *buf == '\0')
134                 return 1;
135
136         /* nid separators or end of nids */
137         endp = strpbrk(buf, ",: /");
138         if (!endp)
139                 endp = buf + strlen(buf);
140
141         tmp = *endp;
142         *endp = '\0';
143         switch (opc) {
144         default:
145                 LBUG();
146         case CLASS_PARSE_NID:
147                 rc = parse_nid(buf, value, quiet);
148                 break;
149         case CLASS_PARSE_NET:
150                 rc = parse_net(buf, value);
151                 break;
152         }
153         *endp = tmp;
154         if (rc != 0)
155                 return rc;
156         if (endh)
157                 *endh = endp;
158         return 0;
159 }
160
161 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
162 {
163         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
164 }
165 EXPORT_SYMBOL(class_parse_nid);
166
167 int class_parse_nid_quiet(char *buf, lnet_nid_t *nid, char **endh)
168 {
169         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
170 }
171 EXPORT_SYMBOL(class_parse_nid_quiet);
172
173 /********************** class fns **********************/
174
175 /**
176  * Create a new obd device and set the type, name and uuid.  If successful,
177  * the new device can be accessed by either name or uuid.
178  */
179 static int class_attach(struct lustre_cfg *lcfg)
180 {
181         struct obd_device *obd = NULL;
182         char *typename, *name, *uuid;
183         int rc, len;
184
185         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
186                 CERROR("No type passed!\n");
187                 return -EINVAL;
188         }
189         typename = lustre_cfg_string(lcfg, 1);
190
191         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
192                 CERROR("No name passed!\n");
193                 return -EINVAL;
194         }
195         name = lustre_cfg_string(lcfg, 0);
196
197         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
198                 CERROR("No UUID passed!\n");
199                 return -EINVAL;
200         }
201         uuid = lustre_cfg_string(lcfg, 2);
202
203         CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
204                MKSTR(typename), MKSTR(name), MKSTR(uuid));
205
206         obd = class_newdev(typename, name);
207         if (IS_ERR(obd)) {
208                 /* Already exists or out of obds */
209                 rc = PTR_ERR(obd);
210                 obd = NULL;
211                 CERROR("Cannot create device %s of type %s : %d\n",
212                        name, typename, rc);
213                 goto out;
214         }
215         LASSERTF(obd, "Cannot get obd device %s of type %s\n",
216                  name, typename);
217         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
218                  "obd %p obd_magic %08X != %08X\n",
219                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
220         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
221                  "%p obd_name %s != %s\n", obd, obd->obd_name, name);
222
223         rwlock_init(&obd->obd_pool_lock);
224         obd->obd_pool_limit = 0;
225         obd->obd_pool_slv = 0;
226
227         INIT_LIST_HEAD(&obd->obd_exports);
228         INIT_LIST_HEAD(&obd->obd_unlinked_exports);
229         INIT_LIST_HEAD(&obd->obd_delayed_exports);
230         spin_lock_init(&obd->obd_nid_lock);
231         spin_lock_init(&obd->obd_dev_lock);
232         mutex_init(&obd->obd_dev_mutex);
233         spin_lock_init(&obd->obd_osfs_lock);
234         /* obd->obd_osfs_age must be set to a value in the distant
235          * past to guarantee a fresh statfs is fetched on mount.
236          */
237         obd->obd_osfs_age = cfs_time_shift_64(-1000);
238
239         /* XXX belongs in setup not attach  */
240         init_rwsem(&obd->obd_observer_link_sem);
241         /* recovery data */
242         init_waitqueue_head(&obd->obd_evict_inprogress_waitq);
243
244         llog_group_init(&obd->obd_olg);
245
246         obd->obd_conn_inprogress = 0;
247
248         len = strlen(uuid);
249         if (len >= sizeof(obd->obd_uuid)) {
250                 CERROR("uuid must be < %d bytes long\n",
251                        (int)sizeof(obd->obd_uuid));
252                 rc = -EINVAL;
253                 goto out;
254         }
255         memcpy(obd->obd_uuid.uuid, uuid, len);
256
257         /* Detach drops this */
258         spin_lock(&obd->obd_dev_lock);
259         atomic_set(&obd->obd_refcount, 1);
260         spin_unlock(&obd->obd_dev_lock);
261         lu_ref_init(&obd->obd_reference);
262         lu_ref_add(&obd->obd_reference, "attach", obd);
263
264         obd->obd_attached = 1;
265         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
266                obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
267         return 0;
268  out:
269         if (obd)
270                 class_release_dev(obd);
271
272         return rc;
273 }
274
275 /** Create hashes, self-export, and call type-specific setup.
276  * Setup is effectively the "start this obd" call.
277  */
278 static int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
279 {
280         int err = 0;
281         struct obd_export *exp;
282
283         LASSERT(obd);
284         LASSERTF(obd == class_num2obd(obd->obd_minor),
285                  "obd %p != obd_devs[%d] %p\n",
286                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
287         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
288                  "obd %p obd_magic %08x != %08x\n",
289                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
290
291         /* have we attached a type to this device? */
292         if (!obd->obd_attached) {
293                 CERROR("Device %d not attached\n", obd->obd_minor);
294                 return -ENODEV;
295         }
296
297         if (obd->obd_set_up) {
298                 CERROR("Device %d already setup (type %s)\n",
299                        obd->obd_minor, obd->obd_type->typ_name);
300                 return -EEXIST;
301         }
302
303         /* is someone else setting us up right now? (attach inits spinlock) */
304         spin_lock(&obd->obd_dev_lock);
305         if (obd->obd_starting) {
306                 spin_unlock(&obd->obd_dev_lock);
307                 CERROR("Device %d setup in progress (type %s)\n",
308                        obd->obd_minor, obd->obd_type->typ_name);
309                 return -EEXIST;
310         }
311         /* just leave this on forever.  I can't use obd_set_up here because
312          * other fns check that status, and we're not actually set up yet.
313          */
314         obd->obd_starting = 1;
315         obd->obd_uuid_hash = NULL;
316         spin_unlock(&obd->obd_dev_lock);
317
318         /* create an uuid-export lustre hash */
319         obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
320                                              HASH_UUID_CUR_BITS,
321                                              HASH_UUID_MAX_BITS,
322                                              HASH_UUID_BKT_BITS, 0,
323                                              CFS_HASH_MIN_THETA,
324                                              CFS_HASH_MAX_THETA,
325                                              &uuid_hash_ops, CFS_HASH_DEFAULT);
326         if (!obd->obd_uuid_hash) {
327                 err = -ENOMEM;
328                 goto err_hash;
329         }
330
331         exp = class_new_export(obd, &obd->obd_uuid);
332         if (IS_ERR(exp)) {
333                 err = PTR_ERR(exp);
334                 goto err_hash;
335         }
336
337         obd->obd_self_export = exp;
338         class_export_put(exp);
339
340         err = obd_setup(obd, lcfg);
341         if (err)
342                 goto err_exp;
343
344         obd->obd_set_up = 1;
345
346         spin_lock(&obd->obd_dev_lock);
347         /* cleanup drops this */
348         class_incref(obd, "setup", obd);
349         spin_unlock(&obd->obd_dev_lock);
350
351         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
352                obd->obd_name, obd->obd_uuid.uuid);
353
354         return 0;
355 err_exp:
356         if (obd->obd_self_export) {
357                 class_unlink_export(obd->obd_self_export);
358                 obd->obd_self_export = NULL;
359         }
360 err_hash:
361         if (obd->obd_uuid_hash) {
362                 cfs_hash_putref(obd->obd_uuid_hash);
363                 obd->obd_uuid_hash = NULL;
364         }
365         obd->obd_starting = 0;
366         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
367         return err;
368 }
369
370 /** We have finished using this obd and are ready to destroy it.
371  * There can be no more references to this obd.
372  */
373 static int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
374 {
375         if (obd->obd_set_up) {
376                 CERROR("OBD device %d still set up\n", obd->obd_minor);
377                 return -EBUSY;
378         }
379
380         spin_lock(&obd->obd_dev_lock);
381         if (!obd->obd_attached) {
382                 spin_unlock(&obd->obd_dev_lock);
383                 CERROR("OBD device %d not attached\n", obd->obd_minor);
384                 return -ENODEV;
385         }
386         obd->obd_attached = 0;
387         spin_unlock(&obd->obd_dev_lock);
388
389         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
390                obd->obd_name, obd->obd_uuid.uuid);
391
392         class_decref(obd, "attach", obd);
393         return 0;
394 }
395
396 /** Start shutting down the obd.  There may be in-progress ops when
397  * this is called.  We tell them to start shutting down with a call
398  * to class_disconnect_exports().
399  */
400 static int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
401 {
402         int err = 0;
403         char *flag;
404
405         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
406
407         if (!obd->obd_set_up) {
408                 CERROR("Device %d not setup\n", obd->obd_minor);
409                 return -ENODEV;
410         }
411
412         spin_lock(&obd->obd_dev_lock);
413         if (obd->obd_stopping) {
414                 spin_unlock(&obd->obd_dev_lock);
415                 CERROR("OBD %d already stopping\n", obd->obd_minor);
416                 return -ENODEV;
417         }
418         /* Leave this on forever */
419         obd->obd_stopping = 1;
420         spin_unlock(&obd->obd_dev_lock);
421
422         while (obd->obd_conn_inprogress > 0)
423                 yield();
424         smp_rmb();
425
426         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
427                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
428                         switch (*flag) {
429                         case 'F':
430                                 obd->obd_force = 1;
431                                 break;
432                         case 'A':
433                                 LCONSOLE_WARN("Failing over %s\n",
434                                               obd->obd_name);
435                                 obd->obd_fail = 1;
436                                 obd->obd_no_transno = 1;
437                                 obd->obd_no_recov = 1;
438                                 if (OBP(obd, iocontrol)) {
439                                         obd_iocontrol(OBD_IOC_SYNC,
440                                                       obd->obd_self_export,
441                                                       0, NULL, NULL);
442                                 }
443                                 break;
444                         default:
445                                 CERROR("Unrecognised flag '%c'\n", *flag);
446                         }
447         }
448
449         LASSERT(obd->obd_self_export);
450
451         /* Precleanup, we must make sure all exports get destroyed. */
452         err = obd_precleanup(obd);
453         if (err)
454                 CERROR("Precleanup %s returned %d\n",
455                        obd->obd_name, err);
456
457         /* destroy an uuid-export hash body */
458         if (obd->obd_uuid_hash) {
459                 cfs_hash_putref(obd->obd_uuid_hash);
460                 obd->obd_uuid_hash = NULL;
461         }
462
463         class_decref(obd, "setup", obd);
464         obd->obd_set_up = 0;
465
466         return 0;
467 }
468
469 struct obd_device *class_incref(struct obd_device *obd,
470                                 const char *scope, const void *source)
471 {
472         lu_ref_add_atomic(&obd->obd_reference, scope, source);
473         atomic_inc(&obd->obd_refcount);
474         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
475                atomic_read(&obd->obd_refcount));
476
477         return obd;
478 }
479 EXPORT_SYMBOL(class_incref);
480
481 void class_decref(struct obd_device *obd, const char *scope, const void *source)
482 {
483         int err;
484         int refs;
485
486         spin_lock(&obd->obd_dev_lock);
487         atomic_dec(&obd->obd_refcount);
488         refs = atomic_read(&obd->obd_refcount);
489         spin_unlock(&obd->obd_dev_lock);
490         lu_ref_del(&obd->obd_reference, scope, source);
491
492         CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
493
494         if ((refs == 1) && obd->obd_stopping) {
495                 /* All exports have been destroyed; there should
496                  * be no more in-progress ops by this point.
497                  */
498
499                 spin_lock(&obd->obd_self_export->exp_lock);
500                 obd->obd_self_export->exp_flags |= exp_flags_from_obd(obd);
501                 spin_unlock(&obd->obd_self_export->exp_lock);
502
503                 /* note that we'll recurse into class_decref again */
504                 class_unlink_export(obd->obd_self_export);
505                 return;
506         }
507
508         if (refs == 0) {
509                 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
510                        obd->obd_name, obd->obd_uuid.uuid);
511                 LASSERT(!obd->obd_attached);
512                 if (obd->obd_stopping) {
513                         /* If we're not stopping, we were never set up */
514                         err = obd_cleanup(obd);
515                         if (err)
516                                 CERROR("Cleanup %s returned %d\n",
517                                        obd->obd_name, err);
518                 }
519                 class_release_dev(obd);
520         }
521 }
522 EXPORT_SYMBOL(class_decref);
523
524 /** Add a failover nid location.
525  * Client obd types contact server obd types using this nid list.
526  */
527 static int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
528 {
529         struct obd_import *imp;
530         struct obd_uuid uuid;
531         int rc;
532
533         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
534             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
535                 CERROR("invalid conn_uuid\n");
536                 return -EINVAL;
537         }
538         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
539             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
540             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
541             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
542             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
543                 CERROR("can't add connection on non-client dev\n");
544                 return -EINVAL;
545         }
546
547         imp = obd->u.cli.cl_import;
548         if (!imp) {
549                 CERROR("try to add conn on immature client dev\n");
550                 return -EINVAL;
551         }
552
553         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
554         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
555
556         return rc;
557 }
558
559 /** Remove a failover nid location.
560  */
561 static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
562 {
563         struct obd_import *imp;
564         struct obd_uuid uuid;
565         int rc;
566
567         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
568             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
569                 CERROR("invalid conn_uuid\n");
570                 return -EINVAL;
571         }
572         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
573             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
574                 CERROR("can't del connection on non-client dev\n");
575                 return -EINVAL;
576         }
577
578         imp = obd->u.cli.cl_import;
579         if (!imp) {
580                 CERROR("try to del conn on immature client dev\n");
581                 return -EINVAL;
582         }
583
584         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
585         rc = obd_del_conn(imp, &uuid);
586
587         return rc;
588 }
589
590 static LIST_HEAD(lustre_profile_list);
591 static DEFINE_SPINLOCK(lustre_profile_list_lock);
592
593 struct lustre_profile *class_get_profile(const char *prof)
594 {
595         struct lustre_profile *lprof;
596
597         spin_lock(&lustre_profile_list_lock);
598         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
599                 if (!strcmp(lprof->lp_profile, prof)) {
600                         lprof->lp_refs++;
601                         spin_unlock(&lustre_profile_list_lock);
602                         return lprof;
603                 }
604         }
605         spin_unlock(&lustre_profile_list_lock);
606         return NULL;
607 }
608 EXPORT_SYMBOL(class_get_profile);
609
610 /** Create a named "profile".
611  * This defines the mdc and osc names to use for a client.
612  * This also is used to define the lov to be used by a mdt.
613  */
614 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
615                              int mdclen, char *mdc)
616 {
617         struct lustre_profile *lprof;
618         int err = 0;
619
620         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
621
622         lprof = kzalloc(sizeof(*lprof), GFP_NOFS);
623         if (!lprof)
624                 return -ENOMEM;
625         INIT_LIST_HEAD(&lprof->lp_list);
626
627         LASSERT(proflen == (strlen(prof) + 1));
628         lprof->lp_profile = kmemdup(prof, proflen, GFP_NOFS);
629         if (!lprof->lp_profile) {
630                 err = -ENOMEM;
631                 goto free_lprof;
632         }
633
634         LASSERT(osclen == (strlen(osc) + 1));
635         lprof->lp_dt = kmemdup(osc, osclen, GFP_NOFS);
636         if (!lprof->lp_dt) {
637                 err = -ENOMEM;
638                 goto free_lp_profile;
639         }
640
641         if (mdclen > 0) {
642                 LASSERT(mdclen == (strlen(mdc) + 1));
643                 lprof->lp_md = kmemdup(mdc, mdclen, GFP_NOFS);
644                 if (!lprof->lp_md) {
645                         err = -ENOMEM;
646                         goto free_lp_dt;
647                 }
648         }
649
650         spin_lock(&lustre_profile_list_lock);
651         lprof->lp_refs = 1;
652         lprof->lp_list_deleted = false;
653         list_add(&lprof->lp_list, &lustre_profile_list);
654         spin_unlock(&lustre_profile_list_lock);
655         return err;
656
657 free_lp_dt:
658         kfree(lprof->lp_dt);
659 free_lp_profile:
660         kfree(lprof->lp_profile);
661 free_lprof:
662         kfree(lprof);
663         return err;
664 }
665
666 void class_del_profile(const char *prof)
667 {
668         struct lustre_profile *lprof;
669
670         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
671
672         lprof = class_get_profile(prof);
673         if (lprof) {
674                 spin_lock(&lustre_profile_list_lock);
675                 /* because get profile increments the ref counter */
676                 lprof->lp_refs--;
677                 list_del(&lprof->lp_list);
678                 lprof->lp_list_deleted = true;
679                 spin_unlock(&lustre_profile_list_lock);
680
681                 class_put_profile(lprof);
682         }
683 }
684 EXPORT_SYMBOL(class_del_profile);
685
686 void class_put_profile(struct lustre_profile *lprof)
687 {
688         spin_lock(&lustre_profile_list_lock);
689         if (--lprof->lp_refs > 0) {
690                 LASSERT(lprof->lp_refs > 0);
691                 spin_unlock(&lustre_profile_list_lock);
692                 return;
693         }
694         spin_unlock(&lustre_profile_list_lock);
695
696         /* confirm not a negative number */
697         LASSERT(!lprof->lp_refs);
698
699         /*
700          * At least one class_del_profile/profiles must be called
701          * on the target profile or lustre_profile_list will corrupt
702          */
703         LASSERT(lprof->lp_list_deleted);
704         kfree(lprof->lp_profile);
705         kfree(lprof->lp_dt);
706         kfree(lprof->lp_md);
707         kfree(lprof);
708 }
709 EXPORT_SYMBOL(class_put_profile);
710
711 /* COMPAT_146 */
712 void class_del_profiles(void)
713 {
714         struct lustre_profile *lprof, *n;
715
716         spin_lock(&lustre_profile_list_lock);
717         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
718                 list_del(&lprof->lp_list);
719                 lprof->lp_list_deleted = true;
720                 spin_unlock(&lustre_profile_list_lock);
721
722                 class_put_profile(lprof);
723
724                 spin_lock(&lustre_profile_list_lock);
725         }
726         spin_unlock(&lustre_profile_list_lock);
727 }
728 EXPORT_SYMBOL(class_del_profiles);
729
730 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
731 {
732         if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
733                 at_min = val;
734         else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
735                 at_max = val;
736         else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
737                 at_extra = val;
738         else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
739                 at_early_margin = val;
740         else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
741                 at_history = val;
742         else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
743                 strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
744                         JOBSTATS_JOBID_VAR_MAX_LEN + 1);
745         else
746                 return -EINVAL;
747
748         CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
749         return 0;
750 }
751
752 /* We can't call ll_process_config or lquota_process_config directly because
753  * it lives in a module that must be loaded after this one.
754  */
755 static int (*client_process_config)(struct lustre_cfg *lcfg);
756 static int (*quota_process_config)(struct lustre_cfg *lcfg);
757
758 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
759 {
760         client_process_config = cpc;
761 }
762 EXPORT_SYMBOL(lustre_register_client_process_config);
763
764 static int process_param2_config(struct lustre_cfg *lcfg)
765 {
766         char *param = lustre_cfg_string(lcfg, 1);
767         char *upcall = lustre_cfg_string(lcfg, 2);
768         char *argv[] = {
769                 [0] = "/usr/sbin/lctl",
770                 [1] = "set_param",
771                 [2] = param,
772                 [3] = NULL
773         };
774         ktime_t start;
775         ktime_t end;
776         int             rc;
777
778         /* Add upcall processing here. Now only lctl is supported */
779         if (strcmp(upcall, LCTL_UPCALL) != 0) {
780                 CERROR("Unsupported upcall %s\n", upcall);
781                 return -EINVAL;
782         }
783
784         start = ktime_get();
785         rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
786         end = ktime_get();
787
788         if (rc < 0) {
789                 CERROR(
790                        "lctl: error invoking upcall %s %s %s: rc = %d; time %ldus\n",
791                        argv[0], argv[1], argv[2], rc,
792                        (long)ktime_us_delta(end, start));
793         } else {
794                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
795                        argv[0], argv[1], argv[2],
796                        (long)ktime_us_delta(end, start));
797                        rc = 0;
798         }
799
800         return rc;
801 }
802
803 /** Process configuration commands given in lustre_cfg form.
804  * These may come from direct calls (e.g. class_manual_cleanup)
805  * or processing the config llog, or ioctl from lctl.
806  */
807 int class_process_config(struct lustre_cfg *lcfg)
808 {
809         struct obd_device *obd;
810         int err;
811
812         LASSERT(lcfg && !IS_ERR(lcfg));
813         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
814
815         /* Commands that don't need a device */
816         switch (lcfg->lcfg_command) {
817         case LCFG_ATTACH: {
818                 err = class_attach(lcfg);
819                 goto out;
820         }
821         case LCFG_ADD_UUID: {
822                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx (%s)\n",
823                        lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid,
824                        libcfs_nid2str(lcfg->lcfg_nid));
825
826                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
827                 goto out;
828         }
829         case LCFG_DEL_UUID: {
830                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
831                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
832                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
833
834                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
835                 goto out;
836         }
837         case LCFG_MOUNTOPT: {
838                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
839                        lustre_cfg_string(lcfg, 1),
840                        lustre_cfg_string(lcfg, 2),
841                        lustre_cfg_string(lcfg, 3));
842                 /* set these mount options somewhere, so ll_fill_super
843                  * can find them.
844                  */
845                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
846                                         lustre_cfg_string(lcfg, 1),
847                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
848                                         lustre_cfg_string(lcfg, 2),
849                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
850                                         lustre_cfg_string(lcfg, 3));
851                 goto out;
852         }
853         case LCFG_DEL_MOUNTOPT: {
854                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
855                        lustre_cfg_string(lcfg, 1));
856                 class_del_profile(lustre_cfg_string(lcfg, 1));
857                 err = 0;
858                 goto out;
859         }
860         case LCFG_SET_TIMEOUT: {
861                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
862                        obd_timeout, lcfg->lcfg_num);
863                 obd_timeout = max(lcfg->lcfg_num, 1U);
864                 obd_timeout_set = 1;
865                 err = 0;
866                 goto out;
867         }
868         case LCFG_SET_LDLM_TIMEOUT: {
869                 /* ldlm_timeout is not used on the client */
870                 err = 0;
871                 goto out;
872         }
873         case LCFG_SET_UPCALL: {
874                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
875                 /* COMPAT_146 Don't fail on old configs */
876                 err = 0;
877                 goto out;
878         }
879         case LCFG_MARKER: {
880                 struct cfg_marker *marker;
881
882                 marker = lustre_cfg_buf(lcfg, 1);
883                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
884                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
885                 err = 0;
886                 goto out;
887         }
888         case LCFG_PARAM: {
889                 char *tmp;
890                 /* llite has no obd */
891                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
892                                        PARAM_LLITE, NULL) == 0) &&
893                     client_process_config) {
894                         err = (*client_process_config)(lcfg);
895                         goto out;
896                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
897                                               PARAM_SYS, &tmp) == 0)) {
898                         /* Global param settings */
899                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
900                         /*
901                          * Client or server should not fail to mount if
902                          * it hits an unknown configuration parameter.
903                          */
904                         if (err != 0)
905                                 CWARN("Ignoring unknown param %s\n", tmp);
906
907                         err = 0;
908                         goto out;
909                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
910                                               PARAM_QUOTA, &tmp) == 0) &&
911                            quota_process_config) {
912                         err = (*quota_process_config)(lcfg);
913                         goto out;
914                 }
915
916                 break;
917         }
918         case LCFG_SET_PARAM: {
919                 err = process_param2_config(lcfg);
920                 goto out;
921         }
922         }
923         /* Commands that require a device */
924         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
925         if (!obd) {
926                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
927                         CERROR("this lcfg command requires a device name\n");
928                 else
929                         CERROR("no device for: %s\n",
930                                lustre_cfg_string(lcfg, 0));
931
932                 err = -EINVAL;
933                 goto out;
934         }
935
936         switch (lcfg->lcfg_command) {
937         case LCFG_SETUP: {
938                 err = class_setup(obd, lcfg);
939                 goto out;
940         }
941         case LCFG_DETACH: {
942                 err = class_detach(obd, lcfg);
943                 err = 0;
944                 goto out;
945         }
946         case LCFG_CLEANUP: {
947                 err = class_cleanup(obd, lcfg);
948                 err = 0;
949                 goto out;
950         }
951         case LCFG_ADD_CONN: {
952                 err = class_add_conn(obd, lcfg);
953                 err = 0;
954                 goto out;
955         }
956         case LCFG_DEL_CONN: {
957                 err = class_del_conn(obd, lcfg);
958                 err = 0;
959                 goto out;
960         }
961         case LCFG_POOL_NEW: {
962                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
963                 err = 0;
964                 goto out;
965         }
966         case LCFG_POOL_ADD: {
967                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
968                                    lustre_cfg_string(lcfg, 3));
969                 err = 0;
970                 goto out;
971         }
972         case LCFG_POOL_REM: {
973                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
974                                    lustre_cfg_string(lcfg, 3));
975                 err = 0;
976                 goto out;
977         }
978         case LCFG_POOL_DEL: {
979                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
980                 err = 0;
981                 goto out;
982         }
983         default: {
984                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
985                 goto out;
986         }
987         }
988 out:
989         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
990                 CWARN("Ignoring error %d on optional command %#x\n", err,
991                       lcfg->lcfg_command);
992                 err = 0;
993         }
994         return err;
995 }
996 EXPORT_SYMBOL(class_process_config);
997
998 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
999                              struct lustre_cfg *lcfg, void *data)
1000 {
1001         struct lprocfs_vars *var;
1002         struct file fakefile;
1003         struct seq_file fake_seqfile;
1004         char *key, *sval;
1005         int i, keylen, vallen;
1006         int matched = 0, j = 0;
1007         int rc = 0;
1008         int skip = 0;
1009
1010         if (lcfg->lcfg_command != LCFG_PARAM) {
1011                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1012                 return -EINVAL;
1013         }
1014
1015         /* fake a seq file so that var->fops->write can work... */
1016         fakefile.private_data = &fake_seqfile;
1017         fake_seqfile.private = data;
1018         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1019          * or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1020          * or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1021          */
1022         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1023                 key = lustre_cfg_buf(lcfg, i);
1024                 /* Strip off prefix */
1025                 if (class_match_param(key, prefix, &key)) {
1026                         /*
1027                          * If the prefix doesn't match, return error so we
1028                          * can pass it down the stack
1029                          */
1030                         return -ENOSYS;
1031                 }
1032                 sval = strchr(key, '=');
1033                 if (!sval || (*(sval + 1) == 0)) {
1034                         CERROR("Can't parse param %s (missing '=')\n", key);
1035                         /* rc = -EINVAL;        continue parsing other params */
1036                         continue;
1037                 }
1038                 keylen = sval - key;
1039                 sval++;
1040                 vallen = strlen(sval);
1041                 matched = 0;
1042                 j = 0;
1043                 /* Search proc entries */
1044                 while (lvars[j].name) {
1045                         var = &lvars[j];
1046                         if (!class_match_param(key, var->name, NULL) &&
1047                             keylen == strlen(var->name)) {
1048                                 matched++;
1049                                 rc = -EROFS;
1050                                 if (var->fops && var->fops->write) {
1051                                         mm_segment_t oldfs;
1052
1053                                         oldfs = get_fs();
1054                                         set_fs(KERNEL_DS);
1055                                         rc = var->fops->write(&fakefile,
1056                                                 (const char __user *)sval,
1057                                                                 vallen, NULL);
1058                                         set_fs(oldfs);
1059                                 }
1060                                 break;
1061                         }
1062                         j++;
1063                 }
1064                 if (!matched) {
1065                         CERROR("%.*s: %s unknown param %s\n",
1066                                (int)strlen(prefix) - 1, prefix,
1067                                (char *)lustre_cfg_string(lcfg, 0), key);
1068                         /* rc = -EINVAL;        continue parsing other params */
1069                         skip++;
1070                 } else if (rc < 0) {
1071                         CERROR("%s: error writing proc entry '%s': rc = %d\n",
1072                                prefix, var->name, rc);
1073                         rc = 0;
1074                 } else {
1075                         CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1076                                lustre_cfg_string(lcfg, 0),
1077                                (int)strlen(prefix) - 1, prefix,
1078                                (int)(sval - key - 1), key, sval);
1079                 }
1080         }
1081
1082         if (rc > 0)
1083                 rc = 0;
1084         if (!rc && skip)
1085                 rc = skip;
1086         return rc;
1087 }
1088 EXPORT_SYMBOL(class_process_proc_param);
1089
1090 /** Parse a configuration llog, doing various manipulations on them
1091  * for various reasons, (modifications for compatibility, skip obsolete
1092  * records, change uuids, etc), then class_process_config() resulting
1093  * net records.
1094  */
1095 int class_config_llog_handler(const struct lu_env *env,
1096                               struct llog_handle *handle,
1097                               struct llog_rec_hdr *rec, void *data)
1098 {
1099         struct config_llog_instance *clli = data;
1100         int cfg_len = rec->lrh_len;
1101         char *cfg_buf = (char *)(rec + 1);
1102         int rc = 0;
1103
1104         switch (rec->lrh_type) {
1105         case OBD_CFG_REC: {
1106                 struct lustre_cfg *lcfg, *lcfg_new;
1107                 struct lustre_cfg_bufs bufs;
1108                 char *inst_name = NULL;
1109                 int inst_len = 0;
1110                 int inst = 0, swab = 0;
1111
1112                 lcfg = (struct lustre_cfg *)cfg_buf;
1113                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1114                         lustre_swab_lustre_cfg(lcfg);
1115                         swab = 1;
1116                 }
1117
1118                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1119                 if (rc)
1120                         goto out;
1121
1122                 /* Figure out config state info */
1123                 if (lcfg->lcfg_command == LCFG_MARKER) {
1124                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1125
1126                         lustre_swab_cfg_marker(marker, swab,
1127                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1128                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1129                                clli->cfg_flags, marker->cm_flags);
1130                         if (marker->cm_flags & CM_START) {
1131                                 /* all previous flags off */
1132                                 clli->cfg_flags = CFG_F_MARKER;
1133                                 if (marker->cm_flags & CM_SKIP) {
1134                                         clli->cfg_flags |= CFG_F_SKIP;
1135                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1136                                                marker->cm_step);
1137                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1138                                            (clli->cfg_sb &&
1139                                             lustre_check_exclusion(clli->cfg_sb,
1140                                                          marker->cm_tgtname))) {
1141                                         clli->cfg_flags |= CFG_F_EXCLUDE;
1142                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1143                                                marker->cm_step);
1144                                 }
1145                         } else if (marker->cm_flags & CM_END) {
1146                                 clli->cfg_flags = 0;
1147                         }
1148                 }
1149                 /* A config command without a start marker before it is
1150                  * illegal (post 146)
1151                  */
1152                 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
1153                     !(clli->cfg_flags & CFG_F_MARKER) &&
1154                     (lcfg->lcfg_command != LCFG_MARKER)) {
1155                         CWARN("Config not inside markers, ignoring! (inst: %p, uuid: %s, flags: %#x)\n",
1156                               clli->cfg_instance,
1157                               clli->cfg_uuid.uuid, clli->cfg_flags);
1158                         clli->cfg_flags |= CFG_F_SKIP;
1159                 }
1160                 if (clli->cfg_flags & CFG_F_SKIP) {
1161                         CDEBUG(D_CONFIG, "skipping %#x\n",
1162                                clli->cfg_flags);
1163                         rc = 0;
1164                         /* No processing! */
1165                         break;
1166                 }
1167
1168                 /*
1169                  * For interoperability between 1.8 and 2.0,
1170                  * rename "mds" obd device type to "mdt".
1171                  */
1172                 {
1173                         char *typename = lustre_cfg_string(lcfg, 1);
1174                         char *index = lustre_cfg_string(lcfg, 2);
1175
1176                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1177                              strcmp(typename, "mds") == 0)) {
1178                                 CWARN("For 1.8 interoperability, rename obd type from mds to mdt\n");
1179                                 typename[2] = 't';
1180                         }
1181                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1182                              strcmp(index, "type") == 0)) {
1183                                 CDEBUG(D_INFO, "For 1.8 interoperability, set this index to '0'\n");
1184                                 index[0] = '0';
1185                                 index[1] = 0;
1186                         }
1187                 }
1188
1189                 if (clli->cfg_flags & CFG_F_EXCLUDE) {
1190                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1191                                lcfg->lcfg_command);
1192                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1193                                 /* Add inactive instead */
1194                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1195                 }
1196
1197                 lustre_cfg_bufs_init(&bufs, lcfg);
1198
1199                 if (clli && clli->cfg_instance &&
1200                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1201                         inst = 1;
1202                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1203                                    sizeof(clli->cfg_instance) * 2 + 4;
1204                         inst_name = kasprintf(GFP_NOFS, "%s-%p",
1205                                               lustre_cfg_string(lcfg, 0),
1206                                               clli->cfg_instance);
1207                         if (!inst_name) {
1208                                 rc = -ENOMEM;
1209                                 goto out;
1210                         }
1211                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1212                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1213                                lcfg->lcfg_command, inst_name);
1214                 }
1215
1216                 /* we override the llog's uuid for clients, to insure they
1217                  * are unique
1218                  */
1219                 if (clli && clli->cfg_instance &&
1220                     lcfg->lcfg_command == LCFG_ATTACH) {
1221                         lustre_cfg_bufs_set_string(&bufs, 2,
1222                                                    clli->cfg_uuid.uuid);
1223                 }
1224                 /*
1225                  * sptlrpc config record, we expect 2 data segments:
1226                  *  [0]: fs_name/target_name,
1227                  *  [1]: rule string
1228                  * moving them to index [1] and [2], and insert MGC's
1229                  * obdname at index [0].
1230                  */
1231                 if (clli && !clli->cfg_instance &&
1232                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1233                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1234                                             bufs.lcfg_buflen[1]);
1235                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1236                                             bufs.lcfg_buflen[0]);
1237                         lustre_cfg_bufs_set_string(&bufs, 0,
1238                                                    clli->cfg_obdname);
1239                 }
1240
1241                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1242
1243                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1244                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1245
1246                 /* XXX Hack to try to remain binary compatible with
1247                  * pre-newconfig logs
1248                  */
1249                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1250                     (lcfg->lcfg_nid >> 32) == 0) {
1251                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1252
1253                         lcfg_new->lcfg_nid =
1254                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1255                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1256                               lcfg->lcfg_nal, addr,
1257                               libcfs_nid2str(lcfg_new->lcfg_nid));
1258                 } else {
1259                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1260                 }
1261
1262                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1263
1264                 rc = class_process_config(lcfg_new);
1265                 lustre_cfg_free(lcfg_new);
1266
1267                 if (inst)
1268                         kfree(inst_name);
1269                 break;
1270         }
1271         default:
1272                 CERROR("Unknown llog record type %#x encountered\n",
1273                        rec->lrh_type);
1274                 break;
1275         }
1276 out:
1277         if (rc) {
1278                 CERROR("%s: cfg command failed: rc = %d\n",
1279                        handle->lgh_ctxt->loc_obd->obd_name, rc);
1280                 class_config_dump_handler(NULL, handle, rec, data);
1281         }
1282         return rc;
1283 }
1284 EXPORT_SYMBOL(class_config_llog_handler);
1285
1286 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1287                             char *name, struct config_llog_instance *cfg)
1288 {
1289         struct llog_process_cat_data     cd = {0, 0};
1290         struct llog_handle              *llh;
1291         llog_cb_t                        callback;
1292         int                              rc;
1293
1294         CDEBUG(D_INFO, "looking up llog %s\n", name);
1295         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1296         if (rc)
1297                 return rc;
1298
1299         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1300         if (rc)
1301                 goto parse_out;
1302
1303         /* continue processing from where we last stopped to end-of-log */
1304         if (cfg) {
1305                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1306                 callback = cfg->cfg_callback;
1307                 LASSERT(callback);
1308         } else {
1309                 callback = class_config_llog_handler;
1310         }
1311
1312         cd.lpcd_last_idx = 0;
1313
1314         rc = llog_process(env, llh, callback, cfg, &cd);
1315
1316         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1317                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1318         if (cfg)
1319                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1320
1321 parse_out:
1322         llog_close(env, llh);
1323         return rc;
1324 }
1325 EXPORT_SYMBOL(class_config_parse_llog);
1326
1327 /**
1328  * parse config record and output dump in supplied buffer.
1329  * This is separated from class_config_dump_handler() to use
1330  * for ioctl needs as well
1331  */
1332 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf,
1333                                   int size)
1334 {
1335         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1336         char                    *ptr = buf;
1337         char                    *end = buf + size;
1338         int                      rc = 0;
1339
1340         LASSERT(rec->lrh_type == OBD_CFG_REC);
1341         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1342         if (rc < 0)
1343                 return rc;
1344
1345         ptr += snprintf(ptr, end - ptr, "cmd=%05x ", lcfg->lcfg_command);
1346         if (lcfg->lcfg_flags)
1347                 ptr += snprintf(ptr, end - ptr, "flags=%#08x ",
1348                                 lcfg->lcfg_flags);
1349
1350         if (lcfg->lcfg_num)
1351                 ptr += snprintf(ptr, end - ptr, "num=%#08x ", lcfg->lcfg_num);
1352
1353         if (lcfg->lcfg_nid) {
1354                 char nidstr[LNET_NIDSTR_SIZE];
1355
1356                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1357                 ptr += snprintf(ptr, end - ptr, "nid=%s(%#llx)\n     ",
1358                                 nidstr, lcfg->lcfg_nid);
1359         }
1360
1361         if (lcfg->lcfg_command == LCFG_MARKER) {
1362                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1363
1364                 ptr += snprintf(ptr, end - ptr, "marker=%d(%#x)%s '%s'",
1365                                 marker->cm_step, marker->cm_flags,
1366                                 marker->cm_tgtname, marker->cm_comment);
1367         } else {
1368                 int i;
1369
1370                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1371                         ptr += snprintf(ptr, end - ptr, "%d:%s  ", i,
1372                                         lustre_cfg_string(lcfg, i));
1373                 }
1374         }
1375         ptr += snprintf(ptr, end - ptr, "\n");
1376         /* return consumed bytes */
1377         rc = ptr - buf;
1378         return rc;
1379 }
1380
1381 int class_config_dump_handler(const struct lu_env *env,
1382                               struct llog_handle *handle,
1383                               struct llog_rec_hdr *rec, void *data)
1384 {
1385         char    *outstr;
1386         int      rc = 0;
1387
1388         outstr = kzalloc(256, GFP_NOFS);
1389         if (!outstr)
1390                 return -ENOMEM;
1391
1392         if (rec->lrh_type == OBD_CFG_REC) {
1393                 class_config_parse_rec(rec, outstr, 256);
1394                 LCONSOLE(D_WARNING, "   %s", outstr);
1395         } else {
1396                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1397                 rc = -EINVAL;
1398         }
1399
1400         kfree(outstr);
1401         return rc;
1402 }
1403
1404 /** Call class_cleanup and class_detach.
1405  * "Manual" only in the sense that we're faking lcfg commands.
1406  */
1407 int class_manual_cleanup(struct obd_device *obd)
1408 {
1409         char                flags[3] = "";
1410         struct lustre_cfg      *lcfg;
1411         struct lustre_cfg_bufs  bufs;
1412         int                  rc;
1413
1414         if (!obd) {
1415                 CERROR("empty cleanup\n");
1416                 return -EALREADY;
1417         }
1418
1419         if (obd->obd_force)
1420                 strcat(flags, "F");
1421         if (obd->obd_fail)
1422                 strcat(flags, "A");
1423
1424         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1425                obd->obd_name, flags);
1426
1427         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1428         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1429         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1430         if (IS_ERR(lcfg))
1431                 return PTR_ERR(lcfg);
1432
1433         rc = class_process_config(lcfg);
1434         if (rc) {
1435                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1436                 goto out;
1437         }
1438
1439         /* the lcfg is almost the same for both ops */
1440         lcfg->lcfg_command = LCFG_DETACH;
1441         rc = class_process_config(lcfg);
1442         if (rc)
1443                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1444 out:
1445         lustre_cfg_free(lcfg);
1446         return rc;
1447 }
1448 EXPORT_SYMBOL(class_manual_cleanup);
1449
1450 /*
1451  * uuid<->export lustre hash operations
1452  */
1453
1454 static unsigned int
1455 uuid_hash(struct cfs_hash *hs, const void *key, unsigned int mask)
1456 {
1457         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
1458                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
1459 }
1460
1461 static void *
1462 uuid_key(struct hlist_node *hnode)
1463 {
1464         struct obd_export *exp;
1465
1466         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1467
1468         return &exp->exp_client_uuid;
1469 }
1470
1471 /*
1472  * NOTE: It is impossible to find an export that is in failed
1473  *       state with this function
1474  */
1475 static int
1476 uuid_keycmp(const void *key, struct hlist_node *hnode)
1477 {
1478         struct obd_export *exp;
1479
1480         LASSERT(key);
1481         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1482
1483         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
1484                !exp->exp_failed;
1485 }
1486
1487 static void *
1488 uuid_export_object(struct hlist_node *hnode)
1489 {
1490         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1491 }
1492
1493 static void
1494 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
1495 {
1496         struct obd_export *exp;
1497
1498         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1499         class_export_get(exp);
1500 }
1501
1502 static void
1503 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1504 {
1505         struct obd_export *exp;
1506
1507         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1508         class_export_put(exp);
1509 }
1510
1511 static struct cfs_hash_ops uuid_hash_ops = {
1512         .hs_hash        = uuid_hash,
1513         .hs_key         = uuid_key,
1514         .hs_keycmp      = uuid_keycmp,
1515         .hs_object      = uuid_export_object,
1516         .hs_get         = uuid_export_get,
1517         .hs_put_locked  = uuid_export_put_locked,
1518 };