Whamcloud - gitweb
48112052c2c264e2032719cdc930c34ee33b8f7f
[fs/lustre-release.git] / lustre / obdclass / obd_config.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/obd_config.c
37  *
38  * Config API
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42 #ifdef __KERNEL__
43 #include <obd_class.h>
44 #include <linux/string.h>
45 #else
46 #include <liblustre.h>
47 #include <obd_class.h>
48 #include <obd.h>
49 #endif
50 #include <lustre_log.h>
51 #include <lprocfs_status.h>
52 #include <libcfs/list.h>
53 #include <lustre_param.h>
54 #include <class_hash.h>
55
56 extern struct lustre_hash_operations uuid_hash_operations;
57 extern struct lustre_hash_operations nid_hash_operations;
58
59 /*********** string parsing utils *********/
60
61 /* returns 0 if we find this key in the buffer, else 1 */
62 int class_find_param(char *buf, char *key, char **valp)
63 {
64         char *ptr;
65
66         if (!buf) 
67                 return 1;
68
69         if ((ptr = strstr(buf, key)) == NULL) 
70                 return 1;
71
72         if (valp) 
73                 *valp = ptr + strlen(key);
74         
75         return 0;
76 }
77
78 /* returns 0 if this is the first key in the buffer, else 1.
79    valp points to first char after key. */
80 int class_match_param(char *buf, char *key, char **valp)
81 {
82         if (!buf) 
83                 return 1;
84
85         if (memcmp(buf, key, strlen(key)) != 0) 
86                 return 1;
87
88         if (valp) 
89                 *valp = buf + strlen(key);
90         
91         return 0;
92 }
93
94 /* 0 is good nid, 
95    1 not found
96    < 0 error
97    endh is set to next separator */
98 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
99 {
100         char tmp, *endp;
101
102         if (!buf) 
103                 return 1;
104         while (*buf == ',' || *buf == ':') 
105                 buf++;
106         if (*buf == ' ' || *buf == '/' || *buf == '\0') 
107                 return 1;
108
109         /* nid separators or end of nids */
110         endp = strpbrk(buf, ",: /");
111         if (endp == NULL) 
112                 endp = buf + strlen(buf);
113
114         tmp = *endp;
115         *endp = '\0';
116         *nid = libcfs_str2nid(buf);
117         if (*nid == LNET_NID_ANY) {
118                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
119                 *endp = tmp;
120                 return -EINVAL;
121         }
122         *endp = tmp;
123
124         if (endh) 
125                 *endh = endp;
126         CDEBUG(D_INFO, "Nid %s\n", libcfs_nid2str(*nid));
127         return 0;
128 }
129
130 EXPORT_SYMBOL(class_find_param);
131 EXPORT_SYMBOL(class_match_param);
132 EXPORT_SYMBOL(class_parse_nid);
133
134 /********************** class fns **********************/
135
136 /**
137  * Create a new device and set the type, name and uuid.  If successful, the new
138  * device can be accessed by either name or uuid.
139  */
140 int class_attach(struct lustre_cfg *lcfg)
141 {
142         struct obd_device *obd = NULL;
143         char *typename, *name, *uuid;
144         int rc, len;
145         ENTRY;
146
147         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
148                 CERROR("No type passed!\n");
149                 RETURN(-EINVAL);
150         }
151         typename = lustre_cfg_string(lcfg, 1);
152
153         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
154                 CERROR("No name passed!\n");
155                 RETURN(-EINVAL);
156         }
157         name = lustre_cfg_string(lcfg, 0);
158
159         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
160                 CERROR("No UUID passed!\n");
161                 RETURN(-EINVAL);
162         }
163         uuid = lustre_cfg_string(lcfg, 2);
164
165         CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
166                MKSTR(typename), MKSTR(name), MKSTR(uuid));
167
168         obd = class_newdev(typename, name);
169         if (IS_ERR(obd)) {
170                 /* Already exists or out of obds */
171                 rc = PTR_ERR(obd);
172                 obd = NULL;
173                 CERROR("Cannot create device %s of type %s : %d\n",
174                        name, typename, rc);
175                 GOTO(out, rc);
176         }
177         LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
178                  name, typename);
179         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, 
180                  "obd %p obd_magic %08X != %08X\n",
181                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
182         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0, "%p obd_name %s != %s\n",
183                  obd, obd->obd_name, name);
184
185         rwlock_init(&obd->obd_pool_lock);
186         obd->obd_pool_limit = 0;
187         obd->obd_pool_slv = 0;
188
189         CFS_INIT_LIST_HEAD(&obd->obd_exports);
190         CFS_INIT_LIST_HEAD(&obd->obd_exports_timed);
191         CFS_INIT_LIST_HEAD(&obd->obd_nid_stats);
192         spin_lock_init(&obd->obd_nid_lock);
193         spin_lock_init(&obd->obd_dev_lock);
194         sema_init(&obd->obd_dev_sem, 1);
195         spin_lock_init(&obd->obd_osfs_lock);
196         /* obd->obd_osfs_age must be set to a value in the distant
197          * past to guarantee a fresh statfs is fetched on mount. */
198         obd->obd_osfs_age = cfs_time_shift_64(-1000);
199
200         /* XXX belongs in setup not attach  */
201         /* recovery data */
202         cfs_init_timer(&obd->obd_recovery_timer);
203         spin_lock_init(&obd->obd_processing_task_lock);
204         cfs_waitq_init(&obd->obd_next_transno_waitq);
205         cfs_waitq_init(&obd->obd_evict_inprogress_waitq);
206         CFS_INIT_LIST_HEAD(&obd->obd_req_replay_queue);
207         CFS_INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
208         CFS_INIT_LIST_HEAD(&obd->obd_final_req_queue);
209
210         llog_group_init(&obd->obd_olg, OBD_LLOG_GROUP);
211
212         spin_lock_init(&obd->obd_uncommitted_replies_lock);
213         CFS_INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
214
215         len = strlen(uuid);
216         if (len >= sizeof(obd->obd_uuid)) {
217                 CERROR("uuid must be < %d bytes long\n",
218                        (int)sizeof(obd->obd_uuid));
219                 GOTO(out, rc = -EINVAL);
220         }
221         memcpy(obd->obd_uuid.uuid, uuid, len);
222
223         /* do the attach */
224         if (OBP(obd, attach)) {
225                 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
226                 if (rc)
227                         GOTO(out, rc = -EINVAL);
228         }
229
230         /* Detach drops this */
231         spin_lock(&obd->obd_dev_lock);
232         atomic_set(&obd->obd_refcount, 1);
233         spin_unlock(&obd->obd_dev_lock);
234
235         obd->obd_attached = 1;
236         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
237                obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
238         RETURN(0);
239  out:
240         if (obd != NULL) {
241                 class_release_dev(obd);
242         }
243         return rc;
244 }
245
246 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
247 {
248         int err = 0;
249         struct obd_export *exp;
250         ENTRY;
251
252         LASSERT(obd != NULL);
253         LASSERTF(obd == class_num2obd(obd->obd_minor), "obd %p != obd_devs[%d] %p\n", 
254                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
255         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n", 
256                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
257
258         /* have we attached a type to this device? */
259         if (!obd->obd_attached) {
260                 CERROR("Device %d not attached\n", obd->obd_minor);
261                 RETURN(-ENODEV);
262         }
263
264         if (obd->obd_set_up) {
265                 CERROR("Device %d already setup (type %s)\n",
266                        obd->obd_minor, obd->obd_type->typ_name);
267                 RETURN(-EEXIST);
268         }
269
270         /* is someone else setting us up right now? (attach inits spinlock) */
271         spin_lock(&obd->obd_dev_lock);
272         if (obd->obd_starting) {
273                 spin_unlock(&obd->obd_dev_lock);
274                 CERROR("Device %d setup in progress (type %s)\n",
275                        obd->obd_minor, obd->obd_type->typ_name);
276                 RETURN(-EEXIST);
277         }
278         /* just leave this on forever.  I can't use obd_set_up here because
279            other fns check that status, and we're not actually set up yet. */
280         obd->obd_starting = 1;
281         spin_unlock(&obd->obd_dev_lock);
282
283         /* create an uuid-export hash body */
284         err = lustre_hash_init(&obd->obd_uuid_hash_body, "UUID_HASH", 
285                                128, &uuid_hash_operations);
286         if (err)
287                 GOTO(err_hash, err);
288
289         /* create a nid-export hash body */
290         err = lustre_hash_init(&obd->obd_nid_hash_body, "NID_HASH", 
291                                128, &nid_hash_operations);
292         if (err)
293                 GOTO(err_hash, err);
294
295         /* create a nid-stats hash body */
296         err = lustre_hash_init(&obd->obd_nid_stats_hash_body, "NID_STATS",
297                                128, &nid_stat_hash_operations);
298         if (err)
299                 GOTO(err_hash, err);
300
301         exp = class_new_export(obd, &obd->obd_uuid);
302         if (IS_ERR(exp))
303                 RETURN(PTR_ERR(exp));
304
305         obd->obd_self_export = exp;
306         list_del_init(&exp->exp_obd_chain_timed);
307         class_export_put(exp);
308
309         err = obd_setup(obd, lcfg);
310         if (err)
311                 GOTO(err_exp, err);
312
313         obd->obd_set_up = 1;
314         
315         spin_lock(&obd->obd_dev_lock);
316         /* cleanup drops this */
317         class_incref(obd);
318         spin_unlock(&obd->obd_dev_lock);
319
320         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
321                obd->obd_name, obd->obd_uuid.uuid);
322
323         RETURN(0);
324
325 err_exp:
326         class_unlink_export(obd->obd_self_export);
327         obd->obd_self_export = NULL;
328 err_hash:
329         lustre_hash_exit(&obd->obd_uuid_hash_body);
330         lustre_hash_exit(&obd->obd_nid_hash_body);
331         lustre_hash_exit(&obd->obd_nid_stats_hash_body);
332         obd->obd_starting = 0;
333         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
334         RETURN(err);
335 }
336
337 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
338 {
339         ENTRY;
340
341         if (obd->obd_set_up) {
342                 CERROR("OBD device %d still set up\n", obd->obd_minor);
343                 RETURN(-EBUSY);
344         }
345
346         spin_lock(&obd->obd_dev_lock);
347         if (!obd->obd_attached) {
348                 spin_unlock(&obd->obd_dev_lock);
349                 CERROR("OBD device %d not attached\n", obd->obd_minor);
350                 RETURN(-ENODEV);
351         }
352         obd->obd_attached = 0;
353         spin_unlock(&obd->obd_dev_lock);
354
355         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
356                obd->obd_name, obd->obd_uuid.uuid);
357
358         class_decref(obd);
359
360         /* not strictly necessary, but cleans up eagerly */
361         obd_zombie_impexp_cull();
362
363         RETURN(0);
364 }
365
366 static void dump_exports(struct obd_device *obd)
367 {
368         struct obd_export *exp, *n;
369
370         list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
371                 struct ptlrpc_reply_state *rs;
372                 struct ptlrpc_reply_state *first_reply = NULL;
373                 int                        nreplies = 0;
374
375                 list_for_each_entry (rs, &exp->exp_outstanding_replies,
376                                      rs_exp_list) {
377                         if (nreplies == 0)
378                                 first_reply = rs;
379                         nreplies++;
380                 }
381
382                 CDEBUG(D_IOCTL, "%s: %p %s %s %d %d %d: %p %s\n",
383                        obd->obd_name, exp, exp->exp_client_uuid.uuid,
384                        obd_export_nid2str(exp),
385                        atomic_read(&exp->exp_refcount),
386                        exp->exp_failed, nreplies, first_reply,
387                        nreplies > 3 ? "..." : "");
388         }
389 }
390
391 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
392 {
393         int err = 0;
394         char *flag;
395         ENTRY;
396
397         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
398
399         if (!obd->obd_set_up) {
400                 CERROR("Device %d not setup\n", obd->obd_minor);
401                 RETURN(-ENODEV);
402         }
403
404         spin_lock(&obd->obd_dev_lock);
405         if (obd->obd_stopping) {
406                 spin_unlock(&obd->obd_dev_lock);
407                 CERROR("OBD %d already stopping\n", obd->obd_minor);
408                 RETURN(-ENODEV);
409         }
410         /* Leave this on forever */
411         obd->obd_stopping = 1;
412         spin_unlock(&obd->obd_dev_lock);
413
414         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
415                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
416                         switch (*flag) {
417                         case 'F':
418                                 obd->obd_force = 1;
419                                 break;
420                         case 'A':
421                                 LCONSOLE_WARN("Failing over %s\n",
422                                               obd->obd_name);
423                                 obd->obd_fail = 1;
424                                 obd->obd_no_transno = 1;
425                                 obd->obd_no_recov = 1;
426                                 /* Set the obd readonly if we can */
427                                 if (OBP(obd, iocontrol))
428                                         obd_iocontrol(OBD_IOC_SET_READONLY,
429                                                       obd->obd_self_export,
430                                                       0, NULL, NULL);
431                                 break;
432                         default:
433                                 CERROR("unrecognised flag '%c'\n",
434                                        *flag);
435                         }
436         }
437
438         /* The three references that should be remaining are the
439          * obd_self_export and the attach and setup references. */
440         if (atomic_read(&obd->obd_refcount) > 3) {
441 #if 0           /* We should never fail to cleanup with mountconf */ 
442                 if (!(obd->obd_fail || obd->obd_force)) {
443                         CERROR("OBD %s is still busy with %d references\n"
444                                "You should stop active file system users,"
445                                " or use the --force option to cleanup.\n",
446                                obd->obd_name, atomic_read(&obd->obd_refcount));
447                         dump_exports(obd);
448                         /* Allow a failed cleanup to try again. */
449                         obd->obd_stopping = 0;
450                 }
451 #endif
452                 /* refcounf - 3 might be the number of real exports 
453                    (excluding self export). But class_incref is called
454                    by other things as well, so don't count on it. */
455                 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
456                        obd->obd_name, atomic_read(&obd->obd_refcount) - 3);
457                 dump_exports(obd);
458                 class_disconnect_exports(obd);
459         }
460         LASSERT(obd->obd_self_export);
461
462         /* destroy an uuid-export hash body */
463         lustre_hash_exit(&obd->obd_uuid_hash_body);
464
465         /* destroy a nid-export hash body */
466         lustre_hash_exit(&obd->obd_nid_hash_body);
467
468         /* destroy a nid-stats hash body */
469         lustre_hash_exit(&obd->obd_nid_stats_hash_body);
470
471         /* Precleanup, we must make sure all exports get destroyed. */
472         err = obd_precleanup(obd, OBD_CLEANUP_EXPORTS);
473         if (err)
474                 CERROR("Precleanup %s returned %d\n",
475                        obd->obd_name, err);
476
477         class_decref(obd);
478         obd->obd_set_up = 0;
479         RETURN(0);
480 }
481
482 struct obd_device *class_incref(struct obd_device *obd)
483 {
484         atomic_inc(&obd->obd_refcount);
485         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
486                atomic_read(&obd->obd_refcount));
487
488         return obd;
489 }
490
491 void class_decref(struct obd_device *obd)
492 {
493         int err;
494         int refs;
495
496         spin_lock(&obd->obd_dev_lock);
497         atomic_dec(&obd->obd_refcount);
498         refs = atomic_read(&obd->obd_refcount);
499         spin_unlock(&obd->obd_dev_lock);
500
501         CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
502
503         if ((refs == 1) && obd->obd_stopping) {
504                 /* All exports have been destroyed; there should
505                    be no more in-progress ops by this point.*/
506
507                 spin_lock(&obd->obd_self_export->exp_lock);
508                 obd->obd_self_export->exp_flags |=
509                         (obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
510                         (obd->obd_force ? OBD_OPT_FORCE : 0);
511                 spin_unlock(&obd->obd_self_export->exp_lock);
512
513                 /* note that we'll recurse into class_decref again */
514                 class_unlink_export(obd->obd_self_export);
515                 return;
516         }
517
518         if (refs == 0) {
519                 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
520                        obd->obd_name, obd->obd_uuid.uuid);
521                 LASSERT(!obd->obd_attached);
522                 if (obd->obd_stopping) {
523                         /* If we're not stopping, we were never set up */
524                         err = obd_cleanup(obd);
525                         if (err)
526                                 CERROR("Cleanup %s returned %d\n",
527                                        obd->obd_name, err);
528                 }
529                 if (OBP(obd, detach)) {
530                         err = OBP(obd,detach)(obd);
531                         if (err)
532                                 CERROR("Detach returned %d\n", err);
533                 }
534                 class_release_dev(obd);
535         }
536 }
537
538 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
539 {
540         struct obd_import *imp;
541         struct obd_uuid uuid;
542         int rc;
543         ENTRY;
544
545         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
546             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
547                 CERROR("invalid conn_uuid\n");
548                 RETURN(-EINVAL);
549         }
550         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
551             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
552             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
553                 CERROR("can't add connection on non-client dev\n");
554                 RETURN(-EINVAL);
555         }
556
557         imp = obd->u.cli.cl_import;
558         if (!imp) {
559                 CERROR("try to add conn on immature client dev\n");
560                 RETURN(-EINVAL);
561         }
562
563         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
564         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
565
566         RETURN(rc);
567 }
568
569 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
570 {
571         struct obd_import *imp;
572         struct obd_uuid uuid;
573         int rc;
574         ENTRY;
575
576         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
577             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
578                 CERROR("invalid conn_uuid\n");
579                 RETURN(-EINVAL);
580         }
581         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
582             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
583                 CERROR("can't del connection on non-client dev\n");
584                 RETURN(-EINVAL);
585         }
586
587         imp = obd->u.cli.cl_import;
588         if (!imp) {
589                 CERROR("try to del conn on immature client dev\n");
590                 RETURN(-EINVAL);
591         }
592
593         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
594         rc = obd_del_conn(imp, &uuid);
595
596         RETURN(rc);
597 }
598
599 CFS_LIST_HEAD(lustre_profile_list);
600
601 struct lustre_profile *class_get_profile(const char * prof)
602 {
603         struct lustre_profile *lprof;
604
605         ENTRY;
606         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
607                 if (!strcmp(lprof->lp_profile, prof)) {
608                         RETURN(lprof);
609                 }
610         }
611         RETURN(NULL);
612 }
613
614 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         ENTRY;
620
621         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
622
623         OBD_ALLOC(lprof, sizeof(*lprof));
624         if (lprof == NULL)
625                 RETURN(-ENOMEM);
626         CFS_INIT_LIST_HEAD(&lprof->lp_list);
627
628         LASSERT(proflen == (strlen(prof) + 1));
629         OBD_ALLOC(lprof->lp_profile, proflen);
630         if (lprof->lp_profile == NULL)
631                 GOTO(out, err = -ENOMEM);
632         memcpy(lprof->lp_profile, prof, proflen);
633
634         LASSERT(osclen == (strlen(osc) + 1));
635         OBD_ALLOC(lprof->lp_dt, osclen);
636         if (lprof->lp_dt == NULL)
637                 GOTO(out, err = -ENOMEM);
638         memcpy(lprof->lp_dt, osc, osclen);
639
640         if (mdclen > 0) {
641                 LASSERT(mdclen == (strlen(mdc) + 1));
642                 OBD_ALLOC(lprof->lp_md, mdclen);
643                 if (lprof->lp_md == NULL)
644                         GOTO(out, err = -ENOMEM);
645                 memcpy(lprof->lp_md, mdc, mdclen);
646         }
647
648         list_add(&lprof->lp_list, &lustre_profile_list);
649         RETURN(err);
650
651 out:
652         if (lprof->lp_md)
653                 OBD_FREE(lprof->lp_md, mdclen);
654         if (lprof->lp_dt)
655                 OBD_FREE(lprof->lp_dt, osclen);
656         if (lprof->lp_profile)
657                 OBD_FREE(lprof->lp_profile, proflen);
658         OBD_FREE(lprof, sizeof(*lprof));        
659         RETURN(err);
660 }
661
662 void class_del_profile(const char *prof)
663 {
664         struct lustre_profile *lprof;
665         ENTRY;
666
667         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
668
669         lprof = class_get_profile(prof);
670         if (lprof) {
671                 list_del(&lprof->lp_list);
672                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
673                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
674                 if (lprof->lp_md)
675                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
676                 OBD_FREE(lprof, sizeof *lprof);
677         }
678         EXIT;
679 }
680
681 /* COMPAT_146 */
682 void class_del_profiles(void)
683 {
684         struct lustre_profile *lprof, *n;
685         ENTRY;
686
687         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
688                 list_del(&lprof->lp_list);
689                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
690                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
691                 if (lprof->lp_md)
692                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
693                 OBD_FREE(lprof, sizeof *lprof);
694         }
695         EXIT;
696 }
697
698 /* We can't call ll_process_config directly because it lives in a module that
699    must be loaded after this one. */
700 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
701
702 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
703 {
704         client_process_config = cpc;
705 }
706 EXPORT_SYMBOL(lustre_register_client_process_config);
707
708 int class_process_config(struct lustre_cfg *lcfg)
709 {
710         struct obd_device *obd;
711         int err;
712
713         LASSERT(lcfg && !IS_ERR(lcfg));
714         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
715
716         /* Commands that don't need a device */
717         switch(lcfg->lcfg_command) {
718         case LCFG_ATTACH: {
719                 err = class_attach(lcfg);
720                 GOTO(out, err);
721         }
722         case LCFG_ADD_UUID: {
723                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
724                        " (%s)\n", lustre_cfg_string(lcfg, 1),
725                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
726
727                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
728                 GOTO(out, err);
729         }
730         case LCFG_DEL_UUID: {
731                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
732                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
733                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
734
735                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
736                 GOTO(out, err);
737         }
738         case LCFG_MOUNTOPT: {
739                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
740                        lustre_cfg_string(lcfg, 1),
741                        lustre_cfg_string(lcfg, 2),
742                        lustre_cfg_string(lcfg, 3));
743                 /* set these mount options somewhere, so ll_fill_super
744                  * can find them. */
745                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
746                                         lustre_cfg_string(lcfg, 1),
747                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
748                                         lustre_cfg_string(lcfg, 2),
749                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
750                                         lustre_cfg_string(lcfg, 3));
751                 GOTO(out, err);
752         }
753         case LCFG_DEL_MOUNTOPT: {
754                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
755                        lustre_cfg_string(lcfg, 1));
756                 class_del_profile(lustre_cfg_string(lcfg, 1));
757                 GOTO(out, err = 0);
758         }
759         case LCFG_SET_TIMEOUT: {
760                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
761                        obd_timeout, lcfg->lcfg_num);
762                 obd_timeout = max(lcfg->lcfg_num, 1U);
763                 GOTO(out, err = 0);
764         }
765         case LCFG_SET_UPCALL: {
766                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
767                 /* COMPAT_146 Don't fail on old configs */
768                 GOTO(out, err = 0);
769         }
770         case LCFG_MARKER: {
771                 struct cfg_marker *marker;
772                 marker = lustre_cfg_buf(lcfg, 1);
773                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
774                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
775                 GOTO(out, err = 0);
776         }
777         case LCFG_PARAM: {
778                 /* llite has no obd */
779                 if ((class_match_param(lustre_cfg_string(lcfg, 1), 
780                                        PARAM_LLITE, 0) == 0) &&
781                     client_process_config) {
782                         err = (*client_process_config)(lcfg);
783                         GOTO(out, err);
784                 }
785                 /* Fall through */
786                 break;
787         }
788         }
789
790         /* Commands that require a device */
791         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
792         if (obd == NULL) {
793                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
794                         CERROR("this lcfg command requires a device name\n");
795                 else
796                         CERROR("no device for: %s\n",
797                                lustre_cfg_string(lcfg, 0));
798
799                 GOTO(out, err = -EINVAL);
800         }
801
802         switch(lcfg->lcfg_command) {
803         case LCFG_SETUP: {
804                 err = class_setup(obd, lcfg);
805                 GOTO(out, err);
806         }
807         case LCFG_DETACH: {
808                 err = class_detach(obd, lcfg);
809                 GOTO(out, err = 0);
810         }
811         case LCFG_CLEANUP: {
812                 err = class_cleanup(obd, lcfg);
813                 GOTO(out, err = 0);
814         }
815         case LCFG_ADD_CONN: {
816                 err = class_add_conn(obd, lcfg);
817                 GOTO(out, err = 0);
818         }
819         case LCFG_DEL_CONN: {
820                 err = class_del_conn(obd, lcfg);
821                 GOTO(out, err = 0);
822         }
823         default: {
824                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
825                 GOTO(out, err);
826
827         }
828         }
829 out:
830         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
831                 CWARN("Ignoring error %d on optional command %#x\n", err, 
832                       lcfg->lcfg_command);
833                 err = 0;
834         }
835         return err;
836 }
837
838 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars, 
839                              struct lustre_cfg *lcfg, void *data)
840 {
841 #ifdef __KERNEL__
842         struct lprocfs_vars *var;
843         char *key, *sval;
844         int i, keylen, vallen;
845         int matched = 0, j = 0;
846         int rc = 0;
847         ENTRY;
848
849         if (lcfg->lcfg_command != LCFG_PARAM) {
850                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
851                 RETURN(-EINVAL);
852         }
853
854         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
855            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
856            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
857         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
858                 key = lustre_cfg_buf(lcfg, i);
859                 /* Strip off prefix */
860                 class_match_param(key, prefix, &key);
861                 sval = strchr(key, '=');
862                 if (!sval || (*(sval + 1) == 0)) {
863                         CERROR("Can't parse param %s\n", key);
864                         /* rc = -EINVAL; continue parsing other params */
865                         continue;
866                 }
867                 keylen = sval - key;
868                 sval++;
869                 vallen = strlen(sval);
870                 matched = 0;
871                 j = 0;
872                 /* Search proc entries */
873                 while (lvars[j].name) {
874                         var = &lvars[j];
875                         if (class_match_param(key, (char *)var->name, 0) == 0 &&
876                             keylen == strlen(var->name)) {
877                                 matched++;
878                                 rc = -EROFS;
879                                 if (var->write_fptr) {
880                                         mm_segment_t oldfs;
881                                         oldfs = get_fs();
882                                         set_fs(KERNEL_DS);
883                                         rc = (var->write_fptr)(NULL, sval,
884                                                                vallen, data);
885                                         set_fs(oldfs);
886                                 }
887                                 if (rc < 0) 
888                                         CERROR("writing proc entry %s err %d\n", 
889                                                var->name, rc);
890                                 break;
891                         }
892                         j++;
893                 }    
894                 if (!matched) {
895                         CERROR("%s: unknown param %s\n",
896                                (char *)lustre_cfg_string(lcfg, 0), key);
897                         /* rc = -EINVAL;        continue parsing other params */
898                 } else {
899                         LCONSOLE_INFO("%s.%.*s: set parameter %.*s=%s\n", 
900                                       (char *)lustre_cfg_string(lcfg, 0),
901                                       (int)strlen(prefix) - 1, prefix,
902                                       (int)(sval - key - 1), key, sval);
903                 }
904         }
905         
906         if (rc > 0) 
907                 rc = 0;
908         RETURN(rc);
909 #else
910         CDEBUG(D_CONFIG, "liblustre can't process params.\n");
911         /* Don't throw config error */
912         RETURN(0);
913 #endif
914 }
915
916 int class_config_dump_handler(struct llog_handle * handle,
917                               struct llog_rec_hdr *rec, void *data);
918
919 #ifdef __KERNEL__
920 extern int lustre_check_exclusion(struct super_block *sb, char *svname);
921 #else
922 #define lustre_check_exclusion(a,b)  0
923 #endif
924
925 static int class_config_llog_handler(struct llog_handle * handle,
926                                      struct llog_rec_hdr *rec, void *data)
927 {
928         struct config_llog_instance *clli = data;
929         int cfg_len = rec->lrh_len;
930         char *cfg_buf = (char*) (rec + 1);
931         int rc = 0;
932         ENTRY;
933
934         //class_config_dump_handler(handle, rec, data);
935
936         switch (rec->lrh_type) {
937         case OBD_CFG_REC: {
938                 struct lustre_cfg *lcfg, *lcfg_new;
939                 struct lustre_cfg_bufs bufs;
940                 char *inst_name = NULL;
941                 int inst_len = 0;
942                 int inst = 0;
943
944                 lcfg = (struct lustre_cfg *)cfg_buf;
945                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION))
946                         lustre_swab_lustre_cfg(lcfg);
947
948                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
949                 if (rc)
950                         GOTO(out, rc);
951
952                 /* Figure out config state info */
953                 if (lcfg->lcfg_command == LCFG_MARKER) {
954                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
955                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
956                                clli->cfg_flags, marker->cm_flags);
957                         if (marker->cm_flags & CM_START) {
958                                 /* all previous flags off */
959                                 clli->cfg_flags = CFG_F_MARKER;
960                                 if (marker->cm_flags & CM_SKIP) { 
961                                         clli->cfg_flags |= CFG_F_SKIP;
962                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
963                                                marker->cm_step);
964                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
965                                            lustre_check_exclusion(clli->cfg_sb, 
966                                                           marker->cm_tgtname)) {
967                                         clli->cfg_flags |= CFG_F_EXCLUDE;
968                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
969                                                marker->cm_step);
970                                 }
971                         } else if (marker->cm_flags & CM_END) {
972                                 clli->cfg_flags = 0;
973                         }
974                 }
975                 /* A config command without a start marker before it is 
976                    illegal (post 146) */
977                 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
978                     !(clli->cfg_flags & CFG_F_MARKER) && 
979                     (lcfg->lcfg_command != LCFG_MARKER)) {
980                         CWARN("Config not inside markers, ignoring! "
981                               "(inst: %s, uuid: %s, flags: %#x)\n",
982                               clli->cfg_instance ? clli->cfg_instance : "<null>",
983                               clli->cfg_uuid.uuid, clli->cfg_flags);
984                         clli->cfg_flags |= CFG_F_SKIP;
985                 }
986                 if (clli->cfg_flags & CFG_F_SKIP) {
987                         CDEBUG(D_CONFIG, "skipping %#x\n",
988                                clli->cfg_flags);
989                         rc = 0;
990                         /* No processing! */
991                         break;
992                 }
993
994                 if ((clli->cfg_flags & CFG_F_EXCLUDE) && 
995                     (lcfg->lcfg_command == LCFG_LOV_ADD_OBD))
996                         /* Add inactive instead */
997                         lcfg->lcfg_command = LCFG_LOV_ADD_INA;
998
999                 lustre_cfg_bufs_init(&bufs, lcfg);
1000
1001                 if (clli && clli->cfg_instance && 
1002                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0){
1003                         inst = 1;
1004                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1005                                 strlen(clli->cfg_instance) + 1;
1006                         OBD_ALLOC(inst_name, inst_len);
1007                         if (inst_name == NULL)
1008                                 GOTO(out, rc = -ENOMEM);
1009                         sprintf(inst_name, "%s-%s",
1010                                 lustre_cfg_string(lcfg, 0),
1011                                 clli->cfg_instance);
1012                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1013                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1014                                lcfg->lcfg_command, inst_name);
1015                 }
1016
1017                 /* we override the llog's uuid for clients, to insure they
1018                 are unique */
1019                 if (clli && clli->cfg_instance && 
1020                     lcfg->lcfg_command == LCFG_ATTACH) {
1021                         lustre_cfg_bufs_set_string(&bufs, 2,
1022                                                    clli->cfg_uuid.uuid);
1023                 }
1024
1025                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1026
1027                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1028                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1029
1030                 /* XXX Hack to try to remain binary compatible with
1031                  * pre-newconfig logs */
1032                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1033                     (lcfg->lcfg_nid >> 32) == 0) {
1034                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1035
1036                         lcfg_new->lcfg_nid =
1037                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1038                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1039                               lcfg->lcfg_nal, addr,
1040                               libcfs_nid2str(lcfg_new->lcfg_nid));
1041                 } else {
1042                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1043                 }
1044
1045                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1046
1047                 rc = class_process_config(lcfg_new);
1048                 lustre_cfg_free(lcfg_new);
1049
1050                 if (inst)
1051                         OBD_FREE(inst_name, inst_len);
1052                 break;
1053         }
1054         default:
1055                 CERROR("Unknown llog record type %#x encountered\n",
1056                        rec->lrh_type);
1057                 break;
1058         }
1059 out:
1060         if (rc) {
1061                 CERROR("Err %d on cfg command:\n", rc);
1062                 class_config_dump_handler(handle, rec, data);
1063         }
1064         RETURN(rc);
1065 }
1066
1067 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
1068                             struct config_llog_instance *cfg)
1069 {
1070         struct llog_process_cat_data cd = {0, 0};
1071         struct llog_handle *llh;
1072         int rc, rc2;
1073         ENTRY;
1074
1075         CDEBUG(D_INFO, "looking up llog %s\n", name);
1076         rc = llog_create(ctxt, &llh, NULL, name);
1077         if (rc)
1078                 RETURN(rc);
1079
1080         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1081         if (rc)
1082                 GOTO(parse_out, rc);
1083
1084         /* continue processing from where we last stopped to end-of-log */
1085         if (cfg)
1086                 cd.first_idx = cfg->cfg_last_idx;
1087         cd.last_idx = 0;
1088
1089         rc = llog_process(llh, class_config_llog_handler, cfg, &cd);
1090
1091         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name, 
1092                cd.first_idx + 1, cd.last_idx, rc);
1093
1094         if (cfg)
1095                 cfg->cfg_last_idx = cd.last_idx;
1096
1097 parse_out:
1098         rc2 = llog_close(llh);
1099         if (rc == 0)
1100                 rc = rc2;
1101
1102         RETURN(rc);
1103 }
1104
1105 int class_config_dump_handler(struct llog_handle * handle,
1106                               struct llog_rec_hdr *rec, void *data)
1107 {
1108         int cfg_len = rec->lrh_len;
1109         char *cfg_buf = (char*) (rec + 1);
1110         char *outstr, *ptr, *end;
1111         int rc = 0;
1112         ENTRY;
1113
1114         OBD_ALLOC(outstr, 256);
1115         end = outstr + 256;
1116         ptr = outstr;
1117         if (!outstr) {
1118                 RETURN(-ENOMEM);
1119         }
1120         if (rec->lrh_type == OBD_CFG_REC) {
1121                 struct lustre_cfg *lcfg;
1122                 int i;
1123
1124                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1125                 if (rc)
1126                         GOTO(out, rc);
1127                 lcfg = (struct lustre_cfg *)cfg_buf;
1128
1129                 ptr += snprintf(ptr, end-ptr, "cmd=%05x ",
1130                                 lcfg->lcfg_command);
1131                 if (lcfg->lcfg_flags) {
1132                         ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1133                                         lcfg->lcfg_flags);
1134                 }
1135                 if (lcfg->lcfg_num) {
1136                         ptr += snprintf(ptr, end-ptr, "num=%#08x ",
1137                                         lcfg->lcfg_num);
1138                 }
1139                 if (lcfg->lcfg_nid) {
1140                         ptr += snprintf(ptr, end-ptr, "nid=%s("LPX64")\n     ",
1141                                         libcfs_nid2str(lcfg->lcfg_nid),
1142                                         lcfg->lcfg_nid);
1143                 }
1144                 if (lcfg->lcfg_command == LCFG_MARKER) {
1145                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1146                         ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1147                                         marker->cm_step, marker->cm_flags,
1148                                         marker->cm_tgtname, marker->cm_comment);
1149                 } else {
1150                         for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1151                                 ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1152                                                 lustre_cfg_string(lcfg, i));
1153                         }
1154                 }
1155                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1156         } else {
1157                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1158                 rc = -EINVAL;
1159         }
1160 out:
1161         OBD_FREE(outstr, 256);
1162         RETURN(rc);
1163 }
1164
1165 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
1166                            struct config_llog_instance *cfg)
1167 {
1168         struct llog_handle *llh;
1169         int rc, rc2;
1170         ENTRY;
1171
1172         LCONSOLE_INFO("Dumping config log %s\n", name);
1173
1174         rc = llog_create(ctxt, &llh, NULL, name);
1175         if (rc)
1176                 RETURN(rc);
1177
1178         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1179         if (rc)
1180                 GOTO(parse_out, rc);
1181
1182         rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
1183 parse_out:
1184         rc2 = llog_close(llh);
1185         if (rc == 0)
1186                 rc = rc2;
1187
1188         LCONSOLE_INFO("End config log %s\n", name);
1189         RETURN(rc);
1190
1191 }
1192
1193 /* Cleanup and detach */
1194 int class_manual_cleanup(struct obd_device *obd)
1195 {
1196         struct lustre_cfg *lcfg;
1197         struct lustre_cfg_bufs bufs;
1198         int rc;
1199         char flags[3]="";
1200         ENTRY;
1201
1202         if (!obd) {
1203                 CERROR("empty cleanup\n");
1204                 RETURN(-EALREADY);
1205         }
1206
1207         if (obd->obd_force)
1208                 strcat(flags, "F");
1209         if (obd->obd_fail)
1210                 strcat(flags, "A");
1211
1212         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1213                obd->obd_name, flags);
1214
1215         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1216         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1217         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1218
1219         rc = class_process_config(lcfg);
1220         if (rc) {
1221                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1222                 GOTO(out, rc);
1223         }
1224
1225         /* the lcfg is almost the same for both ops */
1226         lcfg->lcfg_command = LCFG_DETACH;
1227         rc = class_process_config(lcfg);
1228         if (rc)
1229                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1230 out:
1231         lustre_cfg_free(lcfg);
1232         RETURN(rc);
1233 }