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