Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / obdclass / obd_config.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/obd_config.c
37  *
38  * Config API
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42 #ifdef __KERNEL__
43 #include <obd_class.h>
44 #include <linux/string.h>
45 #else
46 #include <liblustre.h>
47 #include <obd_class.h>
48 #include <obd.h>
49 #endif
50 #include <lustre_log.h>
51 #include <lprocfs_status.h>
52 #include <libcfs/list.h>
53 #include <lustre_param.h>
54 #include <class_hash.h>
55
56 extern struct lustre_hash_operations uuid_hash_operations;
57 extern struct lustre_hash_operations nid_hash_operations;
58
59 /*********** string parsing utils *********/
60
61 /* returns 0 if we find this key in the buffer, else 1 */
62 int class_find_param(char *buf, char *key, char **valp)
63 {
64         char *ptr;
65
66         if (!buf) 
67                 return 1;
68
69         if ((ptr = strstr(buf, key)) == NULL) 
70                 return 1;
71
72         if (valp) 
73                 *valp = ptr + strlen(key);
74         
75         return 0;
76 }
77
78 /* returns 0 if this is the first key in the buffer, else 1.
79    valp points to first char after key. */
80 int class_match_param(char *buf, char *key, char **valp)
81 {
82         if (!buf) 
83                 return 1;
84
85         if (memcmp(buf, key, strlen(key)) != 0) 
86                 return 1;
87
88         if (valp) 
89                 *valp = buf + strlen(key);
90         
91         return 0;
92 }
93
94 /* 0 is good nid, 
95    1 not found
96    < 0 error
97    endh is set to next separator */
98 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
99 {
100         char tmp, *endp;
101
102         if (!buf) 
103                 return 1;
104         while (*buf == ',' || *buf == ':') 
105                 buf++;
106         if (*buf == ' ' || *buf == '/' || *buf == '\0') 
107                 return 1;
108
109         /* nid separators or end of nids */
110         endp = strpbrk(buf, ",: /");
111         if (endp == NULL) 
112                 endp = buf + strlen(buf);
113
114         tmp = *endp;
115         *endp = '\0';
116         *nid = libcfs_str2nid(buf);
117         if (*nid == LNET_NID_ANY) {
118                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
119                 *endp = tmp;
120                 return -EINVAL;
121         }
122         *endp = tmp;
123
124         if (endh) 
125                 *endh = endp;
126         CDEBUG(D_INFO, "Nid %s\n", libcfs_nid2str(*nid));
127         return 0;
128 }
129
130 EXPORT_SYMBOL(class_find_param);
131 EXPORT_SYMBOL(class_match_param);
132 EXPORT_SYMBOL(class_parse_nid);
133
134 /********************** class fns **********************/
135
136 /**
137  * Create a new device and set the type, name and uuid.  If successful, the new
138  * device can be accessed by either name or uuid.
139  */
140 int class_attach(struct lustre_cfg *lcfg)
141 {
142         struct obd_device *obd = NULL;
143         char *typename, *name, *uuid;
144         int rc, len;
145         ENTRY;
146
147         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
148                 CERROR("No type passed!\n");
149                 RETURN(-EINVAL);
150         }
151         typename = lustre_cfg_string(lcfg, 1);
152
153         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
154                 CERROR("No name passed!\n");
155                 RETURN(-EINVAL);
156         }
157         name = lustre_cfg_string(lcfg, 0);
158
159         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
160                 CERROR("No UUID passed!\n");
161                 RETURN(-EINVAL);
162         }
163         uuid = lustre_cfg_string(lcfg, 2);
164
165         CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
166                MKSTR(typename), MKSTR(name), MKSTR(uuid));
167
168         obd = class_newdev(typename, name);
169         if (IS_ERR(obd)) {
170                 /* Already exists or out of obds */
171                 rc = PTR_ERR(obd);
172                 obd = NULL;
173                 CERROR("Cannot create device %s of type %s : %d\n",
174                        name, typename, rc);
175                 GOTO(out, rc);
176         }
177         LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
178                  name, typename);
179         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, 
180                  "obd %p obd_magic %08X != %08X\n",
181                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
182         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0, "%p obd_name %s != %s\n",
183                  obd, obd->obd_name, name);
184
185         rwlock_init(&obd->obd_pool_lock);
186         obd->obd_pool_limit = 0;
187         obd->obd_pool_slv = 0;
188
189         CFS_INIT_LIST_HEAD(&obd->obd_exports);
190         CFS_INIT_LIST_HEAD(&obd->obd_exports_timed);
191         CFS_INIT_LIST_HEAD(&obd->obd_nid_stats);
192         spin_lock_init(&obd->obd_nid_lock);
193         spin_lock_init(&obd->obd_dev_lock);
194         sema_init(&obd->obd_dev_sem, 1);
195         spin_lock_init(&obd->obd_osfs_lock);
196         /* obd->obd_osfs_age must be set to a value in the distant
197          * past to guarantee a fresh statfs is fetched on mount. */
198         obd->obd_osfs_age = cfs_time_shift_64(-1000);
199
200         /* XXX belongs in setup not attach  */
201         /* recovery data */
202         cfs_init_timer(&obd->obd_recovery_timer);
203         spin_lock_init(&obd->obd_processing_task_lock);
204         cfs_waitq_init(&obd->obd_next_transno_waitq);
205         cfs_waitq_init(&obd->obd_evict_inprogress_waitq);
206         CFS_INIT_LIST_HEAD(&obd->obd_req_replay_queue);
207         CFS_INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
208         CFS_INIT_LIST_HEAD(&obd->obd_final_req_queue);
209
210         llog_group_init(&obd->obd_olg, OBD_LLOG_GROUP);
211
212         spin_lock_init(&obd->obd_uncommitted_replies_lock);
213         CFS_INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
214
215         len = strlen(uuid);
216         if (len >= sizeof(obd->obd_uuid)) {
217                 CERROR("uuid must be < %d bytes long\n",
218                        (int)sizeof(obd->obd_uuid));
219                 GOTO(out, rc = -EINVAL);
220         }
221         memcpy(obd->obd_uuid.uuid, uuid, len);
222
223         /* do the attach */
224         if (OBP(obd, attach)) {
225                 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
226                 if (rc)
227                         GOTO(out, rc = -EINVAL);
228         }
229
230         /* Detach drops this */
231         spin_lock(&obd->obd_dev_lock);
232         atomic_set(&obd->obd_refcount, 1);
233         spin_unlock(&obd->obd_dev_lock);
234
235         obd->obd_attached = 1;
236         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
237                obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
238         RETURN(0);
239  out:
240         if (obd != NULL) {
241                 class_release_dev(obd);
242         }
243         return rc;
244 }
245
246 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
247 {
248         int err = 0;
249         struct obd_export *exp;
250         ENTRY;
251
252         LASSERT(obd != NULL);
253         LASSERTF(obd == class_num2obd(obd->obd_minor), "obd %p != obd_devs[%d] %p\n", 
254                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
255         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n", 
256                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
257
258         /* have we attached a type to this device? */
259         if (!obd->obd_attached) {
260                 CERROR("Device %d not attached\n", obd->obd_minor);
261                 RETURN(-ENODEV);
262         }
263
264         if (obd->obd_set_up) {
265                 CERROR("Device %d already setup (type %s)\n",
266                        obd->obd_minor, obd->obd_type->typ_name);
267                 RETURN(-EEXIST);
268         }
269
270         /* is someone else setting us up right now? (attach inits spinlock) */
271         spin_lock(&obd->obd_dev_lock);
272         if (obd->obd_starting) {
273                 spin_unlock(&obd->obd_dev_lock);
274                 CERROR("Device %d setup in progress (type %s)\n",
275                        obd->obd_minor, obd->obd_type->typ_name);
276                 RETURN(-EEXIST);
277         }
278         /* just leave this on forever.  I can't use obd_set_up here because
279            other fns check that status, and we're not actually set up yet. */
280         obd->obd_starting = 1;
281         spin_unlock(&obd->obd_dev_lock);
282
283         /* create an uuid-export hash body */
284         err = lustre_hash_init(&obd->obd_uuid_hash_body, "UUID_HASH", 
285                                128, &uuid_hash_operations);
286         if (err)
287                 GOTO(err_hash, err);
288
289         /* create a nid-export hash body */
290         err = lustre_hash_init(&obd->obd_nid_hash_body, "NID_HASH", 
291                                128, &nid_hash_operations);
292         if (err)
293                 GOTO(err_hash, err);
294
295         /* create a nid-stats hash body */
296         err = lustre_hash_init(&obd->obd_nid_stats_hash_body, "NID_STATS",
297                                128, &nid_stat_hash_operations);
298         if (err)
299                 GOTO(err_hash, err);
300
301         exp = class_new_export(obd, &obd->obd_uuid);
302         if (IS_ERR(exp))
303                 RETURN(PTR_ERR(exp));
304
305         obd->obd_self_export = exp;
306         list_del_init(&exp->exp_obd_chain_timed);
307         class_export_put(exp);
308
309         err = obd_setup(obd, lcfg);
310         if (err)
311                 GOTO(err_exp, err);
312
313         obd->obd_set_up = 1;
314         
315         spin_lock(&obd->obd_dev_lock);
316         /* cleanup drops this */
317         class_incref(obd);
318         spin_unlock(&obd->obd_dev_lock);
319
320         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
321                obd->obd_name, obd->obd_uuid.uuid);
322
323         RETURN(0);
324
325 err_exp:
326         class_unlink_export(obd->obd_self_export);
327         obd->obd_self_export = NULL;
328 err_hash:
329         lustre_hash_exit(&obd->obd_uuid_hash_body);
330         lustre_hash_exit(&obd->obd_nid_hash_body);
331         lustre_hash_exit(&obd->obd_nid_stats_hash_body);
332         obd->obd_starting = 0;
333         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
334         RETURN(err);
335 }
336
337 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
338 {
339         ENTRY;
340
341         if (obd->obd_set_up) {
342                 CERROR("OBD device %d still set up\n", obd->obd_minor);
343                 RETURN(-EBUSY);
344         }
345
346         spin_lock(&obd->obd_dev_lock);
347         if (!obd->obd_attached) {
348                 spin_unlock(&obd->obd_dev_lock);
349                 CERROR("OBD device %d not attached\n", obd->obd_minor);
350                 RETURN(-ENODEV);
351         }
352         obd->obd_attached = 0;
353         spin_unlock(&obd->obd_dev_lock);
354
355         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
356                obd->obd_name, obd->obd_uuid.uuid);
357
358         class_decref(obd);
359
360         /* not strictly necessary, but cleans up eagerly */
361         obd_zombie_impexp_cull();
362
363         RETURN(0);
364 }
365
366 static void dump_exports(struct obd_device *obd)
367 {
368         struct obd_export *exp, *n;
369
370         list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
371                 struct ptlrpc_reply_state *rs;
372                 struct ptlrpc_reply_state *first_reply = NULL;
373                 int                        nreplies = 0;
374
375                 list_for_each_entry (rs, &exp->exp_outstanding_replies,
376                                      rs_exp_list) {
377                         if (nreplies == 0)
378                                 first_reply = rs;
379                         nreplies++;
380                 }
381
382                 CDEBUG(D_IOCTL, "%s: %p %s %s %d %d %d: %p %s\n",
383                        obd->obd_name, exp, exp->exp_client_uuid.uuid,
384                        obd_export_nid2str(exp),
385                        atomic_read(&exp->exp_refcount),
386                        exp->exp_failed, nreplies, first_reply,
387                        nreplies > 3 ? "..." : "");
388         }
389 }
390
391 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
392 {
393         int err = 0;
394         char *flag;
395         ENTRY;
396
397         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
398
399         if (!obd->obd_set_up) {
400                 CERROR("Device %d not setup\n", obd->obd_minor);
401                 RETURN(-ENODEV);
402         }
403
404         spin_lock(&obd->obd_dev_lock);
405         if (obd->obd_stopping) {
406                 spin_unlock(&obd->obd_dev_lock);
407                 CERROR("OBD %d already stopping\n", obd->obd_minor);
408                 RETURN(-ENODEV);
409         }
410         /* Leave this on forever */
411         obd->obd_stopping = 1;
412         spin_unlock(&obd->obd_dev_lock);
413
414         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
415                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
416                         switch (*flag) {
417                         case 'F':
418                                 obd->obd_force = 1;
419                                 break;
420                         case 'A':
421                                 LCONSOLE_WARN("Failing over %s\n",
422                                               obd->obd_name);
423                                 obd->obd_fail = 1;
424                                 obd->obd_no_transno = 1;
425                                 obd->obd_no_recov = 1;
426                                 /* Set the obd readonly if we can */
427                                 if (OBP(obd, iocontrol))
428                                         obd_iocontrol(OBD_IOC_SET_READONLY,
429                                                       obd->obd_self_export,
430                                                       0, NULL, NULL);
431                                 break;
432                         default:
433                                 CERROR("unrecognised flag '%c'\n",
434                                        *flag);
435                         }
436         }
437
438         /* The three references that should be remaining are the
439          * obd_self_export and the attach and setup references. */
440         if (atomic_read(&obd->obd_refcount) > 3) {
441 #if 0           /* We should never fail to cleanup with mountconf */ 
442                 if (!(obd->obd_fail || obd->obd_force)) {
443                         CERROR("OBD %s is still busy with %d references\n"
444                                "You should stop active file system users,"
445                                " or use the --force option to cleanup.\n",
446                                obd->obd_name, atomic_read(&obd->obd_refcount));
447                         dump_exports(obd);
448                         /* Allow a failed cleanup to try again. */
449                         obd->obd_stopping = 0;
450                 }
451 #endif
452                 /* refcounf - 3 might be the number of real exports 
453                    (excluding self export). But class_incref is called
454                    by other things as well, so don't count on it. */
455                 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
456                        obd->obd_name, atomic_read(&obd->obd_refcount) - 3);
457                 dump_exports(obd);
458                 class_disconnect_exports(obd);
459         }
460         LASSERT(obd->obd_self_export);
461
462         /* destroy an uuid-export hash body */
463         lustre_hash_exit(&obd->obd_uuid_hash_body);
464
465         /* destroy a nid-export hash body */
466         lustre_hash_exit(&obd->obd_nid_hash_body);
467
468         /* destroy a nid-stats hash body */
469         lustre_hash_exit(&obd->obd_nid_stats_hash_body);
470
471         /* Precleanup, we must make sure all exports get destroyed. */
472         err = obd_precleanup(obd, OBD_CLEANUP_EXPORTS);
473         if (err)
474                 CERROR("Precleanup %s returned %d\n",
475                        obd->obd_name, err);
476         class_decref(obd);
477         obd->obd_set_up = 0;
478         RETURN(0);
479 }
480
481 struct obd_device *class_incref(struct obd_device *obd)
482 {
483         atomic_inc(&obd->obd_refcount);
484         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
485                atomic_read(&obd->obd_refcount));
486
487         return obd;
488 }
489
490 void class_decref(struct obd_device *obd)
491 {
492         int err;
493         int refs;
494
495         spin_lock(&obd->obd_dev_lock);
496         atomic_dec(&obd->obd_refcount);
497         refs = atomic_read(&obd->obd_refcount);
498         spin_unlock(&obd->obd_dev_lock);
499
500         CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
501
502         if ((refs == 1) && obd->obd_stopping) {
503                 /* All exports have been destroyed; there should
504                    be no more in-progress ops by this point.*/
505
506                 spin_lock(&obd->obd_self_export->exp_lock);
507                 obd->obd_self_export->exp_flags |=
508                         (obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
509                         (obd->obd_force ? OBD_OPT_FORCE : 0);
510                 spin_unlock(&obd->obd_self_export->exp_lock);
511
512                 /* note that we'll recurse into class_decref again */
513                 class_unlink_export(obd->obd_self_export);
514                 return;
515         }
516
517         if (refs == 0) {
518                 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
519                        obd->obd_name, obd->obd_uuid.uuid);
520                 LASSERT(!obd->obd_attached);
521                 if (obd->obd_stopping) {
522                         /* If we're not stopping, we were never set up */
523                         err = obd_cleanup(obd);
524                         if (err)
525                                 CERROR("Cleanup %s returned %d\n",
526                                        obd->obd_name, err);
527                 }
528                 if (OBP(obd, detach)) {
529                         err = OBP(obd,detach)(obd);
530                         if (err)
531                                 CERROR("Detach returned %d\n", err);
532                 }
533                 class_release_dev(obd);
534         }
535 }
536
537 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
538 {
539         struct obd_import *imp;
540         struct obd_uuid uuid;
541         int rc;
542         ENTRY;
543
544         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
545             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
546                 CERROR("invalid conn_uuid\n");
547                 RETURN(-EINVAL);
548         }
549         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
550             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
551             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
552                 CERROR("can't add connection on non-client dev\n");
553                 RETURN(-EINVAL);
554         }
555
556         imp = obd->u.cli.cl_import;
557         if (!imp) {
558                 CERROR("try to add conn on immature client dev\n");
559                 RETURN(-EINVAL);
560         }
561
562         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
563         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
564
565         RETURN(rc);
566 }
567
568 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
569 {
570         struct obd_import *imp;
571         struct obd_uuid uuid;
572         int rc;
573         ENTRY;
574
575         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
576             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
577                 CERROR("invalid conn_uuid\n");
578                 RETURN(-EINVAL);
579         }
580         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
581             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
582                 CERROR("can't del connection on non-client dev\n");
583                 RETURN(-EINVAL);
584         }
585
586         imp = obd->u.cli.cl_import;
587         if (!imp) {
588                 CERROR("try to del conn on immature client dev\n");
589                 RETURN(-EINVAL);
590         }
591
592         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
593         rc = obd_del_conn(imp, &uuid);
594
595         RETURN(rc);
596 }
597
598 CFS_LIST_HEAD(lustre_profile_list);
599
600 struct lustre_profile *class_get_profile(const char * prof)
601 {
602         struct lustre_profile *lprof;
603
604         ENTRY;
605         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
606                 if (!strcmp(lprof->lp_profile, prof)) {
607                         RETURN(lprof);
608                 }
609         }
610         RETURN(NULL);
611 }
612
613 int class_add_profile(int proflen, char *prof, int osclen, char *osc,
614                       int mdclen, char *mdc)
615 {
616         struct lustre_profile *lprof;
617         int err = 0;
618         ENTRY;
619
620         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
621
622         OBD_ALLOC(lprof, sizeof(*lprof));
623         if (lprof == NULL)
624                 RETURN(-ENOMEM);
625         CFS_INIT_LIST_HEAD(&lprof->lp_list);
626
627         LASSERT(proflen == (strlen(prof) + 1));
628         OBD_ALLOC(lprof->lp_profile, proflen);
629         if (lprof->lp_profile == NULL)
630                 GOTO(out, err = -ENOMEM);
631         memcpy(lprof->lp_profile, prof, proflen);
632
633         LASSERT(osclen == (strlen(osc) + 1));
634         OBD_ALLOC(lprof->lp_dt, osclen);
635         if (lprof->lp_dt == NULL)
636                 GOTO(out, err = -ENOMEM);
637         memcpy(lprof->lp_dt, osc, osclen);
638
639         if (mdclen > 0) {
640                 LASSERT(mdclen == (strlen(mdc) + 1));
641                 OBD_ALLOC(lprof->lp_md, mdclen);
642                 if (lprof->lp_md == NULL)
643                         GOTO(out, err = -ENOMEM);
644                 memcpy(lprof->lp_md, mdc, mdclen);
645         }
646
647         list_add(&lprof->lp_list, &lustre_profile_list);
648         RETURN(err);
649
650 out:
651         if (lprof->lp_md)
652                 OBD_FREE(lprof->lp_md, mdclen);
653         if (lprof->lp_dt)
654                 OBD_FREE(lprof->lp_dt, osclen);
655         if (lprof->lp_profile)
656                 OBD_FREE(lprof->lp_profile, proflen);
657         OBD_FREE(lprof, sizeof(*lprof));        
658         RETURN(err);
659 }
660
661 void class_del_profile(const char *prof)
662 {
663         struct lustre_profile *lprof;
664         ENTRY;
665
666         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
667
668         lprof = class_get_profile(prof);
669         if (lprof) {
670                 list_del(&lprof->lp_list);
671                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
672                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
673                 if (lprof->lp_md)
674                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
675                 OBD_FREE(lprof, sizeof *lprof);
676         }
677         EXIT;
678 }
679
680 /* COMPAT_146 */
681 void class_del_profiles(void)
682 {
683         struct lustre_profile *lprof, *n;
684         ENTRY;
685
686         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
687                 list_del(&lprof->lp_list);
688                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
689                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
690                 if (lprof->lp_md)
691                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
692                 OBD_FREE(lprof, sizeof *lprof);
693         }
694         EXIT;
695 }
696
697 /* We can't call ll_process_config directly because it lives in a module that
698    must be loaded after this one. */
699 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
700
701 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
702 {
703         client_process_config = cpc;
704 }
705 EXPORT_SYMBOL(lustre_register_client_process_config);
706
707 int class_process_config(struct lustre_cfg *lcfg)
708 {
709         struct obd_device *obd;
710         int err;
711
712         LASSERT(lcfg && !IS_ERR(lcfg));
713         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
714
715         /* Commands that don't need a device */
716         switch(lcfg->lcfg_command) {
717         case LCFG_ATTACH: {
718                 err = class_attach(lcfg);
719                 GOTO(out, err);
720         }
721         case LCFG_ADD_UUID: {
722                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
723                        " (%s)\n", lustre_cfg_string(lcfg, 1),
724                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
725
726                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
727                 GOTO(out, err);
728         }
729         case LCFG_DEL_UUID: {
730                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
731                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
732                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
733
734                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
735                 GOTO(out, err);
736         }
737         case LCFG_MOUNTOPT: {
738                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
739                        lustre_cfg_string(lcfg, 1),
740                        lustre_cfg_string(lcfg, 2),
741                        lustre_cfg_string(lcfg, 3));
742                 /* set these mount options somewhere, so ll_fill_super
743                  * can find them. */
744                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
745                                         lustre_cfg_string(lcfg, 1),
746                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
747                                         lustre_cfg_string(lcfg, 2),
748                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
749                                         lustre_cfg_string(lcfg, 3));
750                 GOTO(out, err);
751         }
752         case LCFG_DEL_MOUNTOPT: {
753                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
754                        lustre_cfg_string(lcfg, 1));
755                 class_del_profile(lustre_cfg_string(lcfg, 1));
756                 GOTO(out, err = 0);
757         }
758         case LCFG_SET_TIMEOUT: {
759                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
760                        obd_timeout, lcfg->lcfg_num);
761                 obd_timeout = max(lcfg->lcfg_num, 1U);
762                 GOTO(out, err = 0);
763         }
764         case LCFG_SET_UPCALL: {
765                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
766                 /* COMPAT_146 Don't fail on old configs */
767                 GOTO(out, err = 0);
768         }
769         case LCFG_MARKER: {
770                 struct cfg_marker *marker;
771                 marker = lustre_cfg_buf(lcfg, 1);
772                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
773                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
774                 GOTO(out, err = 0);
775         }
776         case LCFG_PARAM: {
777                 /* llite has no obd */
778                 if ((class_match_param(lustre_cfg_string(lcfg, 1), 
779                                        PARAM_LLITE, 0) == 0) &&
780                     client_process_config) {
781                         err = (*client_process_config)(lcfg);
782                         GOTO(out, err);
783                 }
784                 /* Fall through */
785                 break;
786         }
787         }
788
789         /* Commands that require a device */
790         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
791         if (obd == NULL) {
792                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
793                         CERROR("this lcfg command requires a device name\n");
794                 else
795                         CERROR("no device for: %s\n",
796                                lustre_cfg_string(lcfg, 0));
797
798                 GOTO(out, err = -EINVAL);
799         }
800
801         switch(lcfg->lcfg_command) {
802         case LCFG_SETUP: {
803                 err = class_setup(obd, lcfg);
804                 GOTO(out, err);
805         }
806         case LCFG_DETACH: {
807                 err = class_detach(obd, lcfg);
808                 GOTO(out, err = 0);
809         }
810         case LCFG_CLEANUP: {
811                 err = class_cleanup(obd, lcfg);
812                 GOTO(out, err = 0);
813         }
814         case LCFG_ADD_CONN: {
815                 err = class_add_conn(obd, lcfg);
816                 GOTO(out, err = 0);
817         }
818         case LCFG_DEL_CONN: {
819                 err = class_del_conn(obd, lcfg);
820                 GOTO(out, err = 0);
821         }
822         default: {
823                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
824                 GOTO(out, err);
825
826         }
827         }
828 out:
829         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
830                 CWARN("Ignoring error %d on optional command %#x\n", err, 
831                       lcfg->lcfg_command);
832                 err = 0;
833         }
834         return err;
835 }
836
837 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars, 
838                              struct lustre_cfg *lcfg, void *data)
839 {
840 #ifdef __KERNEL__
841         struct lprocfs_vars *var;
842         char *key, *sval;
843         int i, keylen, vallen;
844         int matched = 0, j = 0;
845         int rc = 0;
846         ENTRY;
847
848         if (lcfg->lcfg_command != LCFG_PARAM) {
849                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
850                 RETURN(-EINVAL);
851         }
852
853         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
854            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
855            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
856         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
857                 key = lustre_cfg_buf(lcfg, i);
858                 /* Strip off prefix */
859                 class_match_param(key, prefix, &key);
860                 sval = strchr(key, '=');
861                 if (!sval || (*(sval + 1) == 0)) {
862                         CERROR("Can't parse param %s\n", key);
863                         /* rc = -EINVAL; continue parsing other params */
864                         continue;
865                 }
866                 keylen = sval - key;
867                 sval++;
868                 vallen = strlen(sval);
869                 matched = 0;
870                 j = 0;
871                 /* Search proc entries */
872                 while (lvars[j].name) {
873                         var = &lvars[j];
874                         if (class_match_param(key, (char *)var->name, 0) == 0 &&
875                             keylen == strlen(var->name)) {
876                                 matched++;
877                                 rc = -EROFS;
878                                 if (var->write_fptr) {
879                                         mm_segment_t oldfs;
880                                         oldfs = get_fs();
881                                         set_fs(KERNEL_DS);
882                                         rc = (var->write_fptr)(NULL, sval,
883                                                                vallen, data);
884                                         set_fs(oldfs);
885                                 }
886                                 if (rc < 0) 
887                                         CERROR("writing proc entry %s err %d\n", 
888                                                var->name, rc);
889                                 break;
890                         }
891                         j++;
892                 }    
893                 if (!matched) {
894                         CERROR("%s: unknown param %s\n",
895                                (char *)lustre_cfg_string(lcfg, 0), key);
896                         /* rc = -EINVAL;        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.lpcd_first_idx = cfg->cfg_last_idx;
1086         cd.lpcd_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.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1092
1093         if (cfg)
1094                 cfg->cfg_last_idx = cd.lpcd_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 }