Whamcloud - gitweb
ad2f518902147c1ab3e7092904a866958f8cb214
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_config.c
33  *
34  * Config API
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <linux/string.h>
40
41 #include <llog_swab.h>
42 #include <lprocfs_status.h>
43 #include <lustre_disk.h>
44 #include <lustre_ioctl.h>
45 #include <lustre_log.h>
46 #include <lustre_param.h>
47 #include <obd_class.h>
48
49 #include "llog_internal.h"
50
51 static struct cfs_hash_ops uuid_hash_ops;
52 static struct cfs_hash_ops nid_hash_ops;
53 static struct cfs_hash_ops nid_stat_hash_ops;
54 static struct cfs_hash_ops gen_hash_ops;
55
56 /*********** string parsing utils *********/
57
58 /* returns 0 if we find this key in the buffer, else 1 */
59 int class_find_param(char *buf, char *key, char **valp)
60 {
61         char *ptr;
62
63         if (!buf)
64                 return 1;
65
66         if ((ptr = strstr(buf, key)) == NULL)
67                 return 1;
68
69         if (valp)
70                 *valp = ptr + strlen(key);
71
72         return 0;
73 }
74 EXPORT_SYMBOL(class_find_param);
75
76 /**
77  * Check whether the proc parameter \a param is an old parameter or not from
78  * the array \a ptr which contains the mapping from old parameters to new ones.
79  * If it's an old one, then return the pointer to the cfg_interop_param struc-
80  * ture which contains both the old and new parameters.
81  *
82  * \param param                 proc parameter
83  * \param ptr                   an array which contains the mapping from
84  *                              old parameters to new ones
85  *
86  * \retval valid-pointer        pointer to the cfg_interop_param structure
87  *                              which contains the old and new parameters
88  * \retval NULL                 \a param or \a ptr is NULL,
89  *                              or \a param is not an old parameter
90  */
91 struct cfg_interop_param *class_find_old_param(const char *param,
92                                                struct cfg_interop_param *ptr)
93 {
94         char *value = NULL;
95         int   name_len = 0;
96
97         if (param == NULL || ptr == NULL)
98                 RETURN(NULL);
99
100         value = strchr(param, '=');
101         if (value == NULL)
102                 name_len = strlen(param);
103         else
104                 name_len = value - param;
105
106         while (ptr->old_param != NULL) {
107                 if (strncmp(param, ptr->old_param, name_len) == 0 &&
108                     name_len == strlen(ptr->old_param))
109                         RETURN(ptr);
110                 ptr++;
111         }
112
113         RETURN(NULL);
114 }
115 EXPORT_SYMBOL(class_find_old_param);
116
117 /**
118  * Finds a parameter in \a params and copies it to \a copy.
119  *
120  * Leading spaces are skipped. Next space or end of string is the
121  * parameter terminator with the exception that spaces inside single or double
122  * quotes get included into a parameter. The parameter is copied into \a copy
123  * which has to be allocated big enough by a caller, quotes are stripped in
124  * the copy and the copy is terminated by 0.
125  *
126  * On return \a params is set to next parameter or to NULL if last
127  * parameter is returned.
128  *
129  * \retval 0 if parameter is returned in \a copy
130  * \retval 1 otherwise
131  * \retval -EINVAL if unbalanced quota is found
132  */
133 int class_get_next_param(char **params, char *copy)
134 {
135         char *q1, *q2, *str;
136         int len;
137
138         str = *params;
139         while (*str == ' ')
140                 str++;
141
142         if (*str == '\0') {
143                 *params = NULL;
144                 return 1;
145         }
146
147         while (1) {
148                 q1 = strpbrk(str, " '\"");
149                 if (q1 == NULL) {
150                         len = strlen(str);
151                         memcpy(copy, str, len);
152                         copy[len] = '\0';
153                         *params = NULL;
154                         return 0;
155                 }
156                 len = q1 - str;
157                 if (*q1 == ' ') {
158                         memcpy(copy, str, len);
159                         copy[len] = '\0';
160                         *params = str + len;
161                         return 0;
162                 }
163
164                 memcpy(copy, str, len);
165                 copy += len;
166
167                 /* search for the matching closing quote */
168                 str = q1 + 1;
169                 q2 = strchr(str, *q1);
170                 if (q2 == NULL) {
171                         CERROR("Unbalanced quota in parameters: \"%s\"\n",
172                                *params);
173                         return -EINVAL;
174                 }
175                 len = q2 - str;
176                 memcpy(copy, str, len);
177                 copy += len;
178                 str = q2 + 1;
179         }
180         return 1;
181 }
182 EXPORT_SYMBOL(class_get_next_param);
183
184 /* returns 0 if this is the first key in the buffer, else 1.
185    valp points to first char after key. */
186 int class_match_param(char *buf, const char *key, char **valp)
187 {
188         if (!buf)
189                 return 1;
190
191         if (memcmp(buf, key, strlen(key)) != 0)
192                 return 1;
193
194         if (valp)
195                 *valp = buf + strlen(key);
196
197         return 0;
198 }
199 EXPORT_SYMBOL(class_match_param);
200
201 static int parse_nid(char *buf, void *value, int quiet)
202 {
203         lnet_nid_t *nid = (lnet_nid_t *)value;
204
205         *nid = libcfs_str2nid(buf);
206         if (*nid != LNET_NID_ANY)
207                 return 0;
208
209         if (!quiet)
210                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
211         return -EINVAL;
212 }
213
214 static int parse_net(char *buf, void *value)
215 {
216         __u32 *net = (__u32 *)value;
217
218         *net = libcfs_str2net(buf);
219         CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
220         return 0;
221 }
222
223 enum {
224         CLASS_PARSE_NID = 1,
225         CLASS_PARSE_NET,
226 };
227
228 /* 0 is good nid,
229    1 not found
230    < 0 error
231    endh is set to next separator */
232 static int class_parse_value(char *buf, int opc, void *value, char **endh,
233                              int quiet)
234 {
235         char *endp;
236         char  tmp;
237         int   rc = 0;
238
239         if (!buf)
240                 return 1;
241         while (*buf == ',' || *buf == ':')
242                 buf++;
243         if (*buf == ' ' || *buf == '/' || *buf == '\0')
244                 return 1;
245
246         /* nid separators or end of nids */
247         endp = strpbrk(buf, ",: /");
248         if (endp == NULL)
249                 endp = buf + strlen(buf);
250
251         tmp = *endp;
252         *endp = '\0';
253         switch (opc) {
254         default:
255                 LBUG();
256         case CLASS_PARSE_NID:
257                 rc = parse_nid(buf, value, quiet);
258                 break;
259         case CLASS_PARSE_NET:
260                 rc = parse_net(buf, value);
261                 break;
262         }
263         *endp = tmp;
264         if (rc != 0)
265                 return rc;
266         if (endh)
267                 *endh = endp;
268         return 0;
269 }
270
271 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
272 {
273         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
274 }
275 EXPORT_SYMBOL(class_parse_nid);
276
277 int class_parse_nid_quiet(char *buf, lnet_nid_t *nid, char **endh)
278 {
279         return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
280 }
281 EXPORT_SYMBOL(class_parse_nid_quiet);
282
283 int class_parse_net(char *buf, __u32 *net, char **endh)
284 {
285         return class_parse_value(buf, CLASS_PARSE_NET, (void *)net, endh, 0);
286 }
287
288 /* 1 param contains key and match
289  * 0 param contains key and not match
290  * -1 param does not contain key
291  */
292 int class_match_nid(char *buf, char *key, lnet_nid_t nid)
293 {
294         lnet_nid_t tmp;
295         int   rc = -1;
296
297         while (class_find_param(buf, key, &buf) == 0) {
298                 /* please restrict to the nids pertaining to
299                  * the specified nids */
300                 while (class_parse_nid(buf, &tmp, &buf) == 0) {
301                         if (tmp == nid)
302                                 return 1;
303                 }
304                 rc = 0;
305         }
306         return rc;
307 }
308
309 int class_match_net(char *buf, char *key, __u32 net)
310 {
311         __u32 tmp;
312         int   rc = -1;
313
314         while (class_find_param(buf, key, &buf) == 0) {
315                 /* please restrict to the nids pertaining to
316                  * the specified networks */
317                 while (class_parse_net(buf, &tmp, &buf) == 0) {
318                         if (tmp == net)
319                                 return 1;
320                 }
321                 rc = 0;
322         }
323         return rc;
324 }
325
326 /********************** class fns **********************/
327
328 /**
329  * Create a new obd device and set the type, name and uuid.  If successful,
330  * the new device can be accessed by either name or uuid.
331  */
332 int class_attach(struct lustre_cfg *lcfg)
333 {
334         struct obd_device *obd = NULL;
335         char *typename, *name, *uuid;
336         int rc, len;
337         ENTRY;
338
339         if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
340                 CERROR("No type passed!\n");
341                 RETURN(-EINVAL);
342         }
343         typename = lustre_cfg_string(lcfg, 1);
344
345         if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
346                 CERROR("No name passed!\n");
347                 RETURN(-EINVAL);
348         }
349         name = lustre_cfg_string(lcfg, 0);
350
351         if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
352                 CERROR("No UUID passed!\n");
353                 RETURN(-EINVAL);
354         }
355         uuid = lustre_cfg_string(lcfg, 2);
356
357         CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
358                MKSTR(typename), MKSTR(name), MKSTR(uuid));
359
360         obd = class_newdev(typename, name);
361         if (IS_ERR(obd)) {
362                 /* Already exists or out of obds */
363                 rc = PTR_ERR(obd);
364                 obd = NULL;
365                 CERROR("Cannot create device %s of type %s : %d\n",
366                        name, typename, rc);
367                 GOTO(out, rc);
368         }
369         LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
370                  name, typename);
371         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
372                  "obd %p obd_magic %08X != %08X\n",
373                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
374         LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
375                  "%p obd_name %s != %s\n", obd, obd->obd_name, name);
376
377         rwlock_init(&obd->obd_pool_lock);
378         obd->obd_pool_limit = 0;
379         obd->obd_pool_slv = 0;
380
381         INIT_LIST_HEAD(&obd->obd_exports);
382         INIT_LIST_HEAD(&obd->obd_unlinked_exports);
383         INIT_LIST_HEAD(&obd->obd_delayed_exports);
384         INIT_LIST_HEAD(&obd->obd_exports_timed);
385         INIT_LIST_HEAD(&obd->obd_nid_stats);
386         spin_lock_init(&obd->obd_nid_lock);
387         spin_lock_init(&obd->obd_dev_lock);
388         mutex_init(&obd->obd_dev_mutex);
389         spin_lock_init(&obd->obd_osfs_lock);
390         /* obd->obd_osfs_age must be set to a value in the distant
391          * past to guarantee a fresh statfs is fetched on mount. */
392         obd->obd_osfs_age = cfs_time_shift_64(-1000);
393
394         /* XXX belongs in setup not attach  */
395         init_rwsem(&obd->obd_observer_link_sem);
396         /* recovery data */
397         cfs_init_timer(&obd->obd_recovery_timer);
398         spin_lock_init(&obd->obd_recovery_task_lock);
399         init_waitqueue_head(&obd->obd_next_transno_waitq);
400         init_waitqueue_head(&obd->obd_evict_inprogress_waitq);
401         INIT_LIST_HEAD(&obd->obd_req_replay_queue);
402         INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
403         INIT_LIST_HEAD(&obd->obd_final_req_queue);
404         INIT_LIST_HEAD(&obd->obd_evict_list);
405         INIT_LIST_HEAD(&obd->obd_lwp_list);
406
407         llog_group_init(&obd->obd_olg);
408
409         obd->obd_conn_inprogress = 0;
410
411         len = strlen(uuid);
412         if (len >= sizeof(obd->obd_uuid)) {
413                 CERROR("uuid must be < %d bytes long\n",
414                        (int)sizeof(obd->obd_uuid));
415                 GOTO(out, rc = -EINVAL);
416         }
417         memcpy(obd->obd_uuid.uuid, uuid, len);
418
419         /* Detach drops this */
420         spin_lock(&obd->obd_dev_lock);
421         atomic_set(&obd->obd_refcount, 1);
422         spin_unlock(&obd->obd_dev_lock);
423         lu_ref_init(&obd->obd_reference);
424         lu_ref_add(&obd->obd_reference, "attach", obd);
425
426         obd->obd_attached = 1;
427         CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
428                obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
429         RETURN(0);
430  out:
431         if (obd != NULL) {
432                 class_release_dev(obd);
433         }
434         return rc;
435 }
436 EXPORT_SYMBOL(class_attach);
437
438 /** Create hashes, self-export, and call type-specific setup.
439  * Setup is effectively the "start this obd" call.
440  */
441 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
442 {
443         int err = 0;
444         struct obd_export *exp;
445         ENTRY;
446
447         LASSERT(obd != NULL);
448         LASSERTF(obd == class_num2obd(obd->obd_minor),
449                  "obd %p != obd_devs[%d] %p\n",
450                  obd, obd->obd_minor, class_num2obd(obd->obd_minor));
451         LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
452                  "obd %p obd_magic %08x != %08x\n",
453                  obd, obd->obd_magic, OBD_DEVICE_MAGIC);
454
455         /* have we attached a type to this device? */
456         if (!obd->obd_attached) {
457                 CERROR("Device %d not attached\n", obd->obd_minor);
458                 RETURN(-ENODEV);
459         }
460
461         if (obd->obd_set_up) {
462                 CERROR("Device %d already setup (type %s)\n",
463                        obd->obd_minor, obd->obd_type->typ_name);
464                 RETURN(-EEXIST);
465         }
466
467         /* is someone else setting us up right now? (attach inits spinlock) */
468         spin_lock(&obd->obd_dev_lock);
469         if (obd->obd_starting) {
470                 spin_unlock(&obd->obd_dev_lock);
471                 CERROR("Device %d setup in progress (type %s)\n",
472                        obd->obd_minor, obd->obd_type->typ_name);
473                 RETURN(-EEXIST);
474         }
475         /* just leave this on forever.  I can't use obd_set_up here because
476            other fns check that status, and we're not actually set up yet. */
477         obd->obd_starting = 1;
478         obd->obd_uuid_hash = NULL;
479         obd->obd_nid_hash = NULL;
480         obd->obd_nid_stats_hash = NULL;
481         obd->obd_gen_hash = NULL;
482         spin_unlock(&obd->obd_dev_lock);
483
484         /* create an uuid-export lustre hash */
485         obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
486                                              HASH_UUID_CUR_BITS,
487                                              HASH_UUID_MAX_BITS,
488                                              HASH_UUID_BKT_BITS, 0,
489                                              CFS_HASH_MIN_THETA,
490                                              CFS_HASH_MAX_THETA,
491                                              &uuid_hash_ops, CFS_HASH_DEFAULT);
492         if (!obd->obd_uuid_hash)
493                 GOTO(err_hash, err = -ENOMEM);
494
495         /* create a nid-export lustre hash */
496         obd->obd_nid_hash = cfs_hash_create("NID_HASH",
497                                             HASH_NID_CUR_BITS,
498                                             HASH_NID_MAX_BITS,
499                                             HASH_NID_BKT_BITS, 0,
500                                             CFS_HASH_MIN_THETA,
501                                             CFS_HASH_MAX_THETA,
502                                             &nid_hash_ops, CFS_HASH_DEFAULT);
503         if (!obd->obd_nid_hash)
504                 GOTO(err_hash, err = -ENOMEM);
505
506         /* create a nid-stats lustre hash */
507         obd->obd_nid_stats_hash = cfs_hash_create("NID_STATS",
508                                                   HASH_NID_STATS_CUR_BITS,
509                                                   HASH_NID_STATS_MAX_BITS,
510                                                   HASH_NID_STATS_BKT_BITS, 0,
511                                                   CFS_HASH_MIN_THETA,
512                                                   CFS_HASH_MAX_THETA,
513                                                   &nid_stat_hash_ops, CFS_HASH_DEFAULT);
514         if (!obd->obd_nid_stats_hash)
515                 GOTO(err_hash, err = -ENOMEM);
516
517         /* create a client_generation-export lustre hash */
518         obd->obd_gen_hash = cfs_hash_create("UUID_HASH",
519                                             HASH_GEN_CUR_BITS,
520                                             HASH_GEN_MAX_BITS,
521                                             HASH_GEN_BKT_BITS, 0,
522                                             CFS_HASH_MIN_THETA,
523                                             CFS_HASH_MAX_THETA,
524                                             &gen_hash_ops, CFS_HASH_DEFAULT);
525         if (!obd->obd_gen_hash)
526                 GOTO(err_hash, err = -ENOMEM);
527
528         exp = class_new_export(obd, &obd->obd_uuid);
529         if (IS_ERR(exp))
530                 GOTO(err_hash, err = PTR_ERR(exp));
531
532         obd->obd_self_export = exp;
533         list_del_init(&exp->exp_obd_chain_timed);
534         class_export_put(exp);
535
536         err = obd_setup(obd, lcfg);
537         if (err)
538                 GOTO(err_exp, err);
539
540         obd->obd_set_up = 1;
541
542         spin_lock(&obd->obd_dev_lock);
543         /* cleanup drops this */
544         class_incref(obd, "setup", obd);
545         spin_unlock(&obd->obd_dev_lock);
546
547         CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
548                obd->obd_name, obd->obd_uuid.uuid);
549
550         RETURN(0);
551 err_exp:
552         if (obd->obd_self_export) {
553                 class_unlink_export(obd->obd_self_export);
554                 obd->obd_self_export = NULL;
555         }
556 err_hash:
557         if (obd->obd_uuid_hash) {
558                 cfs_hash_putref(obd->obd_uuid_hash);
559                 obd->obd_uuid_hash = NULL;
560         }
561         if (obd->obd_nid_hash) {
562                 cfs_hash_putref(obd->obd_nid_hash);
563                 obd->obd_nid_hash = NULL;
564         }
565         if (obd->obd_nid_stats_hash) {
566                 cfs_hash_putref(obd->obd_nid_stats_hash);
567                 obd->obd_nid_stats_hash = NULL;
568         }
569         if (obd->obd_gen_hash) {
570                 cfs_hash_putref(obd->obd_gen_hash);
571                 obd->obd_gen_hash = NULL;
572         }
573         obd->obd_starting = 0;
574         CERROR("setup %s failed (%d)\n", obd->obd_name, err);
575         return err;
576 }
577 EXPORT_SYMBOL(class_setup);
578
579 /** We have finished using this obd and are ready to destroy it.
580  * There can be no more references to this obd.
581  */
582 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
583 {
584         ENTRY;
585
586         if (obd->obd_set_up) {
587                 CERROR("OBD device %d still set up\n", obd->obd_minor);
588                 RETURN(-EBUSY);
589         }
590
591         spin_lock(&obd->obd_dev_lock);
592         if (!obd->obd_attached) {
593                 spin_unlock(&obd->obd_dev_lock);
594                 CERROR("OBD device %d not attached\n", obd->obd_minor);
595                 RETURN(-ENODEV);
596         }
597         obd->obd_attached = 0;
598         spin_unlock(&obd->obd_dev_lock);
599
600         CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
601                obd->obd_name, obd->obd_uuid.uuid);
602
603         class_decref(obd, "attach", obd);
604         RETURN(0);
605 }
606 EXPORT_SYMBOL(class_detach);
607
608 /** Start shutting down the obd.  There may be in-progess ops when
609  * this is called.  We tell them to start shutting down with a call
610  * to class_disconnect_exports().
611  */
612 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
613 {
614         int err = 0;
615         char *flag;
616         ENTRY;
617
618         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
619
620         if (!obd->obd_set_up) {
621                 CERROR("Device %d not setup\n", obd->obd_minor);
622                 RETURN(-ENODEV);
623         }
624
625         spin_lock(&obd->obd_dev_lock);
626         if (obd->obd_stopping) {
627                 spin_unlock(&obd->obd_dev_lock);
628                 CERROR("OBD %d already stopping\n", obd->obd_minor);
629                 RETURN(-ENODEV);
630         }
631         /* Leave this on forever */
632         obd->obd_stopping = 1;
633         spin_unlock(&obd->obd_dev_lock);
634
635         /* wait for already-arrived-connections to finish. */
636         while (obd->obd_conn_inprogress > 0)
637                 yield();
638         smp_rmb();
639
640         if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
641                 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
642                         switch (*flag) {
643                         case 'F':
644                                 obd->obd_force = 1;
645                                 break;
646                         case 'A':
647                                 LCONSOLE_WARN("Failing over %s\n",
648                                               obd->obd_name);
649                                 obd->obd_fail = 1;
650                                 obd->obd_no_transno = 1;
651                                 obd->obd_no_recov = 1;
652                                 if (OBP(obd, iocontrol)) {
653                                         obd_iocontrol(OBD_IOC_SYNC,
654                                                       obd->obd_self_export,
655                                                       0, NULL, NULL);
656                                 }
657                                 break;
658                         default:
659                                 CERROR("Unrecognised flag '%c'\n", *flag);
660                         }
661         }
662
663         LASSERT(obd->obd_self_export);
664
665         /* The three references that should be remaining are the
666          * obd_self_export and the attach and setup references. */
667         if (atomic_read(&obd->obd_refcount) > 3) {
668                 /* refcounf - 3 might be the number of real exports
669                    (excluding self export). But class_incref is called
670                    by other things as well, so don't count on it. */
671                 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
672                        obd->obd_name, atomic_read(&obd->obd_refcount) - 3);
673                 dump_exports(obd, 0, D_HA);
674                 class_disconnect_exports(obd);
675         }
676
677         /* Precleanup, we must make sure all exports get destroyed. */
678         err = obd_precleanup(obd);
679         if (err)
680                 CERROR("Precleanup %s returned %d\n",
681                        obd->obd_name, err);
682
683         /* destroy an uuid-export hash body */
684         if (obd->obd_uuid_hash) {
685                 cfs_hash_putref(obd->obd_uuid_hash);
686                 obd->obd_uuid_hash = NULL;
687         }
688
689         /* destroy a nid-export hash body */
690         if (obd->obd_nid_hash) {
691                 cfs_hash_putref(obd->obd_nid_hash);
692                 obd->obd_nid_hash = NULL;
693         }
694
695         /* destroy a nid-stats hash body */
696         if (obd->obd_nid_stats_hash) {
697                 cfs_hash_putref(obd->obd_nid_stats_hash);
698                 obd->obd_nid_stats_hash = NULL;
699         }
700
701         /* destroy a client_generation-export hash body */
702         if (obd->obd_gen_hash) {
703                 cfs_hash_putref(obd->obd_gen_hash);
704                 obd->obd_gen_hash = NULL;
705         }
706
707         class_decref(obd, "setup", obd);
708         obd->obd_set_up = 0;
709
710         RETURN(0);
711 }
712
713 struct obd_device *class_incref(struct obd_device *obd,
714                                 const char *scope, const void *source)
715 {
716         lu_ref_add_atomic(&obd->obd_reference, scope, source);
717         atomic_inc(&obd->obd_refcount);
718         CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
719                atomic_read(&obd->obd_refcount));
720
721         return obd;
722 }
723 EXPORT_SYMBOL(class_incref);
724
725 void class_decref(struct obd_device *obd, const char *scope, const void *source)
726 {
727         int err;
728         int refs;
729
730         spin_lock(&obd->obd_dev_lock);
731         atomic_dec(&obd->obd_refcount);
732         refs = atomic_read(&obd->obd_refcount);
733         spin_unlock(&obd->obd_dev_lock);
734         lu_ref_del(&obd->obd_reference, scope, source);
735
736         CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
737
738         if ((refs == 1) && obd->obd_stopping) {
739                 /* All exports have been destroyed; there should
740                    be no more in-progress ops by this point.*/
741
742                 spin_lock(&obd->obd_self_export->exp_lock);
743                 obd->obd_self_export->exp_flags |= exp_flags_from_obd(obd);
744                 spin_unlock(&obd->obd_self_export->exp_lock);
745
746                 /* note that we'll recurse into class_decref again */
747                 class_unlink_export(obd->obd_self_export);
748                 return;
749         }
750
751         if (refs == 0) {
752                 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
753                        obd->obd_name, obd->obd_uuid.uuid);
754                 LASSERT(!obd->obd_attached);
755                 if (obd->obd_stopping) {
756                         /* If we're not stopping, we were never set up */
757                         err = obd_cleanup(obd);
758                         if (err)
759                                 CERROR("Cleanup %s returned %d\n",
760                                        obd->obd_name, err);
761                 }
762
763                 class_release_dev(obd);
764         }
765 }
766 EXPORT_SYMBOL(class_decref);
767
768 /** Add a failover nid location.
769  * Client obd types contact server obd types using this nid list.
770  */
771 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
772 {
773         struct obd_import *imp;
774         struct obd_uuid uuid;
775         int rc;
776         ENTRY;
777
778         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
779             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
780                 CERROR("invalid conn_uuid\n");
781                 RETURN(-EINVAL);
782         }
783         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
784             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
785             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
786             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
787             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
788                 CERROR("can't add connection on non-client dev\n");
789                 RETURN(-EINVAL);
790         }
791
792         imp = obd->u.cli.cl_import;
793         if (!imp) {
794                 CERROR("try to add conn on immature client dev\n");
795                 RETURN(-EINVAL);
796         }
797
798         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
799         rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
800
801         RETURN(rc);
802 }
803
804 /** Remove a failover nid location.
805  */
806 static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
807 {
808         struct obd_import *imp;
809         struct obd_uuid uuid;
810         int rc;
811         ENTRY;
812
813         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
814             LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
815                 CERROR("invalid conn_uuid\n");
816                 RETURN(-EINVAL);
817         }
818         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
819             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
820                 CERROR("can't del connection on non-client dev\n");
821                 RETURN(-EINVAL);
822         }
823
824         imp = obd->u.cli.cl_import;
825         if (!imp) {
826                 CERROR("try to del conn on immature client dev\n");
827                 RETURN(-EINVAL);
828         }
829
830         obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
831         rc = obd_del_conn(imp, &uuid);
832
833         RETURN(rc);
834 }
835
836 static struct list_head lustre_profile_list =
837         LIST_HEAD_INIT(lustre_profile_list);
838 static DEFINE_SPINLOCK(lustre_profile_list_lock);
839
840 struct lustre_profile *class_get_profile(const char * prof)
841 {
842         struct lustre_profile *lprof;
843
844         ENTRY;
845         spin_lock(&lustre_profile_list_lock);
846         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
847                 if (!strcmp(lprof->lp_profile, prof)) {
848                         lprof->lp_refs++;
849                         spin_unlock(&lustre_profile_list_lock);
850                         RETURN(lprof);
851                 }
852         }
853         spin_unlock(&lustre_profile_list_lock);
854         RETURN(NULL);
855 }
856 EXPORT_SYMBOL(class_get_profile);
857
858 /** Create a named "profile".
859  * This defines the mdc and osc names to use for a client.
860  * This also is used to define the lov to be used by a mdt.
861  */
862 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
863                              int mdclen, char *mdc)
864 {
865         struct lustre_profile *lprof;
866         int err = 0;
867         ENTRY;
868
869         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
870
871         OBD_ALLOC(lprof, sizeof(*lprof));
872         if (lprof == NULL)
873                 RETURN(-ENOMEM);
874         INIT_LIST_HEAD(&lprof->lp_list);
875
876         LASSERT(proflen == (strlen(prof) + 1));
877         OBD_ALLOC(lprof->lp_profile, proflen);
878         if (lprof->lp_profile == NULL)
879                 GOTO(out, err = -ENOMEM);
880         memcpy(lprof->lp_profile, prof, proflen);
881
882         LASSERT(osclen == (strlen(osc) + 1));
883         OBD_ALLOC(lprof->lp_dt, osclen);
884         if (lprof->lp_dt == NULL)
885                 GOTO(out, err = -ENOMEM);
886         memcpy(lprof->lp_dt, osc, osclen);
887
888         if (mdclen > 0) {
889                 LASSERT(mdclen == (strlen(mdc) + 1));
890                 OBD_ALLOC(lprof->lp_md, mdclen);
891                 if (lprof->lp_md == NULL)
892                         GOTO(out, err = -ENOMEM);
893                 memcpy(lprof->lp_md, mdc, mdclen);
894         }
895
896         spin_lock(&lustre_profile_list_lock);
897         lprof->lp_refs = 1;
898         lprof->lp_list_deleted = false;
899
900         list_add(&lprof->lp_list, &lustre_profile_list);
901         spin_unlock(&lustre_profile_list_lock);
902         RETURN(err);
903
904 out:
905         if (lprof->lp_md)
906                 OBD_FREE(lprof->lp_md, mdclen);
907         if (lprof->lp_dt)
908                 OBD_FREE(lprof->lp_dt, osclen);
909         if (lprof->lp_profile)
910                 OBD_FREE(lprof->lp_profile, proflen);
911         OBD_FREE(lprof, sizeof(*lprof));
912         RETURN(err);
913 }
914
915 void class_del_profile(const char *prof)
916 {
917         struct lustre_profile *lprof;
918         ENTRY;
919
920         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
921
922         lprof = class_get_profile(prof);
923         if (lprof) {
924                 spin_lock(&lustre_profile_list_lock);
925                 /* because get profile increments the ref counter */
926                 lprof->lp_refs--;
927                 list_del(&lprof->lp_list);
928                 lprof->lp_list_deleted = true;
929                 spin_unlock(&lustre_profile_list_lock);
930
931                 class_put_profile(lprof);
932         }
933         EXIT;
934 }
935 EXPORT_SYMBOL(class_del_profile);
936
937 void class_put_profile(struct lustre_profile *lprof)
938 {
939         spin_lock(&lustre_profile_list_lock);
940         if ((--lprof->lp_refs) > 0) {
941                 LASSERT(lprof->lp_refs > 0);
942                 spin_unlock(&lustre_profile_list_lock);
943                 return;
944         }
945         spin_unlock(&lustre_profile_list_lock);
946
947         /* confirm not a negative number */
948         LASSERT(lprof->lp_refs == 0);
949
950         /* At least one class_del_profile/profiles must be called
951          * on the target profile or lustre_profile_list will corrupt */
952         LASSERT(lprof->lp_list_deleted);
953         OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
954         OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
955         if (lprof->lp_md != NULL)
956                 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
957         OBD_FREE(lprof, sizeof(*lprof));
958 }
959 EXPORT_SYMBOL(class_put_profile);
960
961 /* COMPAT_146 */
962 void class_del_profiles(void)
963 {
964         struct lustre_profile *lprof, *n;
965         ENTRY;
966
967         spin_lock(&lustre_profile_list_lock);
968         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
969                 list_del(&lprof->lp_list);
970                 lprof->lp_list_deleted = true;
971                 spin_unlock(&lustre_profile_list_lock);
972
973                 class_put_profile(lprof);
974
975                 spin_lock(&lustre_profile_list_lock);
976         }
977         spin_unlock(&lustre_profile_list_lock);
978         EXIT;
979 }
980 EXPORT_SYMBOL(class_del_profiles);
981
982 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
983 {
984         ENTRY;
985         if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
986                 at_min = val;
987         else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
988                 at_max = val;
989         else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
990                 at_extra = val;
991         else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
992                 at_early_margin = val;
993         else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
994                 at_history = val;
995         else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
996                 strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
997                         JOBSTATS_JOBID_VAR_MAX_LEN + 1);
998         else
999                 RETURN(-EINVAL);
1000
1001         CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
1002         RETURN(0);
1003 }
1004
1005
1006 /* We can't call ll_process_config or lquota_process_config directly because
1007  * it lives in a module that must be loaded after this one. */
1008 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
1009 static int (*quota_process_config)(struct lustre_cfg *lcfg) = NULL;
1010
1011 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
1012 {
1013         client_process_config = cpc;
1014 }
1015 EXPORT_SYMBOL(lustre_register_client_process_config);
1016
1017 /**
1018  * Rename the proc parameter in \a cfg with a new name \a new_name.
1019  *
1020  * \param cfg      config structure which contains the proc parameter
1021  * \param new_name new name of the proc parameter
1022  *
1023  * \retval valid-pointer    pointer to the newly-allocated config structure
1024  *                          which contains the renamed proc parameter
1025  * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
1026  *                          not contain a proc parameter
1027  * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
1028  */
1029 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
1030                                      const char *new_name)
1031 {
1032         struct lustre_cfg_bufs  *bufs = NULL;
1033         struct lustre_cfg       *new_cfg = NULL;
1034         char                    *param = NULL;
1035         char                    *new_param = NULL;
1036         char                    *value = NULL;
1037         int                      name_len = 0;
1038         int                      new_len = 0;
1039         ENTRY;
1040
1041         if (cfg == NULL || new_name == NULL)
1042                 RETURN(ERR_PTR(-EINVAL));
1043
1044         param = lustre_cfg_string(cfg, 1);
1045         if (param == NULL)
1046                 RETURN(ERR_PTR(-EINVAL));
1047
1048         value = strchr(param, '=');
1049         if (value == NULL)
1050                 name_len = strlen(param);
1051         else
1052                 name_len = value - param;
1053
1054         new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
1055
1056         OBD_ALLOC(new_param, new_len);
1057         if (new_param == NULL)
1058                 RETURN(ERR_PTR(-ENOMEM));
1059
1060         strcpy(new_param, new_name);
1061         if (value != NULL)
1062                 strcat(new_param, value);
1063
1064         OBD_ALLOC_PTR(bufs);
1065         if (bufs == NULL) {
1066                 OBD_FREE(new_param, new_len);
1067                 RETURN(ERR_PTR(-ENOMEM));
1068         }
1069
1070         lustre_cfg_bufs_reset(bufs, NULL);
1071         lustre_cfg_bufs_init(bufs, cfg);
1072         lustre_cfg_bufs_set_string(bufs, 1, new_param);
1073
1074         new_cfg = lustre_cfg_new(cfg->lcfg_command, bufs);
1075         OBD_FREE(new_param, new_len);
1076         OBD_FREE_PTR(bufs);
1077         if (new_cfg == NULL)
1078                 RETURN(ERR_PTR(-ENOMEM));
1079
1080         new_cfg->lcfg_num = cfg->lcfg_num;
1081         new_cfg->lcfg_flags = cfg->lcfg_flags;
1082         new_cfg->lcfg_nid = cfg->lcfg_nid;
1083         new_cfg->lcfg_nal = cfg->lcfg_nal;
1084
1085         RETURN(new_cfg);
1086 }
1087 EXPORT_SYMBOL(lustre_cfg_rename);
1088
1089 static int process_param2_config(struct lustre_cfg *lcfg)
1090 {
1091         char *param = lustre_cfg_string(lcfg, 1);
1092         char *upcall = lustre_cfg_string(lcfg, 2);
1093         char *argv[] = {
1094                 [0] = "/usr/sbin/lctl",
1095                 [1] = "set_param",
1096                 [2] = param,
1097                 [3] = NULL
1098         };
1099         struct timeval  start;
1100         struct timeval  end;
1101         int             rc;
1102         ENTRY;
1103
1104         /* Add upcall processing here. Now only lctl is supported */
1105         if (strcmp(upcall, LCTL_UPCALL) != 0) {
1106                 CERROR("Unsupported upcall %s\n", upcall);
1107                 RETURN(-EINVAL);
1108         }
1109
1110         do_gettimeofday(&start);
1111         rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1112         do_gettimeofday(&end);
1113
1114         if (rc < 0) {
1115                 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1116                        "time %ldus\n", argv[0], argv[1], argv[2], rc,
1117                        cfs_timeval_sub(&end, &start, NULL));
1118         } else {
1119                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1120                        argv[0], argv[1], argv[2],
1121                        cfs_timeval_sub(&end, &start, NULL));
1122                        rc = 0;
1123         }
1124
1125         RETURN(rc);
1126 }
1127
1128 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1129 {
1130         quota_process_config = qpc;
1131 }
1132 EXPORT_SYMBOL(lustre_register_quota_process_config);
1133
1134 /** Process configuration commands given in lustre_cfg form.
1135  * These may come from direct calls (e.g. class_manual_cleanup)
1136  * or processing the config llog, or ioctl from lctl.
1137  */
1138 int class_process_config(struct lustre_cfg *lcfg)
1139 {
1140         struct obd_device *obd;
1141         int err;
1142
1143         LASSERT(lcfg && !IS_ERR(lcfg));
1144         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1145
1146         /* Commands that don't need a device */
1147         switch(lcfg->lcfg_command) {
1148         case LCFG_ATTACH: {
1149                 err = class_attach(lcfg);
1150                 GOTO(out, err);
1151         }
1152         case LCFG_ADD_UUID: {
1153                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx"
1154                        " (%s)\n", lustre_cfg_string(lcfg, 1),
1155                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1156
1157                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1158                 GOTO(out, err);
1159         }
1160         case LCFG_DEL_UUID: {
1161                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1162                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1163                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1164
1165                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1166                 GOTO(out, err);
1167         }
1168         case LCFG_MOUNTOPT: {
1169                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1170                        lustre_cfg_string(lcfg, 1),
1171                        lustre_cfg_string(lcfg, 2),
1172                        lustre_cfg_string(lcfg, 3));
1173                 /* set these mount options somewhere, so ll_fill_super
1174                  * can find them. */
1175                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1176                                         lustre_cfg_string(lcfg, 1),
1177                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1178                                         lustre_cfg_string(lcfg, 2),
1179                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1180                                         lustre_cfg_string(lcfg, 3));
1181                 GOTO(out, err);
1182         }
1183         case LCFG_DEL_MOUNTOPT: {
1184                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1185                        lustre_cfg_string(lcfg, 1));
1186                 class_del_profile(lustre_cfg_string(lcfg, 1));
1187                 GOTO(out, err = 0);
1188         }
1189         case LCFG_SET_TIMEOUT: {
1190                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1191                        obd_timeout, lcfg->lcfg_num);
1192                 obd_timeout = max(lcfg->lcfg_num, 1U);
1193                 obd_timeout_set = 1;
1194                 GOTO(out, err = 0);
1195         }
1196         case LCFG_SET_LDLM_TIMEOUT: {
1197                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1198                        ldlm_timeout, lcfg->lcfg_num);
1199                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1200                 if (ldlm_timeout >= obd_timeout)
1201                         ldlm_timeout = max(obd_timeout / 3, 1U);
1202                 ldlm_timeout_set = 1;
1203                 GOTO(out, err = 0);
1204         }
1205         case LCFG_SET_UPCALL: {
1206                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1207                 /* COMPAT_146 Don't fail on old configs */
1208                 GOTO(out, err = 0);
1209         }
1210         case LCFG_MARKER: {
1211                 struct cfg_marker *marker;
1212                 marker = lustre_cfg_buf(lcfg, 1);
1213                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1214                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1215                 GOTO(out, err = 0);
1216         }
1217         case LCFG_PARAM: {
1218                 char *tmp;
1219                 /* llite has no obd */
1220                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1221                                        PARAM_LLITE, NULL) == 0) &&
1222                     client_process_config) {
1223                         err = (*client_process_config)(lcfg);
1224                         GOTO(out, err);
1225                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1226                                               PARAM_SYS, &tmp) == 0)) {
1227                         /* Global param settings */
1228                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1229                         /*
1230                          * Client or server should not fail to mount if
1231                          * it hits an unknown configuration parameter.
1232                          */
1233                         if (err != 0)
1234                                 CWARN("Ignoring unknown param %s\n", tmp);
1235
1236                         GOTO(out, err = 0);
1237                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1238                                               PARAM_QUOTA, &tmp) == 0) &&
1239                            quota_process_config) {
1240                         err = (*quota_process_config)(lcfg);
1241                         GOTO(out, err);
1242                 }
1243
1244                 break;
1245         }
1246         case LCFG_SET_PARAM: {
1247                 err = process_param2_config(lcfg);
1248                 GOTO(out, err = 0);
1249         }
1250         }
1251         /* Commands that require a device */
1252         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1253         if (obd == NULL) {
1254                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1255                         CERROR("this lcfg command requires a device name\n");
1256                 else
1257                         CERROR("no device for: %s\n",
1258                                lustre_cfg_string(lcfg, 0));
1259
1260                 GOTO(out, err = -EINVAL);
1261         }
1262
1263         switch(lcfg->lcfg_command) {
1264         case LCFG_SETUP: {
1265                 err = class_setup(obd, lcfg);
1266                 GOTO(out, err);
1267         }
1268         case LCFG_DETACH: {
1269                 err = class_detach(obd, lcfg);
1270                 GOTO(out, err = 0);
1271         }
1272         case LCFG_CLEANUP: {
1273                 err = class_cleanup(obd, lcfg);
1274                 GOTO(out, err = 0);
1275         }
1276         case LCFG_ADD_CONN: {
1277                 err = class_add_conn(obd, lcfg);
1278                 GOTO(out, err = 0);
1279         }
1280         case LCFG_DEL_CONN: {
1281                 err = class_del_conn(obd, lcfg);
1282                 GOTO(out, err = 0);
1283         }
1284         case LCFG_POOL_NEW: {
1285                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1286                 GOTO(out, err = 0);
1287         }
1288         case LCFG_POOL_ADD: {
1289                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1290                                    lustre_cfg_string(lcfg, 3));
1291                 GOTO(out, err = 0);
1292         }
1293         case LCFG_POOL_REM: {
1294                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1295                                    lustre_cfg_string(lcfg, 3));
1296                 GOTO(out, err = 0);
1297         }
1298         case LCFG_POOL_DEL: {
1299                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1300                 GOTO(out, err = 0);
1301         }
1302         default: {
1303                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1304                 GOTO(out, err);
1305
1306         }
1307         }
1308 out:
1309         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1310                 CWARN("Ignoring error %d on optional command %#x\n", err,
1311                       lcfg->lcfg_command);
1312                 err = 0;
1313         }
1314         return err;
1315 }
1316 EXPORT_SYMBOL(class_process_config);
1317
1318 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1319                              struct lustre_cfg *lcfg, void *data)
1320 {
1321         struct lprocfs_vars *var;
1322         struct file fakefile;
1323         struct seq_file fake_seqfile;
1324         char *key, *sval;
1325         int i, keylen, vallen;
1326         int matched = 0, j = 0;
1327         int rc = 0;
1328         int skip = 0;
1329         ENTRY;
1330
1331         if (lcfg->lcfg_command != LCFG_PARAM) {
1332                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1333                 RETURN(-EINVAL);
1334         }
1335
1336         /* fake a seq file so that var->fops->write can work... */
1337         fakefile.private_data = &fake_seqfile;
1338         fake_seqfile.private = data;
1339         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1340            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1341            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1342         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1343                 key = lustre_cfg_buf(lcfg, i);
1344                 /* Strip off prefix */
1345                 if (class_match_param(key, prefix, &key))
1346                         /* If the prefix doesn't match, return error so we
1347                          * can pass it down the stack */
1348                         RETURN(-ENOSYS);
1349                 sval = strchr(key, '=');
1350                 if (!sval || (*(sval + 1) == 0)) {
1351                         CERROR("Can't parse param %s (missing '=')\n", key);
1352                         /* rc = -EINVAL;        continue parsing other params */
1353                         continue;
1354                 }
1355                 keylen = sval - key;
1356                 sval++;
1357                 vallen = strlen(sval);
1358                 matched = 0;
1359                 j = 0;
1360                 /* Search proc entries */
1361                 while (lvars[j].name) {
1362                         var = &lvars[j];
1363                         if (class_match_param(key, var->name, NULL) == 0 &&
1364                             keylen == strlen(var->name)) {
1365                                 matched++;
1366                                 rc = -EROFS;
1367
1368                                 if (var->fops && var->fops->write) {
1369                                         mm_segment_t oldfs;
1370                                         oldfs = get_fs();
1371                                         set_fs(KERNEL_DS);
1372                                         rc = (var->fops->write)(&fakefile, sval,
1373                                                                 vallen, NULL);
1374                                         set_fs(oldfs);
1375                                 }
1376                                 break;
1377                         }
1378                         j++;
1379                 }
1380                 if (!matched) {
1381                         /* It was upgraded from old MDT/OST device,
1382                          * ignore the obsolete "sec_level" parameter. */
1383                         if (strncmp("sec_level", key, keylen) == 0)
1384                                 continue;
1385
1386                         CERROR("%.*s: %s unknown param %s\n",
1387                                (int)strlen(prefix) - 1, prefix,
1388                                (char *)lustre_cfg_string(lcfg, 0), key);
1389                         /* rc = -EINVAL;        continue parsing other params */
1390                         skip++;
1391                 } else if (rc < 0) {
1392                         CERROR("%s: error writing proc entry '%s': rc = %d\n",
1393                                prefix, var->name, rc);
1394                         rc = 0;
1395                 } else {
1396                         CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1397                                          lustre_cfg_string(lcfg, 0),
1398                                          (int)strlen(prefix) - 1, prefix,
1399                                          (int)(sval - key - 1), key, sval);
1400                 }
1401         }
1402
1403         if (rc > 0)
1404                 rc = 0;
1405         if (!rc && skip)
1406                 rc = skip;
1407         RETURN(rc);
1408 }
1409 EXPORT_SYMBOL(class_process_proc_param);
1410
1411 /*
1412  * Supplemental functions for config logs, it allocates lustre_cfg
1413  * buffers plus initialized llog record header at the beginning.
1414  */
1415 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1416 {
1417         struct llog_cfg_rec     *lcr;
1418         int                      reclen;
1419
1420         ENTRY;
1421
1422         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1423         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1424                  sizeof(struct llog_rec_tail);
1425
1426         OBD_ALLOC(lcr, reclen);
1427         if (lcr == NULL)
1428                 RETURN(NULL);
1429
1430         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1431
1432         lcr->lcr_hdr.lrh_len = reclen;
1433         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1434
1435         RETURN(lcr);
1436 }
1437 EXPORT_SYMBOL(lustre_cfg_rec_new);
1438
1439 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1440 {
1441         ENTRY;
1442         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1443         EXIT;
1444 }
1445 EXPORT_SYMBOL(lustre_cfg_rec_free);
1446
1447 /** Parse a configuration llog, doing various manipulations on them
1448  * for various reasons, (modifications for compatibility, skip obsolete
1449  * records, change uuids, etc), then class_process_config() resulting
1450  * net records.
1451  */
1452 int class_config_llog_handler(const struct lu_env *env,
1453                               struct llog_handle *handle,
1454                               struct llog_rec_hdr *rec, void *data)
1455 {
1456         struct config_llog_instance *cfg = data;
1457         int cfg_len = rec->lrh_len;
1458         char *cfg_buf = (char *) (rec + 1);
1459         int rc = 0;
1460         ENTRY;
1461
1462         /* class_config_dump_handler(handle, rec, data); */
1463
1464         switch (rec->lrh_type) {
1465         case OBD_CFG_REC: {
1466                 struct lustre_cfg *lcfg, *lcfg_new;
1467                 struct lustre_cfg_bufs bufs;
1468                 char *inst_name = NULL;
1469                 int inst_len = 0;
1470                 int inst = 0, swab = 0;
1471
1472                 lcfg = (struct lustre_cfg *)cfg_buf;
1473                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1474                         lustre_swab_lustre_cfg(lcfg);
1475                         swab = 1;
1476                 }
1477
1478                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1479                 if (rc)
1480                         GOTO(out, rc);
1481
1482                 /* Figure out config state info */
1483                 if (lcfg->lcfg_command == LCFG_MARKER) {
1484                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1485                         lustre_swab_cfg_marker(marker, swab,
1486                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1487                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1488                                cfg->cfg_flags, marker->cm_flags);
1489                         if (marker->cm_flags & CM_START) {
1490                                 /* all previous flags off */
1491                                 cfg->cfg_flags = CFG_F_MARKER;
1492                                 server_name2index(marker->cm_tgtname,
1493                                                   &cfg->cfg_lwp_idx, NULL);
1494                                 if (marker->cm_flags & CM_SKIP) {
1495                                         cfg->cfg_flags |= CFG_F_SKIP;
1496                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1497                                                marker->cm_step);
1498                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1499                                            (cfg->cfg_sb &&
1500                                            lustre_check_exclusion(cfg->cfg_sb,
1501                                                         marker->cm_tgtname))) {
1502                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1503                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1504                                                marker->cm_step);
1505                                 }
1506                         } else if (marker->cm_flags & CM_END) {
1507                                 cfg->cfg_flags = 0;
1508                         }
1509                 }
1510                 /* A config command without a start marker before it is
1511                    illegal (post 146) */
1512                 if (!(cfg->cfg_flags & CFG_F_COMPAT146) &&
1513                     !(cfg->cfg_flags & CFG_F_MARKER) &&
1514                     (lcfg->lcfg_command != LCFG_MARKER)) {
1515                         CWARN("Config not inside markers, ignoring! "
1516                               "(inst: %p, uuid: %s, flags: %#x)\n",
1517                                 cfg->cfg_instance,
1518                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1519                         cfg->cfg_flags |= CFG_F_SKIP;
1520                 }
1521                 if (cfg->cfg_flags & CFG_F_SKIP) {
1522                         CDEBUG(D_CONFIG, "skipping %#x\n",
1523                                cfg->cfg_flags);
1524                         rc = 0;
1525                         /* No processing! */
1526                         break;
1527                 }
1528
1529                 /*
1530                  * For interoperability between 1.8 and 2.0,
1531                  * rename "mds" obd device type to "mdt".
1532                  */
1533                 {
1534                         char *typename = lustre_cfg_string(lcfg, 1);
1535                         char *index = lustre_cfg_string(lcfg, 2);
1536
1537                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1538                             strcmp(typename, "mds") == 0)) {
1539                                 CWARN("For 1.8 interoperability, rename obd "
1540                                         "type from mds to mdt\n");
1541                                 typename[2] = 't';
1542                         }
1543                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1544                             strcmp(index, "type") == 0)) {
1545                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1546                                        "set this index to '0'\n");
1547                                 index[0] = '0';
1548                                 index[1] = 0;
1549                         }
1550                 }
1551
1552 #ifdef HAVE_SERVER_SUPPORT
1553                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1554                 {
1555                         char *typename = lustre_cfg_string(lcfg, 1);
1556
1557                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1558                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1559                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1560                                 CDEBUG(D_CONFIG,
1561                                        "For 2.x interoperability, rename obd "
1562                                        "type from lov to lod (%s)\n",
1563                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1564                                 strcpy(typename, LUSTRE_LOD_NAME);
1565                         }
1566                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1567                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1568                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1569                                 CDEBUG(D_CONFIG,
1570                                        "For 2.x interoperability, rename obd "
1571                                        "type from osc to osp (%s)\n",
1572                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1573                                 strcpy(typename, LUSTRE_OSP_NAME);
1574                         }
1575                 }
1576 #endif /* HAVE_SERVER_SUPPORT */
1577
1578                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1579                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1580                                lcfg->lcfg_command);
1581                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1582                                 /* Add inactive instead */
1583                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1584                 }
1585
1586                 lustre_cfg_bufs_reset(&bufs, NULL);
1587                 lustre_cfg_bufs_init(&bufs, lcfg);
1588
1589                 if (cfg->cfg_instance &&
1590                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1591                         inst = 1;
1592                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1593                                    sizeof(cfg->cfg_instance) * 2 + 4;
1594                         OBD_ALLOC(inst_name, inst_len);
1595                         if (inst_name == NULL)
1596                                 GOTO(out, rc = -ENOMEM);
1597                         snprintf(inst_name, inst_len, "%s-%p",
1598                                 lustre_cfg_string(lcfg, 0),
1599                                 cfg->cfg_instance);
1600                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1601                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1602                                lcfg->lcfg_command, inst_name);
1603                 }
1604
1605                 /* we override the llog's uuid for clients, to insure they
1606                 are unique */
1607                 if (cfg->cfg_instance != NULL &&
1608                     lcfg->lcfg_command == LCFG_ATTACH) {
1609                         lustre_cfg_bufs_set_string(&bufs, 2,
1610                                                    cfg->cfg_uuid.uuid);
1611                 }
1612                 /*
1613                  * sptlrpc config record, we expect 2 data segments:
1614                  *  [0]: fs_name/target_name,
1615                  *  [1]: rule string
1616                  * moving them to index [1] and [2], and insert MGC's
1617                  * obdname at index [0].
1618                  */
1619                 if (cfg->cfg_instance == NULL &&
1620                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1621                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1622                                             bufs.lcfg_buflen[1]);
1623                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1624                                             bufs.lcfg_buflen[0]);
1625                         lustre_cfg_bufs_set_string(&bufs, 0,
1626                                                    cfg->cfg_obdname);
1627                 }
1628
1629                 /* Add net info to setup command
1630                  * if given on command line.
1631                  * So config log will be:
1632                  * [0]: client name
1633                  * [1]: client UUID
1634                  * [2]: server UUID
1635                  * [3]: inactive-on-startup
1636                  * [4]: restrictive net
1637                  */
1638                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1639                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1640                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1641                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1642
1643                         if (lcfg->lcfg_command == LCFG_SETUP &&
1644                             lcfg->lcfg_bufcount != 2 && nidnet) {
1645                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1646                                        "command for client %s\n", nidnet,
1647                                        lustre_cfg_string(lcfg, 0));
1648                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1649                         }
1650                 }
1651
1652                 /* Skip add_conn command if uuid is
1653                  * not on restricted net */
1654                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1655                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1656                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1657                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1658
1659                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1660                             lsi->lsi_lmd->lmd_nidnet &&
1661                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1662                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1663                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1664                                        uuid_str);
1665                                 rc = 0;
1666                                 /* No processing! */
1667                                 break;
1668                         }
1669                 }
1670
1671                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1672                 if (lcfg_new == NULL)
1673                         GOTO(out, rc = -ENOMEM);
1674
1675                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1676                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1677
1678                 /* XXX Hack to try to remain binary compatible with
1679                  * pre-newconfig logs */
1680                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1681                     (lcfg->lcfg_nid >> 32) == 0) {
1682                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1683
1684                         lcfg_new->lcfg_nid =
1685                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1686                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1687                               lcfg->lcfg_nal, addr,
1688                               libcfs_nid2str(lcfg_new->lcfg_nid));
1689                 } else {
1690                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1691                 }
1692
1693                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1694
1695                 rc = class_process_config(lcfg_new);
1696                 lustre_cfg_free(lcfg_new);
1697
1698                 if (inst)
1699                         OBD_FREE(inst_name, inst_len);
1700                 break;
1701         }
1702         default:
1703                 CERROR("Unknown llog record type %#x encountered\n",
1704                        rec->lrh_type);
1705                 break;
1706         }
1707 out:
1708         if (rc) {
1709                 CERROR("%s: cfg command failed: rc = %d\n",
1710                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1711                 class_config_dump_handler(NULL, handle, rec, data);
1712         }
1713         RETURN(rc);
1714 }
1715 EXPORT_SYMBOL(class_config_llog_handler);
1716
1717 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1718                             char *name, struct config_llog_instance *cfg)
1719 {
1720         struct llog_process_cat_data     cd = {0, 0};
1721         struct llog_handle              *llh;
1722         llog_cb_t                        callback;
1723         int                              rc;
1724         ENTRY;
1725
1726         CDEBUG(D_INFO, "looking up llog %s\n", name);
1727         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1728         if (rc)
1729                 RETURN(rc);
1730
1731         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1732         if (rc)
1733                 GOTO(parse_out, rc);
1734
1735         /* continue processing from where we last stopped to end-of-log */
1736         if (cfg) {
1737                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1738                 callback = cfg->cfg_callback;
1739                 LASSERT(callback != NULL);
1740         } else {
1741                 callback = class_config_llog_handler;
1742         }
1743
1744         cd.lpcd_last_idx = 0;
1745
1746         rc = llog_process(env, llh, callback, cfg, &cd);
1747
1748         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1749                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1750         if (cfg)
1751                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1752
1753 parse_out:
1754         llog_close(env, llh);
1755         RETURN(rc);
1756 }
1757 EXPORT_SYMBOL(class_config_parse_llog);
1758
1759 static struct lcfg_type_data {
1760         __u32    ltd_type;
1761         char    *ltd_name;
1762         char    *ltd_bufs[4];
1763 } lcfg_data_table[] = {
1764         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1765         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1766         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1767         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1768         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1769         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1770         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1771         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1772         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1773         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1774         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1775         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1776         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1777         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1778         { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1779         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1780         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1781         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1782         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1783         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1784         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1785         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1786         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1787         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1788         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1789         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1790         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1791           { "parameter", "2", "3", "4" } },
1792         { 0, NULL, { NULL, NULL, NULL, NULL } }
1793 };
1794
1795 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1796 {
1797         int i = 0;
1798
1799         while (lcfg_data_table[i].ltd_type != 0) {
1800                 if (lcfg_data_table[i].ltd_type == cmd)
1801                         return &lcfg_data_table[i];
1802                 i++;
1803         }
1804         return NULL;
1805 }
1806
1807 /**
1808  * parse config record and output dump in supplied buffer.
1809  * This is separated from class_config_dump_handler() to use
1810  * for ioctl needs as well
1811  *
1812  * Sample Output:
1813  * - { event: attach, device: lustrewt-clilov, type: lov, UUID:
1814  *     lustrewt-clilov_UUID }
1815  */
1816 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1817 {
1818         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1819         char                    *ptr = buf;
1820         char                    *end = buf + size;
1821         int                      rc = 0, i;
1822         struct lcfg_type_data   *ldata;
1823
1824         LASSERT(rec->lrh_type == OBD_CFG_REC);
1825         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1826         if (rc < 0)
1827                 return rc;
1828
1829         ldata = lcfg_cmd2data(lcfg->lcfg_command);
1830         if (ldata == NULL)
1831                 return -ENOTTY;
1832
1833         if (lcfg->lcfg_command == LCFG_MARKER)
1834                 return 0;
1835
1836         /* form YAML entity */
1837         ptr += snprintf(ptr, end - ptr, "- { event: %s", ldata->ltd_name);
1838
1839         if (lcfg->lcfg_flags)
1840                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1841                                 lcfg->lcfg_flags);
1842         if (lcfg->lcfg_num)
1843                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1844                                 lcfg->lcfg_num);
1845         if (lcfg->lcfg_nid) {
1846                 char nidstr[LNET_NIDSTR_SIZE];
1847
1848                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1849                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
1850                                 nidstr, lcfg->lcfg_nid);
1851         }
1852
1853         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1854                 ptr += snprintf(ptr, end - ptr, ", device: %s",
1855                                 lustre_cfg_string(lcfg, 0));
1856
1857         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1858                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1859                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
1860                                         ldata->ltd_bufs[i - 1],
1861                                         lustre_cfg_string(lcfg, i));
1862         }
1863
1864         ptr += snprintf(ptr, end - ptr, " }\n");
1865         /* return consumed bytes */
1866         rc = ptr - buf;
1867         return rc;
1868 }
1869
1870 /**
1871  * parse config record and output dump in supplied buffer.
1872  * This is separated from class_config_dump_handler() to use
1873  * for ioctl needs as well
1874  */
1875 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1876 {
1877         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1878         char                    *ptr = buf;
1879         char                    *end = buf + size;
1880         int                      rc = 0;
1881
1882         ENTRY;
1883
1884         LASSERT(rec->lrh_type == OBD_CFG_REC);
1885         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1886         if (rc < 0)
1887                 RETURN(rc);
1888
1889         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1890         if (lcfg->lcfg_flags)
1891                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1892                                 lcfg->lcfg_flags);
1893
1894         if (lcfg->lcfg_num)
1895                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1896
1897         if (lcfg->lcfg_nid) {
1898                 char nidstr[LNET_NIDSTR_SIZE];
1899
1900                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1901                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n     ",
1902                                 nidstr, lcfg->lcfg_nid);
1903         }
1904
1905         if (lcfg->lcfg_command == LCFG_MARKER) {
1906                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1907
1908                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1909                                 marker->cm_step, marker->cm_flags,
1910                                 marker->cm_tgtname, marker->cm_comment);
1911         } else {
1912                 int i;
1913
1914                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1915                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1916                                         lustre_cfg_string(lcfg, i));
1917                 }
1918         }
1919         ptr += snprintf(ptr, end - ptr, "\n");
1920         /* return consumed bytes */
1921         rc = ptr - buf;
1922         RETURN(rc);
1923 }
1924
1925 int class_config_dump_handler(const struct lu_env *env,
1926                               struct llog_handle *handle,
1927                               struct llog_rec_hdr *rec, void *data)
1928 {
1929         char    *outstr;
1930         int      rc = 0;
1931
1932         ENTRY;
1933
1934         OBD_ALLOC(outstr, 256);
1935         if (outstr == NULL)
1936                 RETURN(-ENOMEM);
1937
1938         if (rec->lrh_type == OBD_CFG_REC) {
1939                 class_config_parse_rec(rec, outstr, 256);
1940                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1941         } else {
1942                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1943                 rc = -EINVAL;
1944         }
1945
1946         OBD_FREE(outstr, 256);
1947         RETURN(rc);
1948 }
1949
1950 /** Call class_cleanup and class_detach.
1951  * "Manual" only in the sense that we're faking lcfg commands.
1952  */
1953 int class_manual_cleanup(struct obd_device *obd)
1954 {
1955         char                    flags[3] = "";
1956         struct lustre_cfg      *lcfg;
1957         struct lustre_cfg_bufs  bufs;
1958         int                     rc;
1959         ENTRY;
1960
1961         if (!obd) {
1962                 CERROR("empty cleanup\n");
1963                 RETURN(-EALREADY);
1964         }
1965
1966         if (obd->obd_force)
1967                 strcat(flags, "F");
1968         if (obd->obd_fail)
1969                 strcat(flags, "A");
1970
1971         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1972                obd->obd_name, flags);
1973
1974         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1975         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1976         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1977         if (lcfg == NULL)
1978                 RETURN(-ENOMEM);
1979
1980         rc = class_process_config(lcfg);
1981         if (rc) {
1982                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1983                 GOTO(out, rc);
1984         }
1985
1986         /* the lcfg is almost the same for both ops */
1987         lcfg->lcfg_command = LCFG_DETACH;
1988         rc = class_process_config(lcfg);
1989         if (rc)
1990                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1991 out:
1992         lustre_cfg_free(lcfg);
1993         RETURN(rc);
1994 }
1995 EXPORT_SYMBOL(class_manual_cleanup);
1996
1997 /*
1998  * uuid<->export lustre hash operations
1999  */
2000
2001 static unsigned
2002 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2003 {
2004         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2005                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
2006 }
2007
2008 static void *
2009 uuid_key(struct hlist_node *hnode)
2010 {
2011         struct obd_export *exp;
2012
2013         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2014
2015         return &exp->exp_client_uuid;
2016 }
2017
2018 /*
2019  * NOTE: It is impossible to find an export that is in failed
2020  *       state with this function
2021  */
2022 static int
2023 uuid_keycmp(const void *key, struct hlist_node *hnode)
2024 {
2025         struct obd_export *exp;
2026
2027         LASSERT(key);
2028         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2029
2030         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2031                !exp->exp_failed;
2032 }
2033
2034 static void *
2035 uuid_export_object(struct hlist_node *hnode)
2036 {
2037         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2038 }
2039
2040 static void
2041 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2042 {
2043         struct obd_export *exp;
2044
2045         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2046         class_export_get(exp);
2047 }
2048
2049 static void
2050 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2051 {
2052         struct obd_export *exp;
2053
2054         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2055         class_export_put(exp);
2056 }
2057
2058 static struct cfs_hash_ops uuid_hash_ops = {
2059         .hs_hash        = uuid_hash,
2060         .hs_key         = uuid_key,
2061         .hs_keycmp      = uuid_keycmp,
2062         .hs_object      = uuid_export_object,
2063         .hs_get         = uuid_export_get,
2064         .hs_put_locked  = uuid_export_put_locked,
2065 };
2066
2067
2068 /*
2069  * nid<->export hash operations
2070  */
2071
2072 static unsigned
2073 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2074 {
2075         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2076 }
2077
2078 static void *
2079 nid_key(struct hlist_node *hnode)
2080 {
2081         struct obd_export *exp;
2082
2083         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2084
2085         RETURN(&exp->exp_connection->c_peer.nid);
2086 }
2087
2088 /*
2089  * NOTE: It is impossible to find an export that is in failed
2090  *       state with this function
2091  */
2092 static int
2093 nid_kepcmp(const void *key, struct hlist_node *hnode)
2094 {
2095         struct obd_export *exp;
2096
2097         LASSERT(key);
2098         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2099
2100         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2101                !exp->exp_failed);
2102 }
2103
2104 static void *
2105 nid_export_object(struct hlist_node *hnode)
2106 {
2107         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2108 }
2109
2110 static void
2111 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2112 {
2113         struct obd_export *exp;
2114
2115         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2116         class_export_get(exp);
2117 }
2118
2119 static void
2120 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2121 {
2122         struct obd_export *exp;
2123
2124         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2125         class_export_put(exp);
2126 }
2127
2128 static struct cfs_hash_ops nid_hash_ops = {
2129         .hs_hash        = nid_hash,
2130         .hs_key         = nid_key,
2131         .hs_keycmp      = nid_kepcmp,
2132         .hs_object      = nid_export_object,
2133         .hs_get         = nid_export_get,
2134         .hs_put_locked  = nid_export_put_locked,
2135 };
2136
2137
2138 /*
2139  * nid<->nidstats hash operations
2140  */
2141
2142 static void *
2143 nidstats_key(struct hlist_node *hnode)
2144 {
2145         struct nid_stat *ns;
2146
2147         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2148
2149         return &ns->nid;
2150 }
2151
2152 static int
2153 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2154 {
2155         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2156 }
2157
2158 static void *
2159 nidstats_object(struct hlist_node *hnode)
2160 {
2161         return hlist_entry(hnode, struct nid_stat, nid_hash);
2162 }
2163
2164 static void
2165 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2166 {
2167         struct nid_stat *ns;
2168
2169         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2170         nidstat_getref(ns);
2171 }
2172
2173 static void
2174 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2175 {
2176         struct nid_stat *ns;
2177
2178         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2179         nidstat_putref(ns);
2180 }
2181
2182 static struct cfs_hash_ops nid_stat_hash_ops = {
2183         .hs_hash        = nid_hash,
2184         .hs_key         = nidstats_key,
2185         .hs_keycmp      = nidstats_keycmp,
2186         .hs_object      = nidstats_object,
2187         .hs_get         = nidstats_get,
2188         .hs_put_locked  = nidstats_put_locked,
2189 };
2190
2191
2192 /*
2193  * client_generation<->export hash operations
2194  */
2195
2196 static unsigned
2197 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2198 {
2199         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2200 }
2201
2202 static void *
2203 gen_key(struct hlist_node *hnode)
2204 {
2205         struct obd_export *exp;
2206
2207         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2208
2209         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2210 }
2211
2212 /*
2213  * NOTE: It is impossible to find an export that is in failed
2214  *       state with this function
2215  */
2216 static int
2217 gen_kepcmp(const void *key, struct hlist_node *hnode)
2218 {
2219         struct obd_export *exp;
2220
2221         LASSERT(key);
2222         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2223
2224         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2225                !exp->exp_failed);
2226 }
2227
2228 static void *
2229 gen_export_object(struct hlist_node *hnode)
2230 {
2231         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2232 }
2233
2234 static void
2235 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2236 {
2237         struct obd_export *exp;
2238
2239         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2240         class_export_get(exp);
2241 }
2242
2243 static void
2244 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2245 {
2246         struct obd_export *exp;
2247
2248         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2249         class_export_put(exp);
2250 }
2251
2252 static struct cfs_hash_ops gen_hash_ops = {
2253         .hs_hash        = gen_hash,
2254         .hs_key         = gen_key,
2255         .hs_keycmp      = gen_kepcmp,
2256         .hs_object      = gen_export_object,
2257         .hs_get         = gen_export_get,
2258         .hs_put_locked  = gen_export_put_locked,
2259 };