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