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