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