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