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