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