Whamcloud - gitweb
LU-1432 ptlrpc: LBUG in lprocfs_free_client_stats()
[fs/lustre-release.git] / lustre / obdclass / obd_config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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
55 static cfs_hash_ops_t uuid_hash_ops;
56 static cfs_hash_ops_t nid_hash_ops;
57 static cfs_hash_ops_t nid_stat_hash_ops;
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 /**
79  * Finds a parameter in \a params and copies it to \a copy.
80  *
81  * Leading spaces are skipped. Next space or end of string is the
82  * parameter terminator with the exception that spaces inside single or double
83  * quotes get included into a parameter. The parameter is copied into \a copy
84  * which has to be allocated big enough by a caller, quotes are stripped in
85  * the copy and the copy is terminated by 0.
86  *
87  * On return \a params is set to next parameter or to NULL if last
88  * parameter is returned.
89  *
90  * \retval 0 if parameter is returned in \a copy
91  * \retval 1 otherwise
92  * \retval -EINVAL if unbalanced quota is found
93  */
94 int class_get_next_param(char **params, char *copy)
95 {
96         char *q1, *q2, *str;
97         int len;
98
99         str = *params;
100         while (*str == ' ')
101                 str++;
102
103         if (*str == '\0') {
104                 *params = NULL;
105                 return 1;
106         }
107
108         while (1) {
109                 q1 = strpbrk(str, " '\"");
110                 if (q1 == NULL) {
111                         len = strlen(str);
112                         memcpy(copy, str, len);
113                         copy[len] = '\0';
114                         *params = NULL;
115                         return 0;
116                 }
117                 len = q1 - str;
118                 if (*q1 == ' ') {
119                         memcpy(copy, str, len);
120                         copy[len] = '\0';
121                         *params = str + len;
122                         return 0;
123                 }
124
125                 memcpy(copy, str, len);
126                 copy += len;
127
128                 /* search for the matching closing quote */
129                 str = q1 + 1;
130                 q2 = strchr(str, *q1);
131                 if (q2 == NULL) {
132                         CERROR("Unbalanced quota in parameters: \"%s\"\n",
133                                *params);
134                         return -EINVAL;
135                 }
136                 len = q2 - str;
137                 memcpy(copy, str, len);
138                 copy += len;
139                 str = q2 + 1;
140         }
141         return 1;
142 }
143
144 /* returns 0 if this is the first key in the buffer, else 1.
145    valp points to first char after key. */
146 int class_match_param(char *buf, char *key, char **valp)
147 {
148         if (!buf)
149                 return 1;
150
151         if (memcmp(buf, key, strlen(key)) != 0)
152                 return 1;
153
154         if (valp)
155                 *valp = buf + strlen(key);
156
157         return 0;
158 }
159
160 static int parse_nid(char *buf, void *value)
161 {
162         lnet_nid_t *nid = (lnet_nid_t *)value;
163
164         *nid = libcfs_str2nid(buf);
165         if (*nid != LNET_NID_ANY)
166                 return 0;
167
168         LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
169         return -EINVAL;
170 }
171
172 static int parse_net(char *buf, void *value)
173 {
174         __u32 *net = (__u32 *)value;
175
176         *net = libcfs_str2net(buf);
177         CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
178         return 0;
179 }
180
181 enum {
182         CLASS_PARSE_NID = 1,
183         CLASS_PARSE_NET,
184 };
185
186 /* 0 is good nid,
187    1 not found
188    < 0 error
189    endh is set to next separator */
190 static int class_parse_value(char *buf, int opc, void *value, char **endh)
191 {
192         char *endp;
193         char  tmp;
194         int   rc = 0;
195
196         if (!buf)
197                 return 1;
198         while (*buf == ',' || *buf == ':')
199                 buf++;
200         if (*buf == ' ' || *buf == '/' || *buf == '\0')
201                 return 1;
202
203         /* nid separators or end of nids */
204         endp = strpbrk(buf, ",: /");
205         if (endp == NULL)
206                 endp = buf + strlen(buf);
207
208         tmp = *endp;
209         *endp = '\0';
210         switch (opc) {
211         default:
212                 LBUG();
213         case CLASS_PARSE_NID:
214                 rc = parse_nid(buf, value);
215                 break;
216         case CLASS_PARSE_NET:
217                 rc = parse_net(buf, value);
218                 break;
219         }
220         *endp = tmp;
221         if (rc != 0)
222                 return rc;
223         if (endh)
224                 *endh = endp;
225         return 0;
226 }
227
228 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
229 {
230         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh);
231 }
232
233 int class_parse_net(char *buf, __u32 *net, char **endh)
234 {
235         return class_parse_value(buf, CLASS_PARSE_NET, (void *)net, endh);
236 }
237
238 /* 1 param contains key and match
239  * 0 param contains key and not match
240  * -1 param does not contain key
241  */
242 int class_match_nid(char *buf, char *key, lnet_nid_t nid)
243 {
244         lnet_nid_t tmp;
245         int   rc = -1;
246
247         while (class_find_param(buf, key, &buf) == 0) {
248                 /* please restrict to the nids pertaining to
249                  * the specified nids */
250                 while (class_parse_nid(buf, &tmp, &buf) == 0) {
251                         if (tmp == nid)
252                                 return 1;
253                 }
254                 rc = 0;
255         }
256         return rc;
257 }
258
259 int class_match_net(char *buf, char *key, __u32 net)
260 {
261         __u32 tmp;
262         int   rc = -1;
263
264         while (class_find_param(buf, key, &buf) == 0) {
265                 /* please restrict to the nids pertaining to
266                  * the specified networks */
267                 while (class_parse_net(buf, &tmp, &buf) == 0) {
268                         if (tmp == net)
269                                 return 1;
270                 }
271                 rc = 0;
272         }
273         return rc;
274 }
275
276 EXPORT_SYMBOL(class_find_param);
277 EXPORT_SYMBOL(class_get_next_param);
278 EXPORT_SYMBOL(class_match_param);
279 EXPORT_SYMBOL(class_parse_nid);
280 EXPORT_SYMBOL(class_parse_net);
281 EXPORT_SYMBOL(class_match_nid);
282 EXPORT_SYMBOL(class_match_net);
283
284 /********************** class fns **********************/
285
286 /**
287  * Create a new obd device and set the type, name and uuid.  If successful,
288  * the new device can be accessed by either name or uuid.
289  */
290 int class_attach(struct lustre_cfg *lcfg)
291 {
292         struct obd_device *obd = NULL;
293         char *typename, *name, *uuid;
294         int rc, len;
295         ENTRY;
296
297         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
298                 CERROR("No type passed!\n");
299                 RETURN(-EINVAL);
300         }
301         typename = lustre_cfg_string(lcfg, 1);
302
303         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
304                 CERROR("No name passed!\n");
305                 RETURN(-EINVAL);
306         }
307         name = lustre_cfg_string(lcfg, 0);
308
309         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
310                 CERROR("No UUID passed!\n");
311                 RETURN(-EINVAL);
312         }
313         uuid = lustre_cfg_string(lcfg, 2);
314
315         CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
316                MKSTR(typename), MKSTR(name), MKSTR(uuid));
317
318         obd = class_newdev(typename, name);
319         if (IS_ERR(obd)) {
320                 /* Already exists or out of obds */
321                 rc = PTR_ERR(obd);
322                 obd = NULL;
323                 CERROR("Cannot create device %s of type %s : %d\n",
324                        name, typename, rc);
325                 GOTO(out, rc);
326         }
327         LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
328                  name, typename);
329         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
330                  "obd %p obd_magic %08X != %08X\n",
331                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
332         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
333                  "%p obd_name %s != %s\n", obd, obd->obd_name, name);
334
335         cfs_rwlock_init(&obd->obd_pool_lock);
336         obd->obd_pool_limit = 0;
337         obd->obd_pool_slv = 0;
338
339         CFS_INIT_LIST_HEAD(&obd->obd_exports);
340         CFS_INIT_LIST_HEAD(&obd->obd_unlinked_exports);
341         CFS_INIT_LIST_HEAD(&obd->obd_delayed_exports);
342         CFS_INIT_LIST_HEAD(&obd->obd_exports_timed);
343         CFS_INIT_LIST_HEAD(&obd->obd_nid_stats);
344         cfs_spin_lock_init(&obd->obd_nid_lock);
345         cfs_spin_lock_init(&obd->obd_dev_lock);
346         cfs_mutex_init(&obd->obd_dev_mutex);
347         cfs_spin_lock_init(&obd->obd_osfs_lock);
348         /* obd->obd_osfs_age must be set to a value in the distant
349          * past to guarantee a fresh statfs is fetched on mount. */
350         obd->obd_osfs_age = cfs_time_shift_64(-1000);
351
352         /* XXX belongs in setup not attach  */
353         cfs_init_rwsem(&obd->obd_observer_link_sem);
354         /* recovery data */
355         cfs_init_timer(&obd->obd_recovery_timer);
356         cfs_spin_lock_init(&obd->obd_recovery_task_lock);
357         cfs_waitq_init(&obd->obd_next_transno_waitq);
358         cfs_waitq_init(&obd->obd_evict_inprogress_waitq);
359         CFS_INIT_LIST_HEAD(&obd->obd_req_replay_queue);
360         CFS_INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
361         CFS_INIT_LIST_HEAD(&obd->obd_final_req_queue);
362         CFS_INIT_LIST_HEAD(&obd->obd_evict_list);
363
364         llog_group_init(&obd->obd_olg, FID_SEQ_LLOG);
365
366         obd->obd_conn_inprogress = 0;
367
368         len = strlen(uuid);
369         if (len >= sizeof(obd->obd_uuid)) {
370                 CERROR("uuid must be < %d bytes long\n",
371                        (int)sizeof(obd->obd_uuid));
372                 GOTO(out, rc = -EINVAL);
373         }
374         memcpy(obd->obd_uuid.uuid, uuid, len);
375
376         /* do the attach */
377         if (OBP(obd, attach)) {
378                 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
379                 if (rc)
380                         GOTO(out, rc = -EINVAL);
381         }
382
383         /* Detach drops this */
384         cfs_spin_lock(&obd->obd_dev_lock);
385         cfs_atomic_set(&obd->obd_refcount, 1);
386         cfs_spin_unlock(&obd->obd_dev_lock);
387         lu_ref_init(&obd->obd_reference);
388         lu_ref_add(&obd->obd_reference, "attach", obd);
389
390         obd->obd_attached = 1;
391         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
392                obd->obd_minor, typename, cfs_atomic_read(&obd->obd_refcount));
393         RETURN(0);
394  out:
395         if (obd != NULL) {
396                 class_release_dev(obd);
397         }
398         return rc;
399 }
400
401 /** Create hashes, self-export, and call type-specific setup.
402  * Setup is effectively the "start this obd" call.
403  */
404 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
405 {
406         int err = 0;
407         struct obd_export *exp;
408         ENTRY;
409
410         LASSERT(obd != NULL);
411         LASSERTF(obd == class_num2obd(obd->obd_minor),
412                  "obd %p != obd_devs[%d] %p\n",
413                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
414         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
415                  "obd %p obd_magic %08x != %08x\n",
416                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
417
418         /* have we attached a type to this device? */
419         if (!obd->obd_attached) {
420                 CERROR("Device %d not attached\n", obd->obd_minor);
421                 RETURN(-ENODEV);
422         }
423
424         if (obd->obd_set_up) {
425                 CERROR("Device %d already setup (type %s)\n",
426                        obd->obd_minor, obd->obd_type->typ_name);
427                 RETURN(-EEXIST);
428         }
429
430         /* is someone else setting us up right now? (attach inits spinlock) */
431         cfs_spin_lock(&obd->obd_dev_lock);
432         if (obd->obd_starting) {
433                 cfs_spin_unlock(&obd->obd_dev_lock);
434                 CERROR("Device %d setup in progress (type %s)\n",
435                        obd->obd_minor, obd->obd_type->typ_name);
436                 RETURN(-EEXIST);
437         }
438         /* just leave this on forever.  I can't use obd_set_up here because
439            other fns check that status, and we're not actually set up yet. */
440         obd->obd_starting = 1;
441         obd->obd_uuid_hash = NULL;
442         obd->obd_nid_hash = NULL;
443         obd->obd_nid_stats_hash = NULL;
444         cfs_spin_unlock(&obd->obd_dev_lock);
445
446         /* create an uuid-export lustre hash */
447         obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
448                                              HASH_UUID_CUR_BITS,
449                                              HASH_UUID_MAX_BITS,
450                                              HASH_UUID_BKT_BITS, 0,
451                                              CFS_HASH_MIN_THETA,
452                                              CFS_HASH_MAX_THETA,
453                                              &uuid_hash_ops, CFS_HASH_DEFAULT);
454         if (!obd->obd_uuid_hash)
455                 GOTO(err_hash, err = -ENOMEM);
456
457         /* create a nid-export lustre hash */
458         obd->obd_nid_hash = cfs_hash_create("NID_HASH",
459                                             HASH_NID_CUR_BITS,
460                                             HASH_NID_MAX_BITS,
461                                             HASH_NID_BKT_BITS, 0,
462                                             CFS_HASH_MIN_THETA,
463                                             CFS_HASH_MAX_THETA,
464                                             &nid_hash_ops, CFS_HASH_DEFAULT);
465         if (!obd->obd_nid_hash)
466                 GOTO(err_hash, err = -ENOMEM);
467
468         /* create a nid-stats lustre hash */
469         obd->obd_nid_stats_hash = cfs_hash_create("NID_STATS",
470                                                   HASH_NID_STATS_CUR_BITS,
471                                                   HASH_NID_STATS_MAX_BITS,
472                                                   HASH_NID_STATS_BKT_BITS, 0,
473                                                   CFS_HASH_MIN_THETA,
474                                                   CFS_HASH_MAX_THETA,
475                                                   &nid_stat_hash_ops, CFS_HASH_DEFAULT);
476         if (!obd->obd_nid_stats_hash)
477                 GOTO(err_hash, err = -ENOMEM);
478
479         exp = class_new_export(obd, &obd->obd_uuid);
480         if (IS_ERR(exp))
481                 GOTO(err_hash, err = PTR_ERR(exp));
482
483         obd->obd_self_export = exp;
484         cfs_list_del_init(&exp->exp_obd_chain_timed);
485         class_export_put(exp);
486
487         err = obd_setup(obd, lcfg);
488         if (err)
489                 GOTO(err_exp, err);
490
491         obd->obd_set_up = 1;
492
493         cfs_spin_lock(&obd->obd_dev_lock);
494         /* cleanup drops this */
495         class_incref(obd, "setup", obd);
496         cfs_spin_unlock(&obd->obd_dev_lock);
497
498         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
499                obd->obd_name, obd->obd_uuid.uuid);
500
501         RETURN(0);
502 err_exp:
503         if (obd->obd_self_export) {
504                 class_unlink_export(obd->obd_self_export);
505                 obd->obd_self_export = NULL;
506         }
507 err_hash:
508         if (obd->obd_uuid_hash) {
509                 cfs_hash_putref(obd->obd_uuid_hash);
510                 obd->obd_uuid_hash = NULL;
511         }
512         if (obd->obd_nid_hash) {
513                 cfs_hash_putref(obd->obd_nid_hash);
514                 obd->obd_nid_hash = NULL;
515         }
516         if (obd->obd_nid_stats_hash) {
517                 cfs_hash_putref(obd->obd_nid_stats_hash);
518                 obd->obd_nid_stats_hash = NULL;
519         }
520         obd->obd_starting = 0;
521         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
522         return err;
523 }
524
525 /** We have finished using this obd and are ready to destroy it.
526  * There can be no more references to this obd.
527  */
528 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
529 {
530         ENTRY;
531
532         if (obd->obd_set_up) {
533                 CERROR("OBD device %d still set up\n", obd->obd_minor);
534                 RETURN(-EBUSY);
535         }
536
537         cfs_spin_lock(&obd->obd_dev_lock);
538         if (!obd->obd_attached) {
539                 cfs_spin_unlock(&obd->obd_dev_lock);
540                 CERROR("OBD device %d not attached\n", obd->obd_minor);
541                 RETURN(-ENODEV);
542         }
543         obd->obd_attached = 0;
544         cfs_spin_unlock(&obd->obd_dev_lock);
545
546         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
547                obd->obd_name, obd->obd_uuid.uuid);
548
549         class_decref(obd, "attach", obd);
550         RETURN(0);
551 }
552
553 /** Start shutting down the obd.  There may be in-progess ops when
554  * this is called.  We tell them to start shutting down with a call
555  * to class_disconnect_exports().
556  */
557 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
558 {
559         int err = 0;
560         char *flag;
561         ENTRY;
562
563         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
564
565         if (!obd->obd_set_up) {
566                 CERROR("Device %d not setup\n", obd->obd_minor);
567                 RETURN(-ENODEV);
568         }
569
570         cfs_spin_lock(&obd->obd_dev_lock);
571         if (obd->obd_stopping) {
572                 cfs_spin_unlock(&obd->obd_dev_lock);
573                 CERROR("OBD %d already stopping\n", obd->obd_minor);
574                 RETURN(-ENODEV);
575         }
576         /* Leave this on forever */
577         obd->obd_stopping = 1;
578
579         /* wait for already-arrived-connections to finish. */
580         while (obd->obd_conn_inprogress > 0) {
581                 cfs_spin_unlock(&obd->obd_dev_lock);
582
583                 cfs_cond_resched();
584
585                 cfs_spin_lock(&obd->obd_dev_lock);
586         }
587        cfs_spin_unlock(&obd->obd_dev_lock);
588
589         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
590                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
591                         switch (*flag) {
592                         case 'F':
593                                 obd->obd_force = 1;
594                                 break;
595                         case 'A':
596                                 LCONSOLE_WARN("Failing over %s\n",
597                                               obd->obd_name);
598                                 obd->obd_fail = 1;
599                                 obd->obd_no_transno = 1;
600                                 obd->obd_no_recov = 1;
601                                 if (OBP(obd, iocontrol)) {
602                                         obd_iocontrol(OBD_IOC_SYNC,
603                                                       obd->obd_self_export,
604                                                       0, NULL, NULL);
605                                 }
606                                 break;
607                         default:
608                                 CERROR("Unrecognised flag '%c'\n", *flag);
609                         }
610         }
611
612         LASSERT(obd->obd_self_export);
613
614         /* The three references that should be remaining are the
615          * obd_self_export and the attach and setup references. */
616         if (cfs_atomic_read(&obd->obd_refcount) > 3) {
617                 /* refcounf - 3 might be the number of real exports
618                    (excluding self export). But class_incref is called
619                    by other things as well, so don't count on it. */
620                 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
621                        obd->obd_name, cfs_atomic_read(&obd->obd_refcount) - 3);
622                 dump_exports(obd, 0);
623                 class_disconnect_exports(obd);
624         }
625
626         /* Precleanup, we must make sure all exports get destroyed. */
627         err = obd_precleanup(obd, OBD_CLEANUP_EXPORTS);
628         if (err)
629                 CERROR("Precleanup %s returned %d\n",
630                        obd->obd_name, err);
631
632         /* destroy an uuid-export hash body */
633         if (obd->obd_uuid_hash) {
634                 cfs_hash_putref(obd->obd_uuid_hash);
635                 obd->obd_uuid_hash = NULL;
636         }
637
638         /* destroy a nid-export hash body */
639         if (obd->obd_nid_hash) {
640                 cfs_hash_putref(obd->obd_nid_hash);
641                 obd->obd_nid_hash = NULL;
642         }
643
644         /* destroy a nid-stats hash body */
645         if (obd->obd_nid_stats_hash) {
646                 cfs_hash_putref(obd->obd_nid_stats_hash);
647                 obd->obd_nid_stats_hash = NULL;
648         }
649
650         class_decref(obd, "setup", obd);
651         obd->obd_set_up = 0;
652
653         RETURN(0);
654 }
655
656 struct obd_device *class_incref(struct obd_device *obd,
657                                 const char *scope, const void *source)
658 {
659         lu_ref_add_atomic(&obd->obd_reference, scope, source);
660         cfs_atomic_inc(&obd->obd_refcount);
661         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
662                cfs_atomic_read(&obd->obd_refcount));
663
664         return obd;
665 }
666
667 void class_decref(struct obd_device *obd, const char *scope, const void *source)
668 {
669         int err;
670         int refs;
671
672         cfs_spin_lock(&obd->obd_dev_lock);
673         cfs_atomic_dec(&obd->obd_refcount);
674         refs = cfs_atomic_read(&obd->obd_refcount);
675         cfs_spin_unlock(&obd->obd_dev_lock);
676         lu_ref_del(&obd->obd_reference, scope, source);
677
678         CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
679
680         if ((refs == 1) && obd->obd_stopping) {
681                 /* All exports have been destroyed; there should
682                    be no more in-progress ops by this point.*/
683
684                 cfs_spin_lock(&obd->obd_self_export->exp_lock);
685                 obd->obd_self_export->exp_flags |= exp_flags_from_obd(obd);
686                 cfs_spin_unlock(&obd->obd_self_export->exp_lock);
687
688                 /* note that we'll recurse into class_decref again */
689                 class_unlink_export(obd->obd_self_export);
690                 return;
691         }
692
693         if (refs == 0) {
694                 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
695                        obd->obd_name, obd->obd_uuid.uuid);
696                 LASSERT(!obd->obd_attached);
697                 if (obd->obd_stopping) {
698                         /* If we're not stopping, we were never set up */
699                         err = obd_cleanup(obd);
700                         if (err)
701                                 CERROR("Cleanup %s returned %d\n",
702                                        obd->obd_name, err);
703                 }
704                 if (OBP(obd, detach)) {
705                         err = OBP(obd, detach)(obd);
706                         if (err)
707                                 CERROR("Detach returned %d\n", err);
708                 }
709                 class_release_dev(obd);
710         }
711 }
712
713 /** Add a failover nid location.
714  * Client obd types contact server obd types using this nid list.
715  */
716 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
717 {
718         struct obd_import *imp;
719         struct obd_uuid uuid;
720         int rc;
721         ENTRY;
722
723         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
724             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
725                 CERROR("invalid conn_uuid\n");
726                 RETURN(-EINVAL);
727         }
728         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
729             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
730             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
731                 CERROR("can't add connection on non-client dev\n");
732                 RETURN(-EINVAL);
733         }
734
735         imp = obd->u.cli.cl_import;
736         if (!imp) {
737                 CERROR("try to add conn on immature client dev\n");
738                 RETURN(-EINVAL);
739         }
740
741         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
742         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
743
744         RETURN(rc);
745 }
746
747 /** Remove a failover nid location.
748  */
749 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
750 {
751         struct obd_import *imp;
752         struct obd_uuid uuid;
753         int rc;
754         ENTRY;
755
756         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
757             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
758                 CERROR("invalid conn_uuid\n");
759                 RETURN(-EINVAL);
760         }
761         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
762             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
763                 CERROR("can't del connection on non-client dev\n");
764                 RETURN(-EINVAL);
765         }
766
767         imp = obd->u.cli.cl_import;
768         if (!imp) {
769                 CERROR("try to del conn on immature client dev\n");
770                 RETURN(-EINVAL);
771         }
772
773         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
774         rc = obd_del_conn(imp, &uuid);
775
776         RETURN(rc);
777 }
778
779 CFS_LIST_HEAD(lustre_profile_list);
780
781 struct lustre_profile *class_get_profile(const char * prof)
782 {
783         struct lustre_profile *lprof;
784
785         ENTRY;
786         cfs_list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
787                 if (!strcmp(lprof->lp_profile, prof)) {
788                         RETURN(lprof);
789                 }
790         }
791         RETURN(NULL);
792 }
793
794 /** Create a named "profile".
795  * This defines the mdc and osc names to use for a client.
796  * This also is used to define the lov to be used by a mdt.
797  */
798 int class_add_profile(int proflen, char *prof, int osclen, char *osc,
799                       int mdclen, char *mdc)
800 {
801         struct lustre_profile *lprof;
802         int err = 0;
803         ENTRY;
804
805         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
806
807         OBD_ALLOC(lprof, sizeof(*lprof));
808         if (lprof == NULL)
809                 RETURN(-ENOMEM);
810         CFS_INIT_LIST_HEAD(&lprof->lp_list);
811
812         LASSERT(proflen == (strlen(prof) + 1));
813         OBD_ALLOC(lprof->lp_profile, proflen);
814         if (lprof->lp_profile == NULL)
815                 GOTO(out, err = -ENOMEM);
816         memcpy(lprof->lp_profile, prof, proflen);
817
818         LASSERT(osclen == (strlen(osc) + 1));
819         OBD_ALLOC(lprof->lp_dt, osclen);
820         if (lprof->lp_dt == NULL)
821                 GOTO(out, err = -ENOMEM);
822         memcpy(lprof->lp_dt, osc, osclen);
823
824         if (mdclen > 0) {
825                 LASSERT(mdclen == (strlen(mdc) + 1));
826                 OBD_ALLOC(lprof->lp_md, mdclen);
827                 if (lprof->lp_md == NULL)
828                         GOTO(out, err = -ENOMEM);
829                 memcpy(lprof->lp_md, mdc, mdclen);
830         }
831
832         cfs_list_add(&lprof->lp_list, &lustre_profile_list);
833         RETURN(err);
834
835 out:
836         if (lprof->lp_md)
837                 OBD_FREE(lprof->lp_md, mdclen);
838         if (lprof->lp_dt)
839                 OBD_FREE(lprof->lp_dt, osclen);
840         if (lprof->lp_profile)
841                 OBD_FREE(lprof->lp_profile, proflen);
842         OBD_FREE(lprof, sizeof(*lprof));
843         RETURN(err);
844 }
845
846 void class_del_profile(const char *prof)
847 {
848         struct lustre_profile *lprof;
849         ENTRY;
850
851         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
852
853         lprof = class_get_profile(prof);
854         if (lprof) {
855                 cfs_list_del(&lprof->lp_list);
856                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
857                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
858                 if (lprof->lp_md)
859                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
860                 OBD_FREE(lprof, sizeof *lprof);
861         }
862         EXIT;
863 }
864
865 /* COMPAT_146 */
866 void class_del_profiles(void)
867 {
868         struct lustre_profile *lprof, *n;
869         ENTRY;
870
871         cfs_list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
872                 cfs_list_del(&lprof->lp_list);
873                 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
874                 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
875                 if (lprof->lp_md)
876                         OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
877                 OBD_FREE(lprof, sizeof *lprof);
878         }
879         EXIT;
880 }
881
882 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
883 {
884         ENTRY;
885         if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
886                 at_min = val;
887         else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
888                 at_max = val;
889         else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
890                 at_extra = val;
891         else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
892                 at_early_margin = val;
893         else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
894                 at_history = val;
895         else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0) {
896                 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
897                 memcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
898                        JOBSTATS_JOBID_VAR_MAX_LEN + 1);
899         } else
900                 RETURN(-EINVAL);
901
902         CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
903         RETURN(0);
904 }
905
906
907 /* We can't call ll_process_config directly because it lives in a module that
908    must be loaded after this one. */
909 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
910
911 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
912 {
913         client_process_config = cpc;
914 }
915 EXPORT_SYMBOL(lustre_register_client_process_config);
916
917 /**
918  * Rename the proc parameter in \a cfg with a new name \a new_name.
919  *
920  * \param cfg      config structure which contains the proc parameter
921  * \param new_name new name of the proc parameter
922  *
923  * \retval valid-pointer    pointer to the newly-allocated config structure
924  *                          which contains the renamed proc parameter
925  * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
926  *                          not contain a proc parameter
927  * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
928  */
929 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
930                                      const char *new_name)
931 {
932         struct lustre_cfg_bufs  *bufs = NULL;
933         struct lustre_cfg       *new_cfg = NULL;
934         char                    *param = NULL;
935         char                    *new_param = NULL;
936         char                    *value = NULL;
937         int                      name_len = 0;
938         int                      new_len = 0;
939         ENTRY;
940
941         if (cfg == NULL || new_name == NULL)
942                 RETURN(ERR_PTR(-EINVAL));
943
944         param = lustre_cfg_string(cfg, 1);
945         if (param == NULL)
946                 RETURN(ERR_PTR(-EINVAL));
947
948         value = strchr(param, '=');
949         if (value == NULL)
950                 name_len = strlen(param);
951         else
952                 name_len = value - param;
953
954         new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
955
956         OBD_ALLOC(new_param, new_len);
957         if (new_param == NULL)
958                 RETURN(ERR_PTR(-ENOMEM));
959
960         strcpy(new_param, new_name);
961         if (value != NULL)
962                 strcat(new_param, value);
963
964         OBD_ALLOC_PTR(bufs);
965         if (bufs == NULL) {
966                 OBD_FREE(new_param, new_len);
967                 RETURN(ERR_PTR(-ENOMEM));
968         }
969
970         lustre_cfg_bufs_reset(bufs, NULL);
971         lustre_cfg_bufs_init(bufs, cfg);
972         lustre_cfg_bufs_set_string(bufs, 1, new_param);
973
974         new_cfg = lustre_cfg_new(cfg->lcfg_command, bufs);
975
976         OBD_FREE(new_param, new_len);
977         OBD_FREE_PTR(bufs);
978         if (new_cfg == NULL)
979                 RETURN(ERR_PTR(-ENOMEM));
980
981         new_cfg->lcfg_num = cfg->lcfg_num;
982         new_cfg->lcfg_flags = cfg->lcfg_flags;
983         new_cfg->lcfg_nid = cfg->lcfg_nid;
984         new_cfg->lcfg_nal = cfg->lcfg_nal;
985
986         RETURN(new_cfg);
987 }
988 EXPORT_SYMBOL(lustre_cfg_rename);
989
990 /** Process configuration commands given in lustre_cfg form.
991  * These may come from direct calls (e.g. class_manual_cleanup)
992  * or processing the config llog, or ioctl from lctl.
993  */
994 int class_process_config(struct lustre_cfg *lcfg)
995 {
996         struct obd_device *obd;
997         int err;
998
999         LASSERT(lcfg && !IS_ERR(lcfg));
1000         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1001
1002         /* Commands that don't need a device */
1003         switch(lcfg->lcfg_command) {
1004         case LCFG_ATTACH: {
1005                 err = class_attach(lcfg);
1006                 GOTO(out, err);
1007         }
1008         case LCFG_ADD_UUID: {
1009                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
1010                        " (%s)\n", lustre_cfg_string(lcfg, 1),
1011                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1012
1013                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1014                 GOTO(out, err);
1015         }
1016         case LCFG_DEL_UUID: {
1017                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1018                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1019                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1020
1021                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1022                 GOTO(out, err);
1023         }
1024         case LCFG_MOUNTOPT: {
1025                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1026                        lustre_cfg_string(lcfg, 1),
1027                        lustre_cfg_string(lcfg, 2),
1028                        lustre_cfg_string(lcfg, 3));
1029                 /* set these mount options somewhere, so ll_fill_super
1030                  * can find them. */
1031                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1032                                         lustre_cfg_string(lcfg, 1),
1033                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1034                                         lustre_cfg_string(lcfg, 2),
1035                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1036                                         lustre_cfg_string(lcfg, 3));
1037                 GOTO(out, err);
1038         }
1039         case LCFG_DEL_MOUNTOPT: {
1040                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1041                        lustre_cfg_string(lcfg, 1));
1042                 class_del_profile(lustre_cfg_string(lcfg, 1));
1043                 GOTO(out, err = 0);
1044         }
1045         case LCFG_SET_TIMEOUT: {
1046                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1047                        obd_timeout, lcfg->lcfg_num);
1048                 obd_timeout = max(lcfg->lcfg_num, 1U);
1049                 GOTO(out, err = 0);
1050         }
1051         case LCFG_SET_LDLM_TIMEOUT: {
1052                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1053                        ldlm_timeout, lcfg->lcfg_num);
1054                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1055                 if (ldlm_timeout >= obd_timeout)
1056                         ldlm_timeout = max(obd_timeout / 3, 1U);
1057
1058                 GOTO(out, err = 0);
1059         }
1060         case LCFG_SET_UPCALL: {
1061                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1062                 /* COMPAT_146 Don't fail on old configs */
1063                 GOTO(out, err = 0);
1064         }
1065         case LCFG_MARKER: {
1066                 struct cfg_marker *marker;
1067                 marker = lustre_cfg_buf(lcfg, 1);
1068                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1069                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1070                 GOTO(out, err = 0);
1071         }
1072         case LCFG_PARAM: {
1073                 char *tmp;
1074                 /* llite has no obd */
1075                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1076                                        PARAM_LLITE, 0) == 0) &&
1077                     client_process_config) {
1078                         err = (*client_process_config)(lcfg);
1079                         GOTO(out, err);
1080                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1081                                               PARAM_SYS, &tmp) == 0)) {
1082                         /* Global param settings */
1083                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1084                         /* Note that since LCFG_PARAM is LCFG_REQUIRED, new
1085                            unknown globals would cause config to fail */
1086                         if (err)
1087                                 CWARN("Ignoring unknown param %s\n", tmp);
1088                         GOTO(out, 0);
1089                 }
1090
1091                 /* Fall through */
1092                 break;
1093         }
1094         }
1095
1096         /* Commands that require a device */
1097         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1098         if (obd == NULL) {
1099                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1100                         CERROR("this lcfg command requires a device name\n");
1101                 else
1102                         CERROR("no device for: %s\n",
1103                                lustre_cfg_string(lcfg, 0));
1104
1105                 GOTO(out, err = -EINVAL);
1106         }
1107
1108         switch(lcfg->lcfg_command) {
1109         case LCFG_SETUP: {
1110                 err = class_setup(obd, lcfg);
1111                 GOTO(out, err);
1112         }
1113         case LCFG_DETACH: {
1114                 err = class_detach(obd, lcfg);
1115                 GOTO(out, err = 0);
1116         }
1117         case LCFG_CLEANUP: {
1118                 err = class_cleanup(obd, lcfg);
1119                 GOTO(out, err = 0);
1120         }
1121         case LCFG_ADD_CONN: {
1122                 err = class_add_conn(obd, lcfg);
1123                 GOTO(out, err = 0);
1124         }
1125         case LCFG_DEL_CONN: {
1126                 err = class_del_conn(obd, lcfg);
1127                 GOTO(out, err = 0);
1128         }
1129         case LCFG_POOL_NEW: {
1130                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1131                 GOTO(out, err = 0);
1132                 break;
1133         }
1134         case LCFG_POOL_ADD: {
1135                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1136                                    lustre_cfg_string(lcfg, 3));
1137                 GOTO(out, err = 0);
1138                 break;
1139         }
1140         case LCFG_POOL_REM: {
1141                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1142                                    lustre_cfg_string(lcfg, 3));
1143                 GOTO(out, err = 0);
1144                 break;
1145         }
1146         case LCFG_POOL_DEL: {
1147                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1148                 GOTO(out, err = 0);
1149                 break;
1150         }
1151         default: {
1152                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1153                 GOTO(out, err);
1154
1155         }
1156         }
1157 out:
1158         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1159                 CWARN("Ignoring error %d on optional command %#x\n", err,
1160                       lcfg->lcfg_command);
1161                 err = 0;
1162         }
1163         return err;
1164 }
1165
1166 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1167                              struct lustre_cfg *lcfg, void *data)
1168 {
1169 #ifdef __KERNEL__
1170         struct lprocfs_vars *var;
1171         char *key, *sval;
1172         int i, keylen, vallen;
1173         int matched = 0, j = 0;
1174         int rc = 0;
1175         int skip = 0;
1176         ENTRY;
1177
1178         if (lcfg->lcfg_command != LCFG_PARAM) {
1179                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1180                 RETURN(-EINVAL);
1181         }
1182
1183         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1184            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1185            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1186         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1187                 key = lustre_cfg_buf(lcfg, i);
1188                 /* Strip off prefix */
1189                 class_match_param(key, prefix, &key);
1190                 sval = strchr(key, '=');
1191                 if (!sval || (*(sval + 1) == 0)) {
1192                         CERROR("Can't parse param %s (missing '=')\n", key);
1193                         /* rc = -EINVAL;        continue parsing other params */
1194                         continue;
1195                 }
1196                 keylen = sval - key;
1197                 sval++;
1198                 vallen = strlen(sval);
1199                 matched = 0;
1200                 j = 0;
1201                 /* Search proc entries */
1202                 while (lvars[j].name) {
1203                         var = &lvars[j];
1204                         if (class_match_param(key, (char *)var->name, 0) == 0 &&
1205                             keylen == strlen(var->name)) {
1206                                 matched++;
1207                                 rc = -EROFS;
1208                                 if (var->write_fptr) {
1209                                         mm_segment_t oldfs;
1210                                         oldfs = get_fs();
1211                                         set_fs(KERNEL_DS);
1212                                         rc = (var->write_fptr)(NULL, sval,
1213                                                                vallen, data);
1214                                         set_fs(oldfs);
1215                                 }
1216                                 break;
1217                         }
1218                         j++;
1219                 }
1220                 if (!matched) {
1221                         /* If the prefix doesn't match, return error so we
1222                            can pass it down the stack */
1223                         if (strnchr(key, keylen, '.'))
1224                             RETURN(-ENOSYS);
1225                         CERROR("%s: unknown param %s\n",
1226                                (char *)lustre_cfg_string(lcfg, 0), key);
1227                         /* rc = -EINVAL;        continue parsing other params */
1228                         skip++;
1229                 } else if (rc < 0) {
1230                         CERROR("writing proc entry %s err %d\n",
1231                                var->name, rc);
1232                         rc = 0;
1233                 } else {
1234                         CDEBUG(D_CONFIG, "%s.%.*s: set parameter %.*s=%s\n",
1235                                       lustre_cfg_string(lcfg, 0),
1236                                       (int)strlen(prefix) - 1, prefix,
1237                                       (int)(sval - key - 1), key, sval);
1238                 }
1239         }
1240
1241         if (rc > 0)
1242                 rc = 0;
1243         if (!rc && skip)
1244                 rc = skip;
1245         RETURN(rc);
1246 #else
1247         CDEBUG(D_CONFIG, "liblustre can't process params.\n");
1248         /* Don't throw config error */
1249         RETURN(0);
1250 #endif
1251 }
1252
1253 int class_config_dump_handler(struct llog_handle * handle,
1254                               struct llog_rec_hdr *rec, void *data);
1255
1256 #ifdef __KERNEL__
1257 extern int lustre_check_exclusion(struct super_block *sb, char *svname);
1258 #else
1259 #define lustre_check_exclusion(a,b)  0
1260 #endif
1261
1262 /** Parse a configuration llog, doing various manipulations on them
1263  * for various reasons, (modifications for compatibility, skip obsolete
1264  * records, change uuids, etc), then class_process_config() resulting
1265  * net records.
1266  */
1267 static int class_config_llog_handler(struct llog_handle * handle,
1268                                      struct llog_rec_hdr *rec, void *data)
1269 {
1270         struct config_llog_instance *clli = data;
1271         int cfg_len = rec->lrh_len;
1272         char *cfg_buf = (char*) (rec + 1);
1273         int rc = 0;
1274         ENTRY;
1275
1276         //class_config_dump_handler(handle, rec, data);
1277
1278         switch (rec->lrh_type) {
1279         case OBD_CFG_REC: {
1280                 struct lustre_cfg *lcfg, *lcfg_new;
1281                 struct lustre_cfg_bufs bufs;
1282                 char *inst_name = NULL;
1283                 int inst_len = 0;
1284                 int inst = 0, swab = 0;
1285
1286                 lcfg = (struct lustre_cfg *)cfg_buf;
1287                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1288                         lustre_swab_lustre_cfg(lcfg);
1289                         swab = 1;
1290                 }
1291
1292                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1293                 if (rc)
1294                         GOTO(out, rc);
1295
1296                 /* Figure out config state info */
1297                 if (lcfg->lcfg_command == LCFG_MARKER) {
1298                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1299                         lustre_swab_cfg_marker(marker, swab,
1300                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1301                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1302                                clli->cfg_flags, marker->cm_flags);
1303                         if (marker->cm_flags & CM_START) {
1304                                 /* all previous flags off */
1305                                 clli->cfg_flags = CFG_F_MARKER;
1306                                 if (marker->cm_flags & CM_SKIP) {
1307                                         clli->cfg_flags |= CFG_F_SKIP;
1308                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1309                                                marker->cm_step);
1310                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1311                                            (clli->cfg_sb &&
1312                                             lustre_check_exclusion(clli->cfg_sb,
1313                                                          marker->cm_tgtname))) {
1314                                         clli->cfg_flags |= CFG_F_EXCLUDE;
1315                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1316                                                marker->cm_step);
1317                                 }
1318                         } else if (marker->cm_flags & CM_END) {
1319                                 clli->cfg_flags = 0;
1320                         }
1321                 }
1322                 /* A config command without a start marker before it is
1323                    illegal (post 146) */
1324                 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
1325                     !(clli->cfg_flags & CFG_F_MARKER) &&
1326                     (lcfg->lcfg_command != LCFG_MARKER)) {
1327                         CWARN("Config not inside markers, ignoring! "
1328                               "(inst: %p, uuid: %s, flags: %#x)\n",
1329                               clli->cfg_instance,
1330                               clli->cfg_uuid.uuid, clli->cfg_flags);
1331                         clli->cfg_flags |= CFG_F_SKIP;
1332                 }
1333                 if (clli->cfg_flags & CFG_F_SKIP) {
1334                         CDEBUG(D_CONFIG, "skipping %#x\n",
1335                                clli->cfg_flags);
1336                         rc = 0;
1337                         /* No processing! */
1338                         break;
1339                 }
1340
1341                 /*
1342                  * For interoperability between 1.8 and 2.0,
1343                  * rename "mds" obd device type to "mdt".
1344                  */
1345                 {
1346                         char *typename = lustre_cfg_string(lcfg, 1);
1347                         char *index = lustre_cfg_string(lcfg, 2);
1348
1349                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1350                              strcmp(typename, "mds") == 0)) {
1351                                 CWARN("For 1.8 interoperability, rename obd "
1352                                        "type from mds to mdt\n");
1353                                 typename[2] = 't';
1354                         }
1355                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1356                              strcmp(index, "type") == 0)) {
1357                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1358                                        "set this index to '0'\n");
1359                                 index[0] = '0';
1360                                 index[1] = 0;
1361                         }
1362                 }
1363
1364                 if ((clli->cfg_flags & CFG_F_EXCLUDE) &&
1365                     (lcfg->lcfg_command == LCFG_LOV_ADD_OBD))
1366                         /* Add inactive instead */
1367                         lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1368
1369                 lustre_cfg_bufs_init(&bufs, lcfg);
1370
1371                 if (clli && clli->cfg_instance &&
1372                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0){
1373                         inst = 1;
1374                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1375                                    sizeof(clli->cfg_instance) * 2 + 4;
1376                         OBD_ALLOC(inst_name, inst_len);
1377                         if (inst_name == NULL)
1378                                 GOTO(out, rc = -ENOMEM);
1379                         sprintf(inst_name, "%s-%p",
1380                                 lustre_cfg_string(lcfg, 0),
1381                                 clli->cfg_instance);
1382                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1383                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1384                                lcfg->lcfg_command, inst_name);
1385                 }
1386
1387                 /* we override the llog's uuid for clients, to insure they
1388                 are unique */
1389                 if (clli && clli->cfg_instance != NULL &&
1390                     lcfg->lcfg_command == LCFG_ATTACH) {
1391                         lustre_cfg_bufs_set_string(&bufs, 2,
1392                                                    clli->cfg_uuid.uuid);
1393                 }
1394                 /*
1395                  * sptlrpc config record, we expect 2 data segments:
1396                  *  [0]: fs_name/target_name,
1397                  *  [1]: rule string
1398                  * moving them to index [1] and [2], and insert MGC's
1399                  * obdname at index [0].
1400                  */
1401                 if (clli && clli->cfg_instance == NULL &&
1402                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1403                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1404                                             bufs.lcfg_buflen[1]);
1405                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1406                                             bufs.lcfg_buflen[0]);
1407                         lustre_cfg_bufs_set_string(&bufs, 0,
1408                                                    clli->cfg_obdname);
1409                 }
1410
1411                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1412
1413                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1414                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1415
1416                 /* XXX Hack to try to remain binary compatible with
1417                  * pre-newconfig logs */
1418                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1419                     (lcfg->lcfg_nid >> 32) == 0) {
1420                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1421
1422                         lcfg_new->lcfg_nid =
1423                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1424                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1425                               lcfg->lcfg_nal, addr,
1426                               libcfs_nid2str(lcfg_new->lcfg_nid));
1427                 } else {
1428                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1429                 }
1430
1431                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1432
1433                 rc = class_process_config(lcfg_new);
1434                 lustre_cfg_free(lcfg_new);
1435
1436                 if (inst)
1437                         OBD_FREE(inst_name, inst_len);
1438                 break;
1439         }
1440         default:
1441                 CERROR("Unknown llog record type %#x encountered\n",
1442                        rec->lrh_type);
1443                 break;
1444         }
1445 out:
1446         if (rc) {
1447                 CERROR("Err %d on cfg command:\n", rc);
1448                 class_config_dump_handler(handle, rec, data);
1449         }
1450         RETURN(rc);
1451 }
1452
1453 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
1454                             struct config_llog_instance *cfg)
1455 {
1456         struct llog_process_cat_data cd = {0, 0};
1457         struct llog_handle *llh;
1458         int rc, rc2;
1459         ENTRY;
1460
1461         CDEBUG(D_INFO, "looking up llog %s\n", name);
1462         rc = llog_create(ctxt, &llh, NULL, name);
1463         if (rc)
1464                 RETURN(rc);
1465
1466         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1467         if (rc)
1468                 GOTO(parse_out, rc);
1469
1470         /* continue processing from where we last stopped to end-of-log */
1471         if (cfg)
1472                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1473         cd.lpcd_last_idx = 0;
1474
1475         rc = llog_process(llh, class_config_llog_handler, cfg, &cd);
1476
1477         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1478                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1479
1480         if (cfg)
1481                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1482
1483 parse_out:
1484         rc2 = llog_close(llh);
1485         if (rc == 0)
1486                 rc = rc2;
1487
1488         RETURN(rc);
1489 }
1490
1491 int class_config_dump_handler(struct llog_handle * handle,
1492                               struct llog_rec_hdr *rec, void *data)
1493 {
1494         int cfg_len = rec->lrh_len;
1495         char *cfg_buf = (char*) (rec + 1);
1496         char *outstr, *ptr, *end;
1497         int rc = 0;
1498         ENTRY;
1499
1500         OBD_ALLOC(outstr, 256);
1501         end = outstr + 256;
1502         ptr = outstr;
1503         if (!outstr) {
1504                 RETURN(-ENOMEM);
1505         }
1506         if (rec->lrh_type == OBD_CFG_REC) {
1507                 struct lustre_cfg *lcfg;
1508                 int i;
1509
1510                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1511                 if (rc)
1512                         GOTO(out, rc);
1513                 lcfg = (struct lustre_cfg *)cfg_buf;
1514
1515                 ptr += snprintf(ptr, end-ptr, "cmd=%05x ",
1516                                 lcfg->lcfg_command);
1517                 if (lcfg->lcfg_flags) {
1518                         ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1519                                         lcfg->lcfg_flags);
1520                 }
1521                 if (lcfg->lcfg_num) {
1522                         ptr += snprintf(ptr, end-ptr, "num=%#08x ",
1523                                         lcfg->lcfg_num);
1524                 }
1525                 if (lcfg->lcfg_nid) {
1526                         ptr += snprintf(ptr, end-ptr, "nid=%s("LPX64")\n     ",
1527                                         libcfs_nid2str(lcfg->lcfg_nid),
1528                                         lcfg->lcfg_nid);
1529                 }
1530                 if (lcfg->lcfg_command == LCFG_MARKER) {
1531                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1532                         ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1533                                         marker->cm_step, marker->cm_flags,
1534                                         marker->cm_tgtname, marker->cm_comment);
1535                 } else {
1536                         for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1537                                 ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1538                                                 lustre_cfg_string(lcfg, i));
1539                         }
1540                 }
1541                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1542         } else {
1543                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1544                 rc = -EINVAL;
1545         }
1546 out:
1547         OBD_FREE(outstr, 256);
1548         RETURN(rc);
1549 }
1550
1551 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
1552                            struct config_llog_instance *cfg)
1553 {
1554         struct llog_handle *llh;
1555         int rc, rc2;
1556         ENTRY;
1557
1558         LCONSOLE_INFO("Dumping config log %s\n", name);
1559
1560         rc = llog_create(ctxt, &llh, NULL, name);
1561         if (rc)
1562                 RETURN(rc);
1563
1564         rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1565         if (rc)
1566                 GOTO(parse_out, rc);
1567
1568         rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
1569 parse_out:
1570         rc2 = llog_close(llh);
1571         if (rc == 0)
1572                 rc = rc2;
1573
1574         LCONSOLE_INFO("End config log %s\n", name);
1575         RETURN(rc);
1576
1577 }
1578
1579 /** Call class_cleanup and class_detach.
1580  * "Manual" only in the sense that we're faking lcfg commands.
1581  */
1582 int class_manual_cleanup(struct obd_device *obd)
1583 {
1584         char                    flags[3] = "";
1585         struct lustre_cfg      *lcfg;
1586         struct lustre_cfg_bufs  bufs;
1587         int                     rc;
1588         ENTRY;
1589
1590         if (!obd) {
1591                 CERROR("empty cleanup\n");
1592                 RETURN(-EALREADY);
1593         }
1594
1595         if (obd->obd_force)
1596                 strcat(flags, "F");
1597         if (obd->obd_fail)
1598                 strcat(flags, "A");
1599
1600         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1601                obd->obd_name, flags);
1602
1603         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1604         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1605         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1606         if (!lcfg)
1607                 RETURN(-ENOMEM);
1608
1609         rc = class_process_config(lcfg);
1610         if (rc) {
1611                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1612                 GOTO(out, rc);
1613         }
1614
1615         /* the lcfg is almost the same for both ops */
1616         lcfg->lcfg_command = LCFG_DETACH;
1617         rc = class_process_config(lcfg);
1618         if (rc)
1619                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1620 out:
1621         lustre_cfg_free(lcfg);
1622         RETURN(rc);
1623 }
1624
1625 /*
1626  * uuid<->export lustre hash operations
1627  */
1628
1629 static unsigned
1630 uuid_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1631 {
1632         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
1633                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
1634 }
1635
1636 static void *
1637 uuid_key(cfs_hlist_node_t *hnode)
1638 {
1639         struct obd_export *exp;
1640
1641         exp = cfs_hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1642
1643         return &exp->exp_client_uuid;
1644 }
1645
1646 /*
1647  * NOTE: It is impossible to find an export that is in failed
1648  *       state with this function
1649  */
1650 static int
1651 uuid_keycmp(const void *key, cfs_hlist_node_t *hnode)
1652 {
1653         struct obd_export *exp;
1654
1655         LASSERT(key);
1656         exp = cfs_hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1657
1658         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
1659                !exp->exp_failed;
1660 }
1661
1662 static void *
1663 uuid_export_object(cfs_hlist_node_t *hnode)
1664 {
1665         return cfs_hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1666 }
1667
1668 static void
1669 uuid_export_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1670 {
1671         struct obd_export *exp;
1672
1673         exp = cfs_hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1674         class_export_get(exp);
1675 }
1676
1677 static void
1678 uuid_export_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1679 {
1680         struct obd_export *exp;
1681
1682         exp = cfs_hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1683         class_export_put(exp);
1684 }
1685
1686 static cfs_hash_ops_t uuid_hash_ops = {
1687         .hs_hash        = uuid_hash,
1688         .hs_key         = uuid_key,
1689         .hs_keycmp      = uuid_keycmp,
1690         .hs_object      = uuid_export_object,
1691         .hs_get         = uuid_export_get,
1692         .hs_put_locked  = uuid_export_put_locked,
1693 };
1694
1695
1696 /*
1697  * nid<->export hash operations
1698  */
1699
1700 static unsigned
1701 nid_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1702 {
1703         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1704 }
1705
1706 static void *
1707 nid_key(cfs_hlist_node_t *hnode)
1708 {
1709         struct obd_export *exp;
1710
1711         exp = cfs_hlist_entry(hnode, struct obd_export, exp_nid_hash);
1712
1713         RETURN(&exp->exp_connection->c_peer.nid);
1714 }
1715
1716 /*
1717  * NOTE: It is impossible to find an export that is in failed
1718  *       state with this function
1719  */
1720 static int
1721 nid_kepcmp(const void *key, cfs_hlist_node_t *hnode)
1722 {
1723         struct obd_export *exp;
1724
1725         LASSERT(key);
1726         exp = cfs_hlist_entry(hnode, struct obd_export, exp_nid_hash);
1727
1728         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
1729                !exp->exp_failed);
1730 }
1731
1732 static void *
1733 nid_export_object(cfs_hlist_node_t *hnode)
1734 {
1735         return cfs_hlist_entry(hnode, struct obd_export, exp_nid_hash);
1736 }
1737
1738 static void
1739 nid_export_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1740 {
1741         struct obd_export *exp;
1742
1743         exp = cfs_hlist_entry(hnode, struct obd_export, exp_nid_hash);
1744         class_export_get(exp);
1745 }
1746
1747 static void
1748 nid_export_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1749 {
1750         struct obd_export *exp;
1751
1752         exp = cfs_hlist_entry(hnode, struct obd_export, exp_nid_hash);
1753         class_export_put(exp);
1754 }
1755
1756 static cfs_hash_ops_t nid_hash_ops = {
1757         .hs_hash        = nid_hash,
1758         .hs_key         = nid_key,
1759         .hs_keycmp      = nid_kepcmp,
1760         .hs_object      = nid_export_object,
1761         .hs_get         = nid_export_get,
1762         .hs_put_locked  = nid_export_put_locked,
1763 };
1764
1765
1766 /*
1767  * nid<->nidstats hash operations
1768  */
1769
1770 static void *
1771 nidstats_key(cfs_hlist_node_t *hnode)
1772 {
1773         struct nid_stat *ns;
1774
1775         ns = cfs_hlist_entry(hnode, struct nid_stat, nid_hash);
1776
1777         return &ns->nid;
1778 }
1779
1780 static int
1781 nidstats_keycmp(const void *key, cfs_hlist_node_t *hnode)
1782 {
1783         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
1784 }
1785
1786 static void *
1787 nidstats_object(cfs_hlist_node_t *hnode)
1788 {
1789         return cfs_hlist_entry(hnode, struct nid_stat, nid_hash);
1790 }
1791
1792 static void
1793 nidstats_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1794 {
1795         struct nid_stat *ns;
1796
1797         ns = cfs_hlist_entry(hnode, struct nid_stat, nid_hash);
1798         nidstat_getref(ns);
1799 }
1800
1801 static void
1802 nidstats_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1803 {
1804         struct nid_stat *ns;
1805
1806         ns = cfs_hlist_entry(hnode, struct nid_stat, nid_hash);
1807         nidstat_putref(ns);
1808 }
1809
1810 static cfs_hash_ops_t nid_stat_hash_ops = {
1811         .hs_hash        = nid_hash,
1812         .hs_key         = nidstats_key,
1813         .hs_keycmp      = nidstats_keycmp,
1814         .hs_object      = nidstats_object,
1815         .hs_get         = nidstats_get,
1816         .hs_put_locked  = nidstats_put_locked,
1817 };