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