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