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