Whamcloud - gitweb
LU-6401 uapi: turn lustre_param.h into a proper UAPI header
[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, 2016, 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 <uapi/linux/lustre_ioctl.h>
45 #include <lustre_log.h>
46 #include <uapi/linux/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         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 LIST_HEAD(lustre_profile_list);
837 static DEFINE_SPINLOCK(lustre_profile_list_lock);
838
839 struct lustre_profile *class_get_profile(const char * prof)
840 {
841         struct lustre_profile *lprof;
842
843         ENTRY;
844         spin_lock(&lustre_profile_list_lock);
845         list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
846                 if (!strcmp(lprof->lp_profile, prof)) {
847                         lprof->lp_refs++;
848                         spin_unlock(&lustre_profile_list_lock);
849                         RETURN(lprof);
850                 }
851         }
852         spin_unlock(&lustre_profile_list_lock);
853         RETURN(NULL);
854 }
855 EXPORT_SYMBOL(class_get_profile);
856
857 /** Create a named "profile".
858  * This defines the mdc and osc names to use for a client.
859  * This also is used to define the lov to be used by a mdt.
860  */
861 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
862                              int mdclen, char *mdc)
863 {
864         struct lustre_profile *lprof;
865         int err = 0;
866         ENTRY;
867
868         CDEBUG(D_CONFIG, "Add profile %s\n", prof);
869
870         OBD_ALLOC(lprof, sizeof(*lprof));
871         if (lprof == NULL)
872                 RETURN(-ENOMEM);
873         INIT_LIST_HEAD(&lprof->lp_list);
874
875         LASSERT(proflen == (strlen(prof) + 1));
876         OBD_ALLOC(lprof->lp_profile, proflen);
877         if (lprof->lp_profile == NULL)
878                 GOTO(out, err = -ENOMEM);
879         memcpy(lprof->lp_profile, prof, proflen);
880
881         LASSERT(osclen == (strlen(osc) + 1));
882         OBD_ALLOC(lprof->lp_dt, osclen);
883         if (lprof->lp_dt == NULL)
884                 GOTO(out, err = -ENOMEM);
885         memcpy(lprof->lp_dt, osc, osclen);
886
887         if (mdclen > 0) {
888                 LASSERT(mdclen == (strlen(mdc) + 1));
889                 OBD_ALLOC(lprof->lp_md, mdclen);
890                 if (lprof->lp_md == NULL)
891                         GOTO(out, err = -ENOMEM);
892                 memcpy(lprof->lp_md, mdc, mdclen);
893         }
894
895         spin_lock(&lustre_profile_list_lock);
896         lprof->lp_refs = 1;
897         lprof->lp_list_deleted = false;
898
899         list_add(&lprof->lp_list, &lustre_profile_list);
900         spin_unlock(&lustre_profile_list_lock);
901         RETURN(err);
902
903 out:
904         if (lprof->lp_md)
905                 OBD_FREE(lprof->lp_md, mdclen);
906         if (lprof->lp_dt)
907                 OBD_FREE(lprof->lp_dt, osclen);
908         if (lprof->lp_profile)
909                 OBD_FREE(lprof->lp_profile, proflen);
910         OBD_FREE(lprof, sizeof(*lprof));
911         RETURN(err);
912 }
913
914 void class_del_profile(const char *prof)
915 {
916         struct lustre_profile *lprof;
917         ENTRY;
918
919         CDEBUG(D_CONFIG, "Del profile %s\n", prof);
920
921         lprof = class_get_profile(prof);
922         if (lprof) {
923                 spin_lock(&lustre_profile_list_lock);
924                 /* because get profile increments the ref counter */
925                 lprof->lp_refs--;
926                 list_del(&lprof->lp_list);
927                 lprof->lp_list_deleted = true;
928                 spin_unlock(&lustre_profile_list_lock);
929
930                 class_put_profile(lprof);
931         }
932         EXIT;
933 }
934 EXPORT_SYMBOL(class_del_profile);
935
936 void class_put_profile(struct lustre_profile *lprof)
937 {
938         spin_lock(&lustre_profile_list_lock);
939         if ((--lprof->lp_refs) > 0) {
940                 LASSERT(lprof->lp_refs > 0);
941                 spin_unlock(&lustre_profile_list_lock);
942                 return;
943         }
944         spin_unlock(&lustre_profile_list_lock);
945
946         /* confirm not a negative number */
947         LASSERT(lprof->lp_refs == 0);
948
949         /* At least one class_del_profile/profiles must be called
950          * on the target profile or lustre_profile_list will corrupt */
951         LASSERT(lprof->lp_list_deleted);
952         OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
953         OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
954         if (lprof->lp_md != NULL)
955                 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
956         OBD_FREE(lprof, sizeof(*lprof));
957 }
958 EXPORT_SYMBOL(class_put_profile);
959
960 /* COMPAT_146 */
961 void class_del_profiles(void)
962 {
963         struct lustre_profile *lprof, *n;
964         ENTRY;
965
966         spin_lock(&lustre_profile_list_lock);
967         list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
968                 list_del(&lprof->lp_list);
969                 lprof->lp_list_deleted = true;
970                 spin_unlock(&lustre_profile_list_lock);
971
972                 class_put_profile(lprof);
973
974                 spin_lock(&lustre_profile_list_lock);
975         }
976         spin_unlock(&lustre_profile_list_lock);
977         EXIT;
978 }
979 EXPORT_SYMBOL(class_del_profiles);
980
981 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
982 {
983         ENTRY;
984         if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
985                 at_min = val;
986         else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
987                 at_max = val;
988         else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
989                 at_extra = val;
990         else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
991                 at_early_margin = val;
992         else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
993                 at_history = val;
994         else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
995                 strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
996                         JOBSTATS_JOBID_VAR_MAX_LEN + 1);
997         else
998                 RETURN(-EINVAL);
999
1000         CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
1001         RETURN(0);
1002 }
1003
1004
1005 /* We can't call ll_process_config or lquota_process_config directly because
1006  * it lives in a module that must be loaded after this one. */
1007 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
1008 static int (*quota_process_config)(struct lustre_cfg *lcfg) = NULL;
1009
1010 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
1011 {
1012         client_process_config = cpc;
1013 }
1014 EXPORT_SYMBOL(lustre_register_client_process_config);
1015
1016 /**
1017  * Rename the proc parameter in \a cfg with a new name \a new_name.
1018  *
1019  * \param cfg      config structure which contains the proc parameter
1020  * \param new_name new name of the proc parameter
1021  *
1022  * \retval valid-pointer    pointer to the newly-allocated config structure
1023  *                          which contains the renamed proc parameter
1024  * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
1025  *                          not contain a proc parameter
1026  * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
1027  */
1028 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
1029                                      const char *new_name)
1030 {
1031         struct lustre_cfg_bufs  *bufs = NULL;
1032         struct lustre_cfg       *new_cfg = NULL;
1033         char                    *param = NULL;
1034         char                    *new_param = NULL;
1035         char                    *value = NULL;
1036         int                      name_len = 0;
1037         int                      new_len = 0;
1038         ENTRY;
1039
1040         if (cfg == NULL || new_name == NULL)
1041                 RETURN(ERR_PTR(-EINVAL));
1042
1043         param = lustre_cfg_string(cfg, 1);
1044         if (param == NULL)
1045                 RETURN(ERR_PTR(-EINVAL));
1046
1047         value = strchr(param, '=');
1048         if (value == NULL)
1049                 name_len = strlen(param);
1050         else
1051                 name_len = value - param;
1052
1053         new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
1054
1055         OBD_ALLOC(new_param, new_len);
1056         if (new_param == NULL)
1057                 RETURN(ERR_PTR(-ENOMEM));
1058
1059         strcpy(new_param, new_name);
1060         if (value != NULL)
1061                 strcat(new_param, value);
1062
1063         OBD_ALLOC_PTR(bufs);
1064         if (bufs == NULL) {
1065                 OBD_FREE(new_param, new_len);
1066                 RETURN(ERR_PTR(-ENOMEM));
1067         }
1068
1069         lustre_cfg_bufs_reset(bufs, NULL);
1070         lustre_cfg_bufs_init(bufs, cfg);
1071         lustre_cfg_bufs_set_string(bufs, 1, new_param);
1072
1073         new_cfg = lustre_cfg_new(cfg->lcfg_command, bufs);
1074         OBD_FREE(new_param, new_len);
1075         OBD_FREE_PTR(bufs);
1076         if (new_cfg == NULL)
1077                 RETURN(ERR_PTR(-ENOMEM));
1078
1079         new_cfg->lcfg_num = cfg->lcfg_num;
1080         new_cfg->lcfg_flags = cfg->lcfg_flags;
1081         new_cfg->lcfg_nid = cfg->lcfg_nid;
1082         new_cfg->lcfg_nal = cfg->lcfg_nal;
1083
1084         RETURN(new_cfg);
1085 }
1086 EXPORT_SYMBOL(lustre_cfg_rename);
1087
1088 static int process_param2_config(struct lustre_cfg *lcfg)
1089 {
1090         char *param = lustre_cfg_string(lcfg, 1);
1091         char *upcall = lustre_cfg_string(lcfg, 2);
1092         char *argv[] = {
1093                 [0] = "/usr/sbin/lctl",
1094                 [1] = "set_param",
1095                 [2] = param,
1096                 [3] = NULL
1097         };
1098         ktime_t start;
1099         ktime_t end;
1100         int             rc;
1101         ENTRY;
1102
1103         /* Add upcall processing here. Now only lctl is supported */
1104         if (strcmp(upcall, LCTL_UPCALL) != 0) {
1105                 CERROR("Unsupported upcall %s\n", upcall);
1106                 RETURN(-EINVAL);
1107         }
1108
1109         start = ktime_get();
1110         rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1111         end = ktime_get();
1112
1113         if (rc < 0) {
1114                 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1115                        "time %ldus\n", argv[0], argv[1], argv[2], rc,
1116                        (long)ktime_us_delta(end, start));
1117         } else {
1118                 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1119                        argv[0], argv[1], argv[2],
1120                        (long)ktime_us_delta(end, start));
1121                        rc = 0;
1122         }
1123
1124         RETURN(rc);
1125 }
1126
1127 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1128 {
1129         quota_process_config = qpc;
1130 }
1131 EXPORT_SYMBOL(lustre_register_quota_process_config);
1132
1133 /** Process configuration commands given in lustre_cfg form.
1134  * These may come from direct calls (e.g. class_manual_cleanup)
1135  * or processing the config llog, or ioctl from lctl.
1136  */
1137 int class_process_config(struct lustre_cfg *lcfg)
1138 {
1139         struct obd_device *obd;
1140         int err;
1141
1142         LASSERT(lcfg && !IS_ERR(lcfg));
1143         CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1144
1145         /* Commands that don't need a device */
1146         switch(lcfg->lcfg_command) {
1147         case LCFG_ATTACH: {
1148                 err = class_attach(lcfg);
1149                 GOTO(out, err);
1150         }
1151         case LCFG_ADD_UUID: {
1152                 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx"
1153                        " (%s)\n", lustre_cfg_string(lcfg, 1),
1154                        lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1155
1156                 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1157                 GOTO(out, err);
1158         }
1159         case LCFG_DEL_UUID: {
1160                 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1161                        (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1162                        ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1163
1164                 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1165                 GOTO(out, err);
1166         }
1167         case LCFG_MOUNTOPT: {
1168                 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1169                        lustre_cfg_string(lcfg, 1),
1170                        lustre_cfg_string(lcfg, 2),
1171                        lustre_cfg_string(lcfg, 3));
1172                 /* set these mount options somewhere, so ll_fill_super
1173                  * can find them. */
1174                 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1175                                         lustre_cfg_string(lcfg, 1),
1176                                         LUSTRE_CFG_BUFLEN(lcfg, 2),
1177                                         lustre_cfg_string(lcfg, 2),
1178                                         LUSTRE_CFG_BUFLEN(lcfg, 3),
1179                                         lustre_cfg_string(lcfg, 3));
1180                 GOTO(out, err);
1181         }
1182         case LCFG_DEL_MOUNTOPT: {
1183                 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1184                        lustre_cfg_string(lcfg, 1));
1185                 class_del_profile(lustre_cfg_string(lcfg, 1));
1186                 GOTO(out, err = 0);
1187         }
1188         case LCFG_SET_TIMEOUT: {
1189                 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1190                        obd_timeout, lcfg->lcfg_num);
1191                 obd_timeout = max(lcfg->lcfg_num, 1U);
1192                 obd_timeout_set = 1;
1193                 GOTO(out, err = 0);
1194         }
1195         case LCFG_SET_LDLM_TIMEOUT: {
1196                 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1197                        ldlm_timeout, lcfg->lcfg_num);
1198                 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1199                 if (ldlm_timeout >= obd_timeout)
1200                         ldlm_timeout = max(obd_timeout / 3, 1U);
1201                 ldlm_timeout_set = 1;
1202                 GOTO(out, err = 0);
1203         }
1204         case LCFG_SET_UPCALL: {
1205                 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1206                 /* COMPAT_146 Don't fail on old configs */
1207                 GOTO(out, err = 0);
1208         }
1209         case LCFG_MARKER: {
1210                 struct cfg_marker *marker;
1211                 marker = lustre_cfg_buf(lcfg, 1);
1212                 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1213                        marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1214                 GOTO(out, err = 0);
1215         }
1216         case LCFG_PARAM: {
1217                 char *tmp;
1218                 /* llite has no obd */
1219                 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1220                                        PARAM_LLITE, NULL) == 0) &&
1221                     client_process_config) {
1222                         err = (*client_process_config)(lcfg);
1223                         GOTO(out, err);
1224                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1225                                               PARAM_SYS, &tmp) == 0)) {
1226                         /* Global param settings */
1227                         err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1228                         /*
1229                          * Client or server should not fail to mount if
1230                          * it hits an unknown configuration parameter.
1231                          */
1232                         if (err != 0)
1233                                 CWARN("Ignoring unknown param %s\n", tmp);
1234
1235                         GOTO(out, err = 0);
1236                 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1237                                               PARAM_QUOTA, &tmp) == 0) &&
1238                            quota_process_config) {
1239                         err = (*quota_process_config)(lcfg);
1240                         GOTO(out, err);
1241                 }
1242
1243                 break;
1244         }
1245         case LCFG_SET_PARAM: {
1246                 err = process_param2_config(lcfg);
1247                 GOTO(out, err = 0);
1248         }
1249         }
1250         /* Commands that require a device */
1251         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1252         if (obd == NULL) {
1253                 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1254                         CERROR("this lcfg command requires a device name\n");
1255                 else
1256                         CERROR("no device for: %s\n",
1257                                lustre_cfg_string(lcfg, 0));
1258
1259                 GOTO(out, err = -EINVAL);
1260         }
1261
1262         switch(lcfg->lcfg_command) {
1263         case LCFG_SETUP: {
1264                 err = class_setup(obd, lcfg);
1265                 GOTO(out, err);
1266         }
1267         case LCFG_DETACH: {
1268                 err = class_detach(obd, lcfg);
1269                 GOTO(out, err = 0);
1270         }
1271         case LCFG_CLEANUP: {
1272                 err = class_cleanup(obd, lcfg);
1273                 GOTO(out, err = 0);
1274         }
1275         case LCFG_ADD_CONN: {
1276                 err = class_add_conn(obd, lcfg);
1277                 GOTO(out, err = 0);
1278         }
1279         case LCFG_DEL_CONN: {
1280                 err = class_del_conn(obd, lcfg);
1281                 GOTO(out, err = 0);
1282         }
1283         case LCFG_POOL_NEW: {
1284                 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1285                 GOTO(out, err = 0);
1286         }
1287         case LCFG_POOL_ADD: {
1288                 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1289                                    lustre_cfg_string(lcfg, 3));
1290                 GOTO(out, err = 0);
1291         }
1292         case LCFG_POOL_REM: {
1293                 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1294                                    lustre_cfg_string(lcfg, 3));
1295                 GOTO(out, err = 0);
1296         }
1297         case LCFG_POOL_DEL: {
1298                 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1299                 GOTO(out, err = 0);
1300         }
1301         default: {
1302                 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1303                 GOTO(out, err);
1304
1305         }
1306         }
1307 out:
1308         if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1309                 CWARN("Ignoring error %d on optional command %#x\n", err,
1310                       lcfg->lcfg_command);
1311                 err = 0;
1312         }
1313         return err;
1314 }
1315 EXPORT_SYMBOL(class_process_config);
1316
1317 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1318                              struct lustre_cfg *lcfg, void *data)
1319 {
1320         struct lprocfs_vars *var;
1321         struct file fakefile;
1322         struct seq_file fake_seqfile;
1323         char *key, *sval;
1324         int i, keylen, vallen;
1325         int matched = 0, j = 0;
1326         int rc = 0;
1327         int skip = 0;
1328         ENTRY;
1329
1330         if (lcfg->lcfg_command != LCFG_PARAM) {
1331                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1332                 RETURN(-EINVAL);
1333         }
1334
1335         /* fake a seq file so that var->fops->write can work... */
1336         fakefile.private_data = &fake_seqfile;
1337         fake_seqfile.private = data;
1338         /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1339            or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1340            or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1341         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1342                 key = lustre_cfg_buf(lcfg, i);
1343                 /* Strip off prefix */
1344                 if (class_match_param(key, prefix, &key))
1345                         /* If the prefix doesn't match, return error so we
1346                          * can pass it down the stack */
1347                         RETURN(-ENOSYS);
1348                 sval = strchr(key, '=');
1349                 if (!sval || (*(sval + 1) == 0)) {
1350                         CERROR("Can't parse param %s (missing '=')\n", key);
1351                         /* rc = -EINVAL;        continue parsing other params */
1352                         continue;
1353                 }
1354                 keylen = sval - key;
1355                 sval++;
1356                 vallen = strlen(sval);
1357                 matched = 0;
1358                 j = 0;
1359                 /* Search proc entries */
1360                 while (lvars[j].name) {
1361                         var = &lvars[j];
1362                         if (class_match_param(key, var->name, NULL) == 0 &&
1363                             keylen == strlen(var->name)) {
1364                                 matched++;
1365                                 rc = -EROFS;
1366
1367                                 if (var->fops && var->fops->write) {
1368                                         mm_segment_t oldfs;
1369                                         oldfs = get_fs();
1370                                         set_fs(KERNEL_DS);
1371                                         rc = (var->fops->write)(&fakefile, sval,
1372                                                                 vallen, NULL);
1373                                         set_fs(oldfs);
1374                                 }
1375                                 break;
1376                         }
1377                         j++;
1378                 }
1379                 if (!matched) {
1380                         /* It was upgraded from old MDT/OST device,
1381                          * ignore the obsolete "sec_level" parameter. */
1382                         if (strncmp("sec_level", key, keylen) == 0)
1383                                 continue;
1384
1385                         CERROR("%.*s: %s unknown param %s\n",
1386                                (int)strlen(prefix) - 1, prefix,
1387                                (char *)lustre_cfg_string(lcfg, 0), key);
1388                         /* rc = -EINVAL;        continue parsing other params */
1389                         skip++;
1390                 } else if (rc < 0) {
1391                         CERROR("%s: error writing proc entry '%s': rc = %d\n",
1392                                prefix, var->name, rc);
1393                         rc = 0;
1394                 } else {
1395                         CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1396                                          lustre_cfg_string(lcfg, 0),
1397                                          (int)strlen(prefix) - 1, prefix,
1398                                          (int)(sval - key - 1), key, sval);
1399                 }
1400         }
1401
1402         if (rc > 0)
1403                 rc = 0;
1404         if (!rc && skip)
1405                 rc = skip;
1406         RETURN(rc);
1407 }
1408 EXPORT_SYMBOL(class_process_proc_param);
1409
1410 /*
1411  * Supplemental functions for config logs, it allocates lustre_cfg
1412  * buffers plus initialized llog record header at the beginning.
1413  */
1414 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1415 {
1416         struct llog_cfg_rec     *lcr;
1417         int                      reclen;
1418
1419         ENTRY;
1420
1421         reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1422         reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1423                  sizeof(struct llog_rec_tail);
1424
1425         OBD_ALLOC(lcr, reclen);
1426         if (lcr == NULL)
1427                 RETURN(NULL);
1428
1429         lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1430
1431         lcr->lcr_hdr.lrh_len = reclen;
1432         lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1433
1434         RETURN(lcr);
1435 }
1436 EXPORT_SYMBOL(lustre_cfg_rec_new);
1437
1438 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1439 {
1440         ENTRY;
1441         OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1442         EXIT;
1443 }
1444 EXPORT_SYMBOL(lustre_cfg_rec_free);
1445
1446 /** Parse a configuration llog, doing various manipulations on them
1447  * for various reasons, (modifications for compatibility, skip obsolete
1448  * records, change uuids, etc), then class_process_config() resulting
1449  * net records.
1450  */
1451 int class_config_llog_handler(const struct lu_env *env,
1452                               struct llog_handle *handle,
1453                               struct llog_rec_hdr *rec, void *data)
1454 {
1455         struct config_llog_instance *cfg = data;
1456         int cfg_len = rec->lrh_len;
1457         char *cfg_buf = (char *) (rec + 1);
1458         int rc = 0;
1459         ENTRY;
1460
1461         /* class_config_dump_handler(handle, rec, data); */
1462
1463         switch (rec->lrh_type) {
1464         case OBD_CFG_REC: {
1465                 struct lustre_cfg *lcfg, *lcfg_new;
1466                 struct lustre_cfg_bufs bufs;
1467                 char *inst_name = NULL;
1468                 int inst_len = 0;
1469                 int inst = 0, swab = 0;
1470
1471                 lcfg = (struct lustre_cfg *)cfg_buf;
1472                 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1473                         lustre_swab_lustre_cfg(lcfg);
1474                         swab = 1;
1475                 }
1476
1477                 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1478                 if (rc)
1479                         GOTO(out, rc);
1480
1481                 /* Figure out config state info */
1482                 if (lcfg->lcfg_command == LCFG_MARKER) {
1483                         struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1484                         lustre_swab_cfg_marker(marker, swab,
1485                                                LUSTRE_CFG_BUFLEN(lcfg, 1));
1486                         CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1487                                cfg->cfg_flags, marker->cm_flags);
1488                         if (marker->cm_flags & CM_START) {
1489                                 /* all previous flags off */
1490                                 cfg->cfg_flags = CFG_F_MARKER;
1491                                 server_name2index(marker->cm_tgtname,
1492                                                   &cfg->cfg_lwp_idx, NULL);
1493                                 if (marker->cm_flags & CM_SKIP) {
1494                                         cfg->cfg_flags |= CFG_F_SKIP;
1495                                         CDEBUG(D_CONFIG, "SKIP #%d\n",
1496                                                marker->cm_step);
1497                                 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1498                                            (cfg->cfg_sb &&
1499                                            lustre_check_exclusion(cfg->cfg_sb,
1500                                                         marker->cm_tgtname))) {
1501                                         cfg->cfg_flags |= CFG_F_EXCLUDE;
1502                                         CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1503                                                marker->cm_step);
1504                                 }
1505                         } else if (marker->cm_flags & CM_END) {
1506                                 cfg->cfg_flags = 0;
1507                         }
1508                 }
1509                 /* A config command without a start marker before it is
1510                    illegal (post 146) */
1511                 if (!(cfg->cfg_flags & CFG_F_COMPAT146) &&
1512                     !(cfg->cfg_flags & CFG_F_MARKER) &&
1513                     (lcfg->lcfg_command != LCFG_MARKER)) {
1514                         CWARN("Config not inside markers, ignoring! "
1515                               "(inst: %p, uuid: %s, flags: %#x)\n",
1516                                 cfg->cfg_instance,
1517                                 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1518                         cfg->cfg_flags |= CFG_F_SKIP;
1519                 }
1520                 if (cfg->cfg_flags & CFG_F_SKIP) {
1521                         CDEBUG(D_CONFIG, "skipping %#x\n",
1522                                cfg->cfg_flags);
1523                         rc = 0;
1524                         /* No processing! */
1525                         break;
1526                 }
1527
1528                 /*
1529                  * For interoperability between 1.8 and 2.0,
1530                  * rename "mds" obd device type to "mdt".
1531                  */
1532                 {
1533                         char *typename = lustre_cfg_string(lcfg, 1);
1534                         char *index = lustre_cfg_string(lcfg, 2);
1535
1536                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1537                             strcmp(typename, "mds") == 0)) {
1538                                 CWARN("For 1.8 interoperability, rename obd "
1539                                         "type from mds to mdt\n");
1540                                 typename[2] = 't';
1541                         }
1542                         if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1543                             strcmp(index, "type") == 0)) {
1544                                 CDEBUG(D_INFO, "For 1.8 interoperability, "
1545                                        "set this index to '0'\n");
1546                                 index[0] = '0';
1547                                 index[1] = 0;
1548                         }
1549                 }
1550
1551 #ifdef HAVE_SERVER_SUPPORT
1552                 /* newer MDS replaces LOV/OSC with LOD/OSP */
1553                 {
1554                         char *typename = lustre_cfg_string(lcfg, 1);
1555
1556                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1557                             strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1558                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1559                                 CDEBUG(D_CONFIG,
1560                                        "For 2.x interoperability, rename obd "
1561                                        "type from lov to lod (%s)\n",
1562                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1563                                 strcpy(typename, LUSTRE_LOD_NAME);
1564                         }
1565                         if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1566                             strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1567                             cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1568                                 CDEBUG(D_CONFIG,
1569                                        "For 2.x interoperability, rename obd "
1570                                        "type from osc to osp (%s)\n",
1571                                        s2lsi(cfg->cfg_sb)->lsi_svname);
1572                                 strcpy(typename, LUSTRE_OSP_NAME);
1573                         }
1574                 }
1575 #endif /* HAVE_SERVER_SUPPORT */
1576
1577                 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1578                         CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1579                                lcfg->lcfg_command);
1580                         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1581                                 /* Add inactive instead */
1582                                 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1583                 }
1584
1585                 lustre_cfg_bufs_reset(&bufs, NULL);
1586                 lustre_cfg_bufs_init(&bufs, lcfg);
1587
1588                 if (cfg->cfg_instance &&
1589                     LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1590                         inst = 1;
1591                         inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1592                                    sizeof(cfg->cfg_instance) * 2 + 4;
1593                         OBD_ALLOC(inst_name, inst_len);
1594                         if (inst_name == NULL)
1595                                 GOTO(out, rc = -ENOMEM);
1596                         snprintf(inst_name, inst_len, "%s-%p",
1597                                 lustre_cfg_string(lcfg, 0),
1598                                 cfg->cfg_instance);
1599                         lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1600                         CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1601                                lcfg->lcfg_command, inst_name);
1602                 }
1603
1604                 /* we override the llog's uuid for clients, to insure they
1605                 are unique */
1606                 if (cfg->cfg_instance != NULL &&
1607                     lcfg->lcfg_command == LCFG_ATTACH) {
1608                         lustre_cfg_bufs_set_string(&bufs, 2,
1609                                                    cfg->cfg_uuid.uuid);
1610                 }
1611                 /*
1612                  * sptlrpc config record, we expect 2 data segments:
1613                  *  [0]: fs_name/target_name,
1614                  *  [1]: rule string
1615                  * moving them to index [1] and [2], and insert MGC's
1616                  * obdname at index [0].
1617                  */
1618                 if (cfg->cfg_instance != NULL &&
1619                     lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1620                         lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1621                                             bufs.lcfg_buflen[1]);
1622                         lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1623                                             bufs.lcfg_buflen[0]);
1624                         lustre_cfg_bufs_set_string(&bufs, 0,
1625                                                    cfg->cfg_obdname);
1626                 }
1627
1628                 /* Add net info to setup command
1629                  * if given on command line.
1630                  * So config log will be:
1631                  * [0]: client name
1632                  * [1]: client UUID
1633                  * [2]: server UUID
1634                  * [3]: inactive-on-startup
1635                  * [4]: restrictive net
1636                  */
1637                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1638                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1639                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1640                         char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1641
1642                         if (lcfg->lcfg_command == LCFG_SETUP &&
1643                             lcfg->lcfg_bufcount != 2 && nidnet) {
1644                                 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1645                                        "command for client %s\n", nidnet,
1646                                        lustre_cfg_string(lcfg, 0));
1647                                 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1648                         }
1649                 }
1650
1651                 /* Skip add_conn command if uuid is
1652                  * not on restricted net */
1653                 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1654                     !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1655                         struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1656                         char *uuid_str = lustre_cfg_string(lcfg, 1);
1657
1658                         if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1659                             lsi->lsi_lmd->lmd_nidnet &&
1660                             LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1661                             libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1662                                 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1663                                        uuid_str);
1664                                 rc = 0;
1665                                 /* No processing! */
1666                                 break;
1667                         }
1668                 }
1669
1670                 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1671                 if (lcfg_new == NULL)
1672                         GOTO(out, rc = -ENOMEM);
1673
1674                 lcfg_new->lcfg_num   = lcfg->lcfg_num;
1675                 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1676
1677                 /* XXX Hack to try to remain binary compatible with
1678                  * pre-newconfig logs */
1679                 if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1680                     (lcfg->lcfg_nid >> 32) == 0) {
1681                         __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1682
1683                         lcfg_new->lcfg_nid =
1684                                 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1685                         CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1686                               lcfg->lcfg_nal, addr,
1687                               libcfs_nid2str(lcfg_new->lcfg_nid));
1688                 } else {
1689                         lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1690                 }
1691
1692                 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1693
1694                 rc = class_process_config(lcfg_new);
1695                 lustre_cfg_free(lcfg_new);
1696
1697                 if (inst)
1698                         OBD_FREE(inst_name, inst_len);
1699                 break;
1700         }
1701         default:
1702                 CERROR("Unknown llog record type %#x encountered\n",
1703                        rec->lrh_type);
1704                 break;
1705         }
1706 out:
1707         if (rc) {
1708                 CERROR("%s: cfg command failed: rc = %d\n",
1709                         handle->lgh_ctxt->loc_obd->obd_name, rc);
1710                 class_config_dump_handler(NULL, handle, rec, data);
1711         }
1712         RETURN(rc);
1713 }
1714 EXPORT_SYMBOL(class_config_llog_handler);
1715
1716 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1717                             char *name, struct config_llog_instance *cfg)
1718 {
1719         struct llog_process_cat_data cd = {
1720                 .lpcd_first_idx = 0,
1721         };
1722         struct llog_handle *llh;
1723         llog_cb_t callback;
1724         int rc;
1725         ENTRY;
1726
1727         CDEBUG(D_INFO, "looking up llog %s\n", name);
1728         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1729         if (rc)
1730                 RETURN(rc);
1731
1732         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1733         if (rc)
1734                 GOTO(parse_out, rc);
1735
1736         /* continue processing from where we last stopped to end-of-log */
1737         if (cfg) {
1738                 cd.lpcd_first_idx = cfg->cfg_last_idx;
1739                 callback = cfg->cfg_callback;
1740                 LASSERT(callback != NULL);
1741         } else {
1742                 callback = class_config_llog_handler;
1743         }
1744
1745         cd.lpcd_last_idx = 0;
1746
1747         rc = llog_process(env, llh, callback, cfg, &cd);
1748
1749         CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1750                cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1751         if (cfg)
1752                 cfg->cfg_last_idx = cd.lpcd_last_idx;
1753
1754 parse_out:
1755         llog_close(env, llh);
1756         RETURN(rc);
1757 }
1758 EXPORT_SYMBOL(class_config_parse_llog);
1759
1760 static struct lcfg_type_data {
1761         __u32    ltd_type;
1762         char    *ltd_name;
1763         char    *ltd_bufs[4];
1764 } lcfg_data_table[] = {
1765         { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1766         { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1767         { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1768         { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1769         { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" }  },
1770         { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" }  },
1771         { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" }  },
1772         { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1773         { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" }  },
1774         { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" }  },
1775         { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" }  },
1776         { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" }  },
1777         { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1778         { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1779         { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1780         { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1781         { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1782         { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1783         { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" }  },
1784         { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1785         { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1786         { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1787         { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" }  },
1788         { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1789         { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1790         { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1791         { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1792           { "parameter", "2", "3", "4" } },
1793         { 0, NULL, { NULL, NULL, NULL, NULL } }
1794 };
1795
1796 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1797 {
1798         int i = 0;
1799
1800         while (lcfg_data_table[i].ltd_type != 0) {
1801                 if (lcfg_data_table[i].ltd_type == cmd)
1802                         return &lcfg_data_table[i];
1803                 i++;
1804         }
1805         return NULL;
1806 }
1807
1808 /**
1809  * Parse config record and output dump in supplied buffer.
1810  *
1811  * This is separated from class_config_dump_handler() to use
1812  * for ioctl needs as well
1813  *
1814  * Sample Output:
1815  * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
1816  *     UUID: lustrewt-clilov_UUID }
1817  */
1818 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1819 {
1820         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1821         char                    *ptr = buf;
1822         char                    *end = buf + size;
1823         int                      rc = 0, i;
1824         struct lcfg_type_data   *ldata;
1825
1826         LASSERT(rec->lrh_type == OBD_CFG_REC);
1827         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1828         if (rc < 0)
1829                 return rc;
1830
1831         ldata = lcfg_cmd2data(lcfg->lcfg_command);
1832         if (ldata == NULL)
1833                 return -ENOTTY;
1834
1835         if (lcfg->lcfg_command == LCFG_MARKER)
1836                 return 0;
1837
1838         /* form YAML entity */
1839         ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
1840                         rec->lrh_index, ldata->ltd_name);
1841
1842         if (lcfg->lcfg_flags)
1843                 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1844                                 lcfg->lcfg_flags);
1845         if (lcfg->lcfg_num)
1846                 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1847                                 lcfg->lcfg_num);
1848         if (lcfg->lcfg_nid) {
1849                 char nidstr[LNET_NIDSTR_SIZE];
1850
1851                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1852                 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
1853                                 nidstr, lcfg->lcfg_nid);
1854         }
1855
1856         if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1857                 ptr += snprintf(ptr, end - ptr, ", device: %s",
1858                                 lustre_cfg_string(lcfg, 0));
1859
1860         for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1861                 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1862                         ptr += snprintf(ptr, end - ptr, ", %s: %s",
1863                                         ldata->ltd_bufs[i - 1],
1864                                         lustre_cfg_string(lcfg, i));
1865         }
1866
1867         ptr += snprintf(ptr, end - ptr, " }\n");
1868         /* return consumed bytes */
1869         rc = ptr - buf;
1870         return rc;
1871 }
1872
1873 /**
1874  * parse config record and output dump in supplied buffer.
1875  * This is separated from class_config_dump_handler() to use
1876  * for ioctl needs as well
1877  */
1878 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1879 {
1880         struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1881         char                    *ptr = buf;
1882         char                    *end = buf + size;
1883         int                      rc = 0;
1884
1885         ENTRY;
1886
1887         LASSERT(rec->lrh_type == OBD_CFG_REC);
1888         rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1889         if (rc < 0)
1890                 RETURN(rc);
1891
1892         ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1893         if (lcfg->lcfg_flags)
1894                 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1895                                 lcfg->lcfg_flags);
1896
1897         if (lcfg->lcfg_num)
1898                 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1899
1900         if (lcfg->lcfg_nid) {
1901                 char nidstr[LNET_NIDSTR_SIZE];
1902
1903                 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1904                 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n     ",
1905                                 nidstr, lcfg->lcfg_nid);
1906         }
1907
1908         if (lcfg->lcfg_command == LCFG_MARKER) {
1909                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1910
1911                 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1912                                 marker->cm_step, marker->cm_flags,
1913                                 marker->cm_tgtname, marker->cm_comment);
1914         } else {
1915                 int i;
1916
1917                 for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1918                         ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1919                                         lustre_cfg_string(lcfg, i));
1920                 }
1921         }
1922         ptr += snprintf(ptr, end - ptr, "\n");
1923         /* return consumed bytes */
1924         rc = ptr - buf;
1925         RETURN(rc);
1926 }
1927
1928 int class_config_dump_handler(const struct lu_env *env,
1929                               struct llog_handle *handle,
1930                               struct llog_rec_hdr *rec, void *data)
1931 {
1932         char    *outstr;
1933         int      rc = 0;
1934
1935         ENTRY;
1936
1937         OBD_ALLOC(outstr, 256);
1938         if (outstr == NULL)
1939                 RETURN(-ENOMEM);
1940
1941         if (rec->lrh_type == OBD_CFG_REC) {
1942                 class_config_parse_rec(rec, outstr, 256);
1943                 LCONSOLE(D_WARNING, "   %s\n", outstr);
1944         } else {
1945                 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1946                 rc = -EINVAL;
1947         }
1948
1949         OBD_FREE(outstr, 256);
1950         RETURN(rc);
1951 }
1952
1953 /** Call class_cleanup and class_detach.
1954  * "Manual" only in the sense that we're faking lcfg commands.
1955  */
1956 int class_manual_cleanup(struct obd_device *obd)
1957 {
1958         char                    flags[3] = "";
1959         struct lustre_cfg      *lcfg;
1960         struct lustre_cfg_bufs  bufs;
1961         int                     rc;
1962         ENTRY;
1963
1964         if (!obd) {
1965                 CERROR("empty cleanup\n");
1966                 RETURN(-EALREADY);
1967         }
1968
1969         if (obd->obd_force)
1970                 strcat(flags, "F");
1971         if (obd->obd_fail)
1972                 strcat(flags, "A");
1973
1974         CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1975                obd->obd_name, flags);
1976
1977         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1978         lustre_cfg_bufs_set_string(&bufs, 1, flags);
1979         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1980         if (lcfg == NULL)
1981                 RETURN(-ENOMEM);
1982
1983         rc = class_process_config(lcfg);
1984         if (rc) {
1985                 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1986                 GOTO(out, rc);
1987         }
1988
1989         /* the lcfg is almost the same for both ops */
1990         lcfg->lcfg_command = LCFG_DETACH;
1991         rc = class_process_config(lcfg);
1992         if (rc)
1993                 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1994 out:
1995         lustre_cfg_free(lcfg);
1996         RETURN(rc);
1997 }
1998 EXPORT_SYMBOL(class_manual_cleanup);
1999
2000 /*
2001  * uuid<->export lustre hash operations
2002  */
2003
2004 static unsigned
2005 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2006 {
2007         return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2008                                   sizeof(((struct obd_uuid *)key)->uuid), mask);
2009 }
2010
2011 static void *
2012 uuid_key(struct hlist_node *hnode)
2013 {
2014         struct obd_export *exp;
2015
2016         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2017
2018         return &exp->exp_client_uuid;
2019 }
2020
2021 /*
2022  * NOTE: It is impossible to find an export that is in failed
2023  *       state with this function
2024  */
2025 static int
2026 uuid_keycmp(const void *key, struct hlist_node *hnode)
2027 {
2028         struct obd_export *exp;
2029
2030         LASSERT(key);
2031         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2032
2033         return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2034                !exp->exp_failed;
2035 }
2036
2037 static void *
2038 uuid_export_object(struct hlist_node *hnode)
2039 {
2040         return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2041 }
2042
2043 static void
2044 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2045 {
2046         struct obd_export *exp;
2047
2048         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2049         class_export_get(exp);
2050 }
2051
2052 static void
2053 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2054 {
2055         struct obd_export *exp;
2056
2057         exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2058         class_export_put(exp);
2059 }
2060
2061 static struct cfs_hash_ops uuid_hash_ops = {
2062         .hs_hash        = uuid_hash,
2063         .hs_key         = uuid_key,
2064         .hs_keycmp      = uuid_keycmp,
2065         .hs_object      = uuid_export_object,
2066         .hs_get         = uuid_export_get,
2067         .hs_put_locked  = uuid_export_put_locked,
2068 };
2069
2070
2071 /*
2072  * nid<->export hash operations
2073  */
2074
2075 static unsigned
2076 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2077 {
2078         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2079 }
2080
2081 static void *
2082 nid_key(struct hlist_node *hnode)
2083 {
2084         struct obd_export *exp;
2085
2086         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2087
2088         RETURN(&exp->exp_connection->c_peer.nid);
2089 }
2090
2091 /*
2092  * NOTE: It is impossible to find an export that is in failed
2093  *       state with this function
2094  */
2095 static int
2096 nid_kepcmp(const void *key, struct hlist_node *hnode)
2097 {
2098         struct obd_export *exp;
2099
2100         LASSERT(key);
2101         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2102
2103         RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2104                !exp->exp_failed);
2105 }
2106
2107 static void *
2108 nid_export_object(struct hlist_node *hnode)
2109 {
2110         return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2111 }
2112
2113 static void
2114 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2115 {
2116         struct obd_export *exp;
2117
2118         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2119         class_export_get(exp);
2120 }
2121
2122 static void
2123 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2124 {
2125         struct obd_export *exp;
2126
2127         exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2128         class_export_put(exp);
2129 }
2130
2131 static struct cfs_hash_ops nid_hash_ops = {
2132         .hs_hash        = nid_hash,
2133         .hs_key         = nid_key,
2134         .hs_keycmp      = nid_kepcmp,
2135         .hs_object      = nid_export_object,
2136         .hs_get         = nid_export_get,
2137         .hs_put_locked  = nid_export_put_locked,
2138 };
2139
2140
2141 /*
2142  * nid<->nidstats hash operations
2143  */
2144
2145 static void *
2146 nidstats_key(struct hlist_node *hnode)
2147 {
2148         struct nid_stat *ns;
2149
2150         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2151
2152         return &ns->nid;
2153 }
2154
2155 static int
2156 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2157 {
2158         return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2159 }
2160
2161 static void *
2162 nidstats_object(struct hlist_node *hnode)
2163 {
2164         return hlist_entry(hnode, struct nid_stat, nid_hash);
2165 }
2166
2167 static void
2168 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2169 {
2170         struct nid_stat *ns;
2171
2172         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2173         nidstat_getref(ns);
2174 }
2175
2176 static void
2177 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2178 {
2179         struct nid_stat *ns;
2180
2181         ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2182         nidstat_putref(ns);
2183 }
2184
2185 static struct cfs_hash_ops nid_stat_hash_ops = {
2186         .hs_hash        = nid_hash,
2187         .hs_key         = nidstats_key,
2188         .hs_keycmp      = nidstats_keycmp,
2189         .hs_object      = nidstats_object,
2190         .hs_get         = nidstats_get,
2191         .hs_put_locked  = nidstats_put_locked,
2192 };
2193
2194
2195 /*
2196  * client_generation<->export hash operations
2197  */
2198
2199 static unsigned
2200 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2201 {
2202         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2203 }
2204
2205 static void *
2206 gen_key(struct hlist_node *hnode)
2207 {
2208         struct obd_export *exp;
2209
2210         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2211
2212         RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2213 }
2214
2215 /*
2216  * NOTE: It is impossible to find an export that is in failed
2217  *       state with this function
2218  */
2219 static int
2220 gen_kepcmp(const void *key, struct hlist_node *hnode)
2221 {
2222         struct obd_export *exp;
2223
2224         LASSERT(key);
2225         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2226
2227         RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2228                !exp->exp_failed);
2229 }
2230
2231 static void *
2232 gen_export_object(struct hlist_node *hnode)
2233 {
2234         return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2235 }
2236
2237 static void
2238 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2239 {
2240         struct obd_export *exp;
2241
2242         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2243         class_export_get(exp);
2244 }
2245
2246 static void
2247 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2248 {
2249         struct obd_export *exp;
2250
2251         exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2252         class_export_put(exp);
2253 }
2254
2255 static struct cfs_hash_ops gen_hash_ops = {
2256         .hs_hash        = gen_hash,
2257         .hs_key         = gen_key,
2258         .hs_keycmp      = gen_kepcmp,
2259         .hs_object      = gen_export_object,
2260         .hs_get         = gen_export_get,
2261         .hs_put_locked  = gen_export_put_locked,
2262 };