Whamcloud - gitweb
LU-6142 lnet: remove most of typedefs from LNet headers
[fs/lustre-release.git] / lustre / obdclass / obd_mount_server.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2013, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_mount_server.c
33  *
34  * Server mount routines
35  *
36  * Author: Nathan Rutman <nathan@clusterfs.com>
37  */
38
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41 #define D_MOUNT (D_SUPER | D_CONFIG /* | D_WARNING */)
42 #define PRINT_CMD CDEBUG
43 #define PRINT_MASK (D_SUPER | D_CONFIG)
44
45 #include <linux/types.h>
46 #include <linux/selinux.h>
47 #include <linux/statfs.h>
48 #include <linux/version.h>
49 #ifdef HAVE_KERNEL_LOCKED
50 #include <linux/smp_lock.h>
51 #endif
52
53 #include <lustre/lustre_idl.h>
54 #include <lustre/lustre_user.h>
55
56 #include <llog_swab.h>
57 #include <lustre_disk.h>
58 #include <lustre_ioctl.h>
59 #include <lustre_log.h>
60 #include <lustre_param.h>
61 #include <obd.h>
62 #include <obd_class.h>
63
64 /*********** mount lookup *********/
65
66 static DEFINE_MUTEX(lustre_mount_info_lock);
67 static struct list_head server_mount_info_list =
68         LIST_HEAD_INIT(server_mount_info_list);
69
70 static struct lustre_mount_info *server_find_mount(const char *name)
71 {
72         struct list_head *tmp;
73         struct lustre_mount_info *lmi;
74         ENTRY;
75
76         list_for_each(tmp, &server_mount_info_list) {
77                 lmi = list_entry(tmp, struct lustre_mount_info,
78                                  lmi_list_chain);
79                 if (strcmp(name, lmi->lmi_name) == 0)
80                         RETURN(lmi);
81         }
82         RETURN(NULL);
83 }
84
85 /* we must register an obd for a mount before we call the setup routine.
86  *_setup will call lustre_get_mount to get the mnt struct
87  by obd_name, since we can't pass the pointer to setup. */
88 static int server_register_mount(const char *name, struct super_block *sb)
89 {
90         struct lustre_mount_info *lmi;
91         char *name_cp;
92         ENTRY;
93
94         LASSERT(sb);
95
96         OBD_ALLOC(lmi, sizeof(*lmi));
97         if (!lmi)
98                 RETURN(-ENOMEM);
99         OBD_ALLOC(name_cp, strlen(name) + 1);
100         if (!name_cp) {
101                 OBD_FREE(lmi, sizeof(*lmi));
102                 RETURN(-ENOMEM);
103         }
104         strcpy(name_cp, name);
105
106         mutex_lock(&lustre_mount_info_lock);
107
108         if (server_find_mount(name)) {
109                 mutex_unlock(&lustre_mount_info_lock);
110                 OBD_FREE(lmi, sizeof(*lmi));
111                 OBD_FREE(name_cp, strlen(name) + 1);
112                 CERROR("Already registered %s\n", name);
113                 RETURN(-EEXIST);
114         }
115         lmi->lmi_name = name_cp;
116         lmi->lmi_sb = sb;
117         list_add(&lmi->lmi_list_chain, &server_mount_info_list);
118
119         mutex_unlock(&lustre_mount_info_lock);
120
121         CDEBUG(D_MOUNT, "register mount %p from %s\n", sb, name);
122
123         RETURN(0);
124 }
125
126 /* when an obd no longer needs a mount */
127 static int server_deregister_mount(const char *name)
128 {
129         struct lustre_mount_info *lmi;
130         ENTRY;
131
132         mutex_lock(&lustre_mount_info_lock);
133         lmi = server_find_mount(name);
134         if (!lmi) {
135                 mutex_unlock(&lustre_mount_info_lock);
136                 CERROR("%s not registered\n", name);
137                 RETURN(-ENOENT);
138         }
139
140         CDEBUG(D_MOUNT, "deregister mount %p from %s\n", lmi->lmi_sb, name);
141
142         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
143         list_del(&lmi->lmi_list_chain);
144         OBD_FREE(lmi, sizeof(*lmi));
145         mutex_unlock(&lustre_mount_info_lock);
146
147         RETURN(0);
148 }
149
150 /* obd's look up a registered mount using their obdname. This is just
151    for initial obd setup to find the mount struct.  It should not be
152    called every time you want to mntget. */
153 struct lustre_mount_info *server_get_mount(const char *name)
154 {
155         struct lustre_mount_info *lmi;
156         struct lustre_sb_info *lsi;
157         ENTRY;
158
159         mutex_lock(&lustre_mount_info_lock);
160         lmi = server_find_mount(name);
161         mutex_unlock(&lustre_mount_info_lock);
162         if (!lmi) {
163                 CERROR("Can't find mount for %s\n", name);
164                 RETURN(NULL);
165         }
166         lsi = s2lsi(lmi->lmi_sb);
167
168         atomic_inc(&lsi->lsi_mounts);
169
170         CDEBUG(D_MOUNT, "get mount %p from %s, refs=%d\n", lmi->lmi_sb,
171                name, atomic_read(&lsi->lsi_mounts));
172
173         RETURN(lmi);
174 }
175 EXPORT_SYMBOL(server_get_mount);
176
177 /**
178  * server_put_mount: to be called from obd_cleanup methods
179  * @name:       obd name
180  * @dereg_mnt:  0 or 1 depending on whether the mount is to be deregistered or
181  * not
182  *
183  * The caller decides whether server_deregister_mount() needs to be called or
184  * not. Calling of server_deregister_mount() does not depend on refcounting on
185  * lsi because we could have say the mgs and mds on the same node and we
186  * unmount the mds, then the ref on the lsi would still be non-zero but we
187  * would still want to deregister the mds mount.
188  */
189 int server_put_mount(const char *name, bool dereg_mnt)
190 {
191         struct lustre_mount_info *lmi;
192         struct lustre_sb_info *lsi;
193         ENTRY;
194
195         mutex_lock(&lustre_mount_info_lock);
196         lmi = server_find_mount(name);
197         mutex_unlock(&lustre_mount_info_lock);
198         if (!lmi) {
199                 CERROR("Can't find mount for %s\n", name);
200                 RETURN(-ENOENT);
201         }
202         lsi = s2lsi(lmi->lmi_sb);
203
204         CDEBUG(D_MOUNT, "put mount %p from %s, refs=%d\n",
205                lmi->lmi_sb, name, atomic_read(&lsi->lsi_mounts));
206
207         if (lustre_put_lsi(lmi->lmi_sb))
208                 CDEBUG(D_MOUNT, "Last put of mount %p from %s\n",
209                        lmi->lmi_sb, name);
210
211         if (dereg_mnt)
212                 /* this obd should never need the mount again */
213                 server_deregister_mount(name);
214
215         RETURN(0);
216 }
217 EXPORT_SYMBOL(server_put_mount);
218
219 /* Set up a MGS to serve startup logs */
220 static int server_start_mgs(struct super_block *sb)
221 {
222         struct lustre_sb_info    *lsi = s2lsi(sb);
223         struct lustre_mount_info *lmi;
224         int    rc = 0;
225         ENTRY;
226
227         /* It is impossible to have more than 1 MGS per node, since
228            MGC wouldn't know which to connect to */
229         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
230         if (lmi) {
231                 lsi = s2lsi(lmi->lmi_sb);
232                 LCONSOLE_ERROR_MSG(0x15d, "The MGS service was already started"
233                                    " from server\n");
234                 RETURN(-EALREADY);
235         }
236
237         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
238
239         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb);
240
241         if (!rc) {
242                 rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
243                                          LUSTRE_MGS_OBDNAME, NULL, NULL,
244                                          lsi->lsi_osd_obdname, NULL);
245                 /* server_deregister_mount() is not called previously, for lsi
246                  * and other stuff can't be freed cleanly when mgs calls
247                  * server_put_mount() in error handling case (see b=17758),
248                  * this problem is caused by a bug in mgs_init0, which forgot
249                  * calling server_put_mount in error case. */
250
251                 if (rc)
252                         server_deregister_mount(LUSTRE_MGS_OBDNAME);
253         }
254
255         if (rc)
256                 LCONSOLE_ERROR_MSG(0x15e, "Failed to start MGS '%s' (%d). "
257                                    "Is the 'mgs' module loaded?\n",
258                                    LUSTRE_MGS_OBDNAME, rc);
259         RETURN(rc);
260 }
261
262 static int server_stop_mgs(struct super_block *sb)
263 {
264         struct obd_device *obd;
265         int rc;
266         struct lustre_mount_info *lmi;
267         ENTRY;
268
269         /* Do not stop MGS if this device is not the running MGT */
270         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
271         if (lmi != NULL && lmi->lmi_sb != sb)
272                 RETURN(0);
273
274         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
275
276         /* There better be only one MGS */
277         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
278         if (!obd) {
279                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
280                 RETURN(-EALREADY);
281         }
282
283         /* The MGS should always stop when we say so */
284         obd->obd_force = 1;
285         rc = class_manual_cleanup(obd);
286         RETURN(rc);
287 }
288
289 /* Since there's only one mgc per node, we have to change it's fs to get
290    access to the right disk. */
291 static int server_mgc_set_fs(const struct lu_env *env,
292                              struct obd_device *mgc, struct super_block *sb)
293 {
294         struct lustre_sb_info *lsi = s2lsi(sb);
295         int rc;
296         ENTRY;
297
298         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
299
300         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
301         rc = obd_set_info_async(env, mgc->obd_self_export,
302                                 sizeof(KEY_SET_FS), KEY_SET_FS,
303                                 sizeof(*sb), sb, NULL);
304         if (rc != 0)
305                 CERROR("can't set_fs %d\n", rc);
306
307         RETURN(rc);
308 }
309
310 static int server_mgc_clear_fs(const struct lu_env *env,
311                                struct obd_device *mgc)
312 {
313         int rc;
314         ENTRY;
315
316         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
317
318         rc = obd_set_info_async(env, mgc->obd_self_export,
319                                 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
320                                 0, NULL, NULL);
321         RETURN(rc);
322 }
323
324 static inline bool is_mdc_device(const char *devname)
325 {
326         char *ptr;
327
328         ptr = strrchr(devname, '-');
329         return ptr != NULL && strcmp(ptr, "-mdc") == 0;
330 }
331
332 static inline bool tgt_is_mdt(const char *tgtname, __u32 *idx)
333 {
334         int type;
335
336         type = server_name2index(tgtname, idx, NULL);
337
338         return type == LDD_F_SV_TYPE_MDT;
339 }
340
341 /**
342  * Convert OST/MDT name(fsname-{MDT,OST}xxxx) to a lwp name with the @idx:yyyy
343  * (fsname-MDTyyyy-lwp-{MDT,OST}xxxx)
344  **/
345 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx)
346 {
347         char            *fsname;
348         const char      *tgt;
349         int             rc;
350         ENTRY;
351
352         OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
353         if (fsname == NULL)
354                 RETURN(-ENOMEM);
355
356         rc = server_name2fsname(tgt_name, fsname, &tgt);
357         if (rc != 0) {
358                 CERROR("%s: failed to get fsname from tgt_name: rc = %d\n",
359                        tgt_name, rc);
360                 GOTO(cleanup, rc);
361         }
362
363         if (*tgt != '-' && *tgt != ':') {
364                 CERROR("%s: invalid tgt_name name!\n", tgt_name);
365                 GOTO(cleanup, rc = -EINVAL);
366         }
367
368         tgt++;
369         if (strncmp(tgt, "OST", 3) != 0 && strncmp(tgt, "MDT", 3) != 0) {
370                 CERROR("%s is not an OST or MDT target!\n", tgt_name);
371                 GOTO(cleanup, rc = -EINVAL);
372         }
373         snprintf(lwp_name, len, "%s-MDT%04x-%s-%s",
374                  fsname, idx, LUSTRE_LWP_NAME, tgt);
375
376         GOTO(cleanup, rc = 0);
377
378 cleanup:
379         if (fsname != NULL)
380                 OBD_FREE(fsname, MTI_NAME_MAXLEN);
381
382         return rc;
383 }
384 EXPORT_SYMBOL(tgt_name2lwp_name);
385
386 static struct list_head lwp_register_list =
387         LIST_HEAD_INIT(lwp_register_list);
388 static DEFINE_SPINLOCK(lwp_register_list_lock);
389
390 static void lustre_put_lwp_item(struct lwp_register_item *lri)
391 {
392         if (atomic_dec_and_test(&lri->lri_ref)) {
393                 LASSERT(list_empty(&lri->lri_list));
394
395                 if (*lri->lri_exp != NULL)
396                         class_export_put(*lri->lri_exp);
397                 OBD_FREE_PTR(lri);
398         }
399 }
400
401 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
402                              register_lwp_cb cb_func, void *cb_data)
403 {
404         struct obd_device        *lwp;
405         struct lwp_register_item *lri;
406         bool cb = false;
407         ENTRY;
408
409         LASSERTF(strlen(lwpname) < MTI_NAME_MAXLEN, "lwpname is too long %s\n",
410                  lwpname);
411         LASSERT(exp != NULL && *exp == NULL);
412
413         OBD_ALLOC_PTR(lri);
414         if (lri == NULL)
415                 RETURN(-ENOMEM);
416
417         lwp = class_name2obd(lwpname);
418         if (lwp != NULL && lwp->obd_set_up == 1) {
419                 struct obd_uuid *uuid;
420
421                 OBD_ALLOC_PTR(uuid);
422                 if (uuid == NULL) {
423                         OBD_FREE_PTR(lri);
424                         RETURN(-ENOMEM);
425                 }
426                 memcpy(uuid->uuid, lwpname, strlen(lwpname));
427                 *exp = cfs_hash_lookup(lwp->obd_uuid_hash, uuid);
428                 OBD_FREE_PTR(uuid);
429         }
430
431         memcpy(lri->lri_name, lwpname, strlen(lwpname));
432         lri->lri_exp = exp;
433         lri->lri_cb_func = cb_func;
434         lri->lri_cb_data = cb_data;
435         INIT_LIST_HEAD(&lri->lri_list);
436         /*
437          * Initialize the lri_ref at 2, one will be released before
438          * current function returned via lustre_put_lwp_item(), the
439          * other will be released in lustre_deregister_lwp_item().
440          */
441         atomic_set(&lri->lri_ref, 2);
442
443         spin_lock(&lwp_register_list_lock);
444         list_add(&lri->lri_list, &lwp_register_list);
445         if (*exp != NULL)
446                 cb = true;
447         spin_unlock(&lwp_register_list_lock);
448
449         if (cb && cb_func != NULL)
450                 cb_func(cb_data);
451         lustre_put_lwp_item(lri);
452
453         RETURN(0);
454 }
455 EXPORT_SYMBOL(lustre_register_lwp_item);
456
457 void lustre_deregister_lwp_item(struct obd_export **exp)
458 {
459         struct lwp_register_item *lri;
460
461         spin_lock(&lwp_register_list_lock);
462         list_for_each_entry(lri, &lwp_register_list, lri_list) {
463                 if (exp == lri->lri_exp) {
464                         list_del_init(&lri->lri_list);
465                         spin_unlock(&lwp_register_list_lock);
466
467                         lustre_put_lwp_item(lri);
468                         return;
469                 }
470         }
471         spin_unlock(&lwp_register_list_lock);
472 }
473 EXPORT_SYMBOL(lustre_deregister_lwp_item);
474
475 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx)
476 {
477         struct lustre_mount_info *lmi;
478         struct lustre_sb_info    *lsi;
479         struct obd_device        *lwp;
480         struct obd_export        *exp = NULL;
481         char                      fsname[16];
482         char                      lwp_name[24];
483         int                       rc;
484
485         lmi = server_get_mount(dev);
486         if (lmi == NULL)
487                 return NULL;
488
489         lsi = s2lsi(lmi->lmi_sb);
490         rc = server_name2fsname(lsi->lsi_svname, fsname, NULL);
491         if (rc != 0) {
492                 CERROR("%s: failed to get fsname: rc = %d\n",
493                        lsi->lsi_svname, rc);
494                 goto err_lmi;
495         }
496
497         snprintf(lwp_name, sizeof(lwp_name), "%s-MDT%04x", fsname, idx);
498         spin_lock(&lsi->lsi_lwp_lock);
499         list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
500                 char *ptr = strstr(lwp->obd_name, lwp_name);
501
502                 if (ptr != NULL && lwp->obd_lwp_export != NULL) {
503                         exp = class_export_get(lwp->obd_lwp_export);
504                         break;
505                 }
506         }
507         spin_unlock(&lsi->lsi_lwp_lock);
508
509 err_lmi:
510         server_put_mount(dev, false);
511
512         return exp;
513 }
514 EXPORT_SYMBOL(lustre_find_lwp_by_index);
515
516 void lustre_notify_lwp_list(struct obd_export *exp)
517 {
518         struct lwp_register_item *lri;
519         LASSERT(exp != NULL);
520
521 again:
522         spin_lock(&lwp_register_list_lock);
523         list_for_each_entry(lri, &lwp_register_list, lri_list) {
524                 if (strcmp(exp->exp_obd->obd_name, lri->lri_name))
525                         continue;
526                 if (*lri->lri_exp != NULL)
527                         continue;
528                 *lri->lri_exp = class_export_get(exp);
529                 if (lri->lri_cb_func == NULL)
530                         continue;
531                 atomic_inc(&lri->lri_ref);
532                 spin_unlock(&lwp_register_list_lock);
533
534                 lri->lri_cb_func(lri->lri_cb_data);
535                 lustre_put_lwp_item(lri);
536
537                 /* Others may have changed the list after we unlock, we have
538                  * to rescan the list from the beginning. Usually, the list
539                  * 'lwp_register_list' is very short, and there is 'guard'
540                  * lri::lri_exp that will prevent the callback to be done
541                  * repeatedly. So rescanning the list has no problem. */
542                 goto again;
543         }
544         spin_unlock(&lwp_register_list_lock);
545 }
546 EXPORT_SYMBOL(lustre_notify_lwp_list);
547
548 static int lustre_lwp_connect(struct obd_device *lwp)
549 {
550         struct lu_env            env;
551         struct lu_context        session_ctx;
552         struct obd_export       *exp;
553         struct obd_uuid         *uuid = NULL;
554         struct obd_connect_data *data = NULL;
555         int                      rc;
556         ENTRY;
557
558         /* log has been fully processed, let clients connect */
559         rc = lu_env_init(&env, lwp->obd_lu_dev->ld_type->ldt_ctx_tags);
560         if (rc != 0)
561                 RETURN(rc);
562
563         lu_context_init(&session_ctx, LCT_SERVER_SESSION);
564         session_ctx.lc_thread = NULL;
565         lu_context_enter(&session_ctx);
566         env.le_ses = &session_ctx;
567
568         OBD_ALLOC_PTR(data);
569         if (data == NULL)
570                 GOTO(out, rc = -ENOMEM);
571
572         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
573         data->ocd_version = LUSTRE_VERSION_CODE;
574         data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS | OBD_CONNECT_FID |
575                 OBD_CONNECT_AT | OBD_CONNECT_LRU_RESIZE |
576                 OBD_CONNECT_FULL20 | OBD_CONNECT_LVB_TYPE |
577                 OBD_CONNECT_LIGHTWEIGHT | OBD_CONNECT_LFSCK |
578                 OBD_CONNECT_BULK_MBITS;
579         OBD_ALLOC_PTR(uuid);
580         if (uuid == NULL)
581                 GOTO(out, rc = -ENOMEM);
582
583         if (strlen(lwp->obd_name) > sizeof(uuid->uuid)) {
584                 CERROR("%s: Too long lwp name %s, max_size is %d\n",
585                        lwp->obd_name, lwp->obd_name, (int)sizeof(uuid->uuid));
586                 GOTO(out, rc = -EINVAL);
587         }
588
589         /* Use lwp name as the uuid, so we find the export by lwp name later */
590         memcpy(uuid->uuid, lwp->obd_name, strlen(lwp->obd_name));
591         rc = obd_connect(&env, &exp, lwp, uuid, data, NULL);
592         if (rc != 0) {
593                 CERROR("%s: connect failed: rc = %d\n", lwp->obd_name, rc);
594         } else {
595                 if (unlikely(lwp->obd_lwp_export != NULL))
596                         class_export_put(lwp->obd_lwp_export);
597                 lwp->obd_lwp_export = class_export_get(exp);
598         }
599
600         GOTO(out, rc);
601
602 out:
603         if (data != NULL)
604                 OBD_FREE_PTR(data);
605         if (uuid != NULL)
606                 OBD_FREE_PTR(uuid);
607
608         lu_env_fini(&env);
609         lu_context_exit(&session_ctx);
610         lu_context_fini(&session_ctx);
611
612         return rc;
613 }
614
615 /**
616  * lwp is used by slaves (Non-MDT0 targets) to manage the connection to MDT0,
617  * or from the OSTx to MDTy.
618  **/
619 static int lustre_lwp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi,
620                             __u32 idx)
621 {
622         struct obd_device       *obd;
623         char                    *lwpname = NULL;
624         char                    *lwpuuid = NULL;
625         int                      rc;
626         ENTRY;
627
628         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
629                             lcfg->lcfg_nid);
630         if (rc != 0) {
631                 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
632                 RETURN(rc);
633         }
634
635         OBD_ALLOC(lwpname, MTI_NAME_MAXLEN);
636         if (lwpname == NULL)
637                 GOTO(out, rc = -ENOMEM);
638
639         rc = tgt_name2lwp_name(lsi->lsi_svname, lwpname, MTI_NAME_MAXLEN, idx);
640         if (rc != 0) {
641                 CERROR("%s: failed to generate lwp name: rc = %d\n",
642                        lsi->lsi_svname, rc);
643                 GOTO(out, rc);
644         }
645
646         OBD_ALLOC(lwpuuid, MTI_NAME_MAXLEN);
647         if (lwpuuid == NULL)
648                 GOTO(out, rc = -ENOMEM);
649
650         sprintf(lwpuuid, "%s_UUID", lwpname);
651         rc = lustre_start_simple(lwpname, LUSTRE_LWP_NAME,
652                                  lwpuuid, lustre_cfg_string(lcfg, 1),
653                                  NULL, NULL, NULL);
654         if (rc) {
655                 CERROR("%s: setup up failed: rc %d\n", lwpname, rc);
656                 GOTO(out, rc);
657         }
658
659         obd = class_name2obd(lwpname);
660         LASSERT(obd != NULL);
661
662         rc = lustre_lwp_connect(obd);
663         if (rc == 0) {
664                 obd->u.cli.cl_max_mds_easize = MAX_MD_SIZE;
665                 spin_lock(&lsi->lsi_lwp_lock);
666                 list_add_tail(&obd->obd_lwp_list, &lsi->lsi_lwp_list);
667                 spin_unlock(&lsi->lsi_lwp_lock);
668         } else {
669                 CERROR("%s: connect failed: rc = %d\n", lwpname, rc);
670         }
671
672         GOTO(out, rc);
673
674 out:
675         if (lwpname != NULL)
676                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
677         if (lwpuuid != NULL)
678                 OBD_FREE(lwpuuid, MTI_NAME_MAXLEN);
679
680         return rc;
681 }
682
683 /* the caller is responsible for memory free */
684 static struct obd_device *lustre_find_lwp(struct lustre_sb_info *lsi,
685                                           char **lwpname, __u32 idx)
686 {
687         struct obd_device       *lwp;
688         int                      rc = 0;
689         ENTRY;
690
691         LASSERT(lwpname != NULL);
692         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
693
694         OBD_ALLOC(*lwpname, MTI_NAME_MAXLEN);
695         if (*lwpname == NULL)
696                 RETURN(ERR_PTR(-ENOMEM));
697
698         rc = tgt_name2lwp_name(lsi->lsi_svname, *lwpname, MTI_NAME_MAXLEN, idx);
699         if (rc != 0) {
700                 CERROR("%s: failed to generate lwp name: rc = %d\n",
701                        lsi->lsi_svname, rc);
702                 GOTO(out, rc = -EINVAL);
703         }
704
705         lwp = class_name2obd(*lwpname);
706
707 out:
708         if (rc != 0) {
709                 if (*lwpname != NULL) {
710                         OBD_FREE(*lwpname, MTI_NAME_MAXLEN);
711                         *lwpname = NULL;
712                 }
713                 lwp = ERR_PTR(rc);
714         }
715
716         RETURN(lwp != NULL ? lwp : ERR_PTR(-ENOENT));
717 }
718
719 static int lustre_lwp_add_conn(struct lustre_cfg *cfg,
720                                struct lustre_sb_info *lsi, __u32 idx)
721 {
722         struct lustre_cfg_bufs *bufs = NULL;
723         struct lustre_cfg      *lcfg = NULL;
724         char                   *lwpname = NULL;
725         struct obd_device      *lwp;
726         int                     rc;
727         ENTRY;
728
729         lwp = lustre_find_lwp(lsi, &lwpname, idx);
730         if (IS_ERR(lwp)) {
731                 CERROR("%s: can't find lwp device.\n", lsi->lsi_svname);
732                 GOTO(out, rc = PTR_ERR(lwp));
733         }
734         LASSERT(lwpname != NULL);
735
736         OBD_ALLOC_PTR(bufs);
737         if (bufs == NULL)
738                 GOTO(out, rc = -ENOMEM);
739
740         lustre_cfg_bufs_reset(bufs, lwpname);
741         lustre_cfg_bufs_set_string(bufs, 1,
742                                    lustre_cfg_string(cfg, 1));
743
744         lcfg = lustre_cfg_new(LCFG_ADD_CONN, bufs);
745         if (lcfg == NULL)
746                 GOTO(out_cfg, rc = -ENOMEM);
747         rc = class_add_conn(lwp, lcfg);
748         if (rc)
749                 CERROR("%s: can't add conn: rc = %d\n", lwpname, rc);
750
751         if (lcfg != NULL)
752                 lustre_cfg_free(lcfg);
753 out_cfg:
754         if (bufs != NULL)
755                 OBD_FREE_PTR(bufs);
756 out:
757         if (lwpname != NULL)
758                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
759         RETURN(rc);
760 }
761
762 /**
763  * Retrieve MDT nids from the client log, then start the lwp device.
764  * there are only two scenarios which would include mdt nid.
765  * 1.
766  * marker   5 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxx-
767  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:192.168.122.162@tcp
768  * attach    0:lustre-MDTyyyy-mdc  1:mdc  2:lustre-clilmv_UUID
769  * setup     0:lustre-MDTyyyy-mdc  1:lustre-MDTyyyy_UUID  2:192.168.122.162@tcp
770  * add_uuid  nid=192.168.172.1@tcp(0x20000c0a8ac01)  0:  1:192.168.172.1@tcp
771  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.172.1@tcp
772  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDTyyyy_UUID xxxx
773  * marker   5 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxxx-
774  * 2.
775  * marker   7 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
776  * add_uuid  nid=192.168.122.2@tcp(0x20000c0a87a02)  0:  1:192.168.122.2@tcp
777  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.122.2@tcp
778  * marker   7 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
779  **/
780 static int client_lwp_config_process(const struct lu_env *env,
781                                      struct llog_handle *handle,
782                                      struct llog_rec_hdr *rec, void *data)
783 {
784         struct config_llog_instance *cfg = data;
785         int                          cfg_len = rec->lrh_len;
786         char                        *cfg_buf = (char *) (rec + 1);
787         struct lustre_cfg           *lcfg = NULL;
788         struct lustre_sb_info       *lsi;
789         int                          rc = 0, swab = 0;
790         ENTRY;
791
792         if (rec->lrh_type != OBD_CFG_REC) {
793                 CERROR("Unknown llog record type %#x encountered\n",
794                        rec->lrh_type);
795                 RETURN(-EINVAL);
796         }
797
798         if (cfg->cfg_sb == NULL)
799                 GOTO(out, rc = -EINVAL);
800         lsi = s2lsi(cfg->cfg_sb);
801
802         lcfg = (struct lustre_cfg *)cfg_buf;
803         if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
804                 lustre_swab_lustre_cfg(lcfg);
805                 swab = 1;
806         }
807
808         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
809         if (rc)
810                 GOTO(out, rc);
811
812         switch (lcfg->lcfg_command) {
813         case LCFG_MARKER: {
814                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
815
816                 lustre_swab_cfg_marker(marker, swab,
817                                        LUSTRE_CFG_BUFLEN(lcfg, 1));
818                 if (marker->cm_flags & CM_SKIP ||
819                     marker->cm_flags & CM_EXCLUDE)
820                         GOTO(out, rc = 0);
821
822                 if (!tgt_is_mdt(marker->cm_tgtname, &cfg->cfg_lwp_idx))
823                         GOTO(out, rc = 0);
824
825                 if (IS_MDT(lsi) && cfg->cfg_lwp_idx != 0)
826                         GOTO(out, rc = 0);
827
828                 if (!strncmp(marker->cm_comment, "add mdc", 7) ||
829                     !strncmp(marker->cm_comment, "add failnid", 11)) {
830                         if (marker->cm_flags & CM_START) {
831                                 cfg->cfg_flags = CFG_F_MARKER;
832                                 /* This hack is to differentiate the
833                                  * ADD_UUID is come from "add mdc" record
834                                  * or from "add failnid" record. */
835                                 if (!strncmp(marker->cm_comment,
836                                              "add failnid", 11))
837                                         cfg->cfg_flags |= CFG_F_SKIP;
838                         } else if (marker->cm_flags & CM_END) {
839                                 cfg->cfg_flags = 0;
840                         }
841                 }
842                 break;
843         }
844         case LCFG_ADD_UUID: {
845                 if (cfg->cfg_flags == CFG_F_MARKER) {
846                         rc = lustre_lwp_setup(lcfg, lsi, cfg->cfg_lwp_idx);
847                         /* XXX: process only the first nid as
848                          * we don't need another instance of lwp */
849                         cfg->cfg_flags |= CFG_F_SKIP;
850                 } else if (cfg->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
851                         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
852                                             lcfg->lcfg_nid);
853                         if (rc)
854                                 CERROR("%s: Fail to add uuid, rc:%d\n",
855                                        lsi->lsi_svname, rc);
856                 }
857                 break;
858         }
859         case LCFG_ADD_CONN: {
860                 char *devname = lustre_cfg_string(lcfg, 0);
861                 char *ptr;
862                 __u32 idx     = 0;
863
864                 if (!is_mdc_device(devname))
865                         break;
866
867                 ptr = strrchr(devname, '-');
868                 if (ptr == NULL)
869                         break;
870
871                 *ptr = 0;
872                 if (!tgt_is_mdt(devname, &idx)) {
873                         *ptr = '-';
874                         break;
875                 }
876                 *ptr = '-';
877
878                 if (IS_MDT(lsi) && idx != 0)
879                         break;
880
881                 rc = lustre_lwp_add_conn(lcfg, lsi, idx);
882                 break;
883         }
884         default:
885                 break;
886         }
887 out:
888         RETURN(rc);
889 }
890
891 static int lustre_disconnect_lwp(struct super_block *sb)
892 {
893         struct lustre_sb_info           *lsi     = s2lsi(sb);
894         struct obd_device               *lwp;
895         char                            *logname = NULL;
896         struct lustre_cfg_bufs          *bufs    = NULL;
897         struct config_llog_instance     *cfg     = NULL;
898         int                              rc      = 0;
899         int                              rc1     = 0;
900         ENTRY;
901
902         if (likely(lsi->lsi_lwp_started)) {
903                 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
904                 if (logname == NULL)
905                         RETURN(-ENOMEM);
906
907                 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
908                 if (rc != 0) {
909                         CERROR("%s: failed to get fsname from svname: "
910                                "rc = %d\n", lsi->lsi_svname, rc);
911                         GOTO(out, rc = -EINVAL);
912                 }
913
914                 strcat(logname, "-client");
915                 OBD_ALLOC_PTR(cfg);
916                 if (cfg == NULL)
917                         GOTO(out, rc = -ENOMEM);
918
919                 /* end log first */
920                 cfg->cfg_instance = sb;
921                 rc = lustre_end_log(sb, logname, cfg);
922                 if (rc != 0 && rc != -ENOENT)
923                         GOTO(out, rc);
924
925                 lsi->lsi_lwp_started = 0;
926         }
927
928         OBD_ALLOC_PTR(bufs);
929         if (bufs == NULL)
930                 GOTO(out, rc = -ENOMEM);
931
932         list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
933                 struct lustre_cfg *lcfg;
934
935                 if (likely(lwp->obd_lwp_export != NULL)) {
936                         class_export_put(lwp->obd_lwp_export);
937                         lwp->obd_lwp_export = NULL;
938                 }
939
940                 lustre_cfg_bufs_reset(bufs, lwp->obd_name);
941                 lustre_cfg_bufs_set_string(bufs, 1, NULL);
942                 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
943                 if (lcfg == NULL)
944                         GOTO(out, rc = -ENOMEM);
945
946                 /* Disconnect import first. NULL is passed for the '@env',
947                  * since it will not be used. */
948                 rc = lwp->obd_lu_dev->ld_ops->ldo_process_config(NULL,
949                                                         lwp->obd_lu_dev, lcfg);
950                 lustre_cfg_free(lcfg);
951                 if (rc != 0 && rc != -ETIMEDOUT) {
952                         CERROR("%s: fail to disconnect LWP: rc = %d\n",
953                                lwp->obd_name, rc);
954                         rc1 = rc;
955                 }
956         }
957
958         GOTO(out, rc);
959
960 out:
961         if (bufs != NULL)
962                 OBD_FREE_PTR(bufs);
963         if (cfg != NULL)
964                 OBD_FREE_PTR(cfg);
965         if (logname != NULL)
966                 OBD_FREE(logname, MTI_NAME_MAXLEN);
967
968         return rc1 != 0 ? rc1 : rc;
969 }
970
971 /**
972  * Stop the lwp for an OST/MDT target.
973  **/
974 static int lustre_stop_lwp(struct super_block *sb)
975 {
976         struct lustre_sb_info   *lsi = s2lsi(sb);
977         struct obd_device       *lwp;
978         int                      rc  = 0;
979         int                      rc1 = 0;
980         ENTRY;
981
982         while (!list_empty(&lsi->lsi_lwp_list)) {
983                 lwp = list_entry(lsi->lsi_lwp_list.next, struct obd_device,
984                                  obd_lwp_list);
985                 list_del_init(&lwp->obd_lwp_list);
986                 lwp->obd_force = 1;
987                 rc = class_manual_cleanup(lwp);
988                 if (rc != 0) {
989                         CERROR("%s: fail to stop LWP: rc = %d\n",
990                                lwp->obd_name, rc);
991                         rc1 = rc;
992                 }
993         }
994
995         RETURN(rc1 != 0 ? rc1 : rc);
996 }
997
998 /**
999  * Start the lwp(fsname-MDTyyyy-lwp-{MDT,OST}xxxx) for a MDT/OST or MDT target.
1000  **/
1001 static int lustre_start_lwp(struct super_block *sb)
1002 {
1003         struct lustre_sb_info       *lsi = s2lsi(sb);
1004         struct config_llog_instance *cfg = NULL;
1005         char                        *logname;
1006         int                          rc;
1007         ENTRY;
1008
1009         if (unlikely(lsi->lsi_lwp_started))
1010                 RETURN(0);
1011
1012         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1013         if (logname == NULL)
1014                 RETURN(-ENOMEM);
1015
1016         rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
1017         if (rc != 0) {
1018                 CERROR("%s: failed to get fsname from svname: rc = %d\n",
1019                        lsi->lsi_svname, rc);
1020                 GOTO(out, rc = -EINVAL);
1021         }
1022
1023         strcat(logname, "-client");
1024         OBD_ALLOC_PTR(cfg);
1025         if (cfg == NULL)
1026                 GOTO(out, rc = -ENOMEM);
1027
1028         cfg->cfg_callback = client_lwp_config_process;
1029         cfg->cfg_instance = sb;
1030         rc = lustre_process_log(sb, logname, cfg);
1031         /* need to remove config llog from mgc */
1032         lsi->lsi_lwp_started = 1;
1033
1034         GOTO(out, rc);
1035
1036 out:
1037         OBD_FREE(logname, MTI_NAME_MAXLEN);
1038         if (cfg != NULL)
1039                 OBD_FREE_PTR(cfg);
1040
1041         return rc;
1042 }
1043
1044 static DEFINE_MUTEX(server_start_lock);
1045
1046 /* Stop MDS/OSS if nobody is using them */
1047 static int server_stop_servers(int lsiflags)
1048 {
1049         struct obd_device *obd = NULL;
1050         struct obd_type *type = NULL;
1051         int rc = 0;
1052         ENTRY;
1053
1054         mutex_lock(&server_start_lock);
1055
1056         /* Either an MDT or an OST or neither  */
1057         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1058         if (lsiflags & LDD_F_SV_TYPE_MDT) {
1059                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1060                 if (obd != NULL)
1061                         type = class_search_type(LUSTRE_MDT_NAME);
1062         }
1063
1064         /* if this was an OST, and there are no more OST's, clean up the OSS */
1065         if (lsiflags & LDD_F_SV_TYPE_OST) {
1066                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1067                 if (obd != NULL)
1068                         type = class_search_type(LUSTRE_OST_NAME);
1069         }
1070
1071         if (obd != NULL && (type == NULL || type->typ_refcnt == 0)) {
1072                 obd->obd_force = 1;
1073                 /* obd_fail doesn't mean much on a server obd */
1074                 rc = class_manual_cleanup(obd);
1075         }
1076
1077         mutex_unlock(&server_start_lock);
1078
1079         RETURN(rc);
1080 }
1081
1082 int server_mti_print(const char *title, struct mgs_target_info *mti)
1083 {
1084         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1085         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1086         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
1087         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
1088         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
1089                   mti->mti_config_ver, mti->mti_flags);
1090         return 0;
1091 }
1092
1093 /* Generate data for registration */
1094 static int server_lsi2mti(struct lustre_sb_info *lsi,
1095                           struct mgs_target_info *mti)
1096 {
1097         struct lnet_process_id id;
1098         int rc, i = 0;
1099         int cplen = 0;
1100         ENTRY;
1101
1102         if (!IS_SERVER(lsi))
1103                 RETURN(-EINVAL);
1104
1105         if (strlcpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname))
1106             >= sizeof(mti->mti_svname))
1107                 RETURN(-E2BIG);
1108
1109         mti->mti_nid_count = 0;
1110         while (LNetGetId(i++, &id) != -ENOENT) {
1111                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
1112                         continue;
1113
1114                 /* server use --servicenode param, only allow specified
1115                  * nids be registered */
1116                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) != 0 &&
1117                     class_match_nid(lsi->lsi_lmd->lmd_params,
1118                                     PARAM_FAILNODE, id.nid) < 1)
1119                         continue;
1120
1121                 /* match specified network */
1122                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1123                                      PARAM_NETWORK, LNET_NIDNET(id.nid)))
1124                         continue;
1125
1126                 mti->mti_nids[mti->mti_nid_count] = id.nid;
1127                 mti->mti_nid_count++;
1128                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1129                         CWARN("Only using first %d nids for %s\n",
1130                               mti->mti_nid_count, mti->mti_svname);
1131                         break;
1132                 }
1133         }
1134
1135         if (mti->mti_nid_count == 0) {
1136                 CERROR("Failed to get NID for server %s, please check whether "
1137                        "the target is specifed with improper --servicenode or "
1138                        "--network options.\n", mti->mti_svname);
1139                 RETURN(-EINVAL);
1140         }
1141
1142         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1143         mti->mti_config_ver = 0;
1144
1145         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1146         if (rc != 0)
1147                 return rc;
1148
1149         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1150         if (rc < 0)
1151                 return rc;
1152         /* Orion requires index to be set */
1153         LASSERT(!(rc & LDD_F_NEED_INDEX));
1154         /* keep only LDD flags */
1155         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1156         if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1157                 mti->mti_flags |= LDD_F_UPDATE;
1158         cplen = strlcpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1159                         sizeof(mti->mti_params));
1160         if (cplen >= sizeof(mti->mti_params))
1161                 return -E2BIG;
1162         return 0;
1163 }
1164
1165 /* Register an old or new target with the MGS. If needed MGS will construct
1166    startup logs and assign index */
1167 static int server_register_target(struct lustre_sb_info *lsi)
1168 {
1169         struct obd_device *mgc = lsi->lsi_mgc;
1170         struct mgs_target_info *mti = NULL;
1171         bool writeconf;
1172         int rc;
1173         ENTRY;
1174
1175         LASSERT(mgc);
1176
1177         if (!IS_SERVER(lsi))
1178                 RETURN(-EINVAL);
1179
1180         OBD_ALLOC_PTR(mti);
1181         if (!mti)
1182                 RETURN(-ENOMEM);
1183
1184         rc = server_lsi2mti(lsi, mti);
1185         if (rc)
1186                 GOTO(out, rc);
1187
1188         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1189                mti->mti_svname, mti->mti_fsname,
1190                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1191                mti->mti_flags);
1192
1193         /* if write_conf is true, the registration must succeed */
1194         writeconf = !!(lsi->lsi_flags & (LDD_F_NEED_INDEX | LDD_F_UPDATE));
1195         mti->mti_flags |= LDD_F_OPC_REG;
1196
1197         /* Register the target */
1198         /* FIXME use mgc_process_config instead */
1199         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1200                                 sizeof(KEY_REGISTER_TARGET),
1201                                 KEY_REGISTER_TARGET,
1202                                 sizeof(*mti), mti, NULL);
1203         if (rc) {
1204                 if (mti->mti_flags & LDD_F_ERROR) {
1205                         LCONSOLE_ERROR_MSG(0x160,
1206                                 "%s: the MGS refuses to allow this server "
1207                                 "to start: rc = %d. Please see messages on "
1208                                 "the MGS.\n", lsi->lsi_svname, rc);
1209                 } else if (writeconf) {
1210                         LCONSOLE_ERROR_MSG(0x15f,
1211                                 "%s: cannot register this server with the MGS: "
1212                                 "rc = %d. Is the MGS running?\n",
1213                                 lsi->lsi_svname, rc);
1214                 } else {
1215                         CDEBUG(D_HA, "%s: error registering with the MGS: "
1216                                "rc = %d (not fatal)\n", lsi->lsi_svname, rc);
1217                         /* reset the error code for non-fatal error. */
1218                         rc = 0;
1219                 }
1220                 GOTO(out, rc);
1221         }
1222
1223 out:
1224         if (mti)
1225                 OBD_FREE_PTR(mti);
1226         RETURN(rc);
1227 }
1228
1229 /**
1230  * Notify the MGS that this target is ready.
1231  * Used by IR - if the MGS receives this message, it will notify clients.
1232  */
1233 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1234 {
1235         struct lustre_sb_info *lsi = s2lsi(sb);
1236         struct obd_device *mgc = lsi->lsi_mgc;
1237         struct mgs_target_info *mti = NULL;
1238         int rc;
1239         ENTRY;
1240
1241         LASSERT(mgc);
1242
1243         if (!(IS_SERVER(lsi)))
1244                 RETURN(-EINVAL);
1245
1246         OBD_ALLOC_PTR(mti);
1247         if (!mti)
1248                 RETURN(-ENOMEM);
1249         rc = server_lsi2mti(lsi, mti);
1250         if (rc)
1251                 GOTO(out, rc);
1252
1253         mti->mti_instance = obd->u.obt.obt_instance;
1254         mti->mti_flags |= LDD_F_OPC_READY;
1255
1256         /* FIXME use mgc_process_config instead */
1257         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1258                                 sizeof(KEY_REGISTER_TARGET),
1259                                 KEY_REGISTER_TARGET,
1260                                 sizeof(*mti), mti, NULL);
1261
1262         /* Imperative recovery: if the mgs informs us to use IR? */
1263         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1264             (mti->mti_flags & LDD_F_IR_CAPABLE))
1265                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1266
1267 out:
1268         if (mti)
1269                 OBD_FREE_PTR(mti);
1270         RETURN(rc);
1271
1272 }
1273
1274 /** Start server targets: MDTs and OSTs
1275  */
1276 static int server_start_targets(struct super_block *sb)
1277 {
1278         struct obd_device *obd;
1279         struct lustre_sb_info *lsi = s2lsi(sb);
1280         struct config_llog_instance cfg;
1281         struct lu_env mgc_env;
1282         struct lu_device *dev;
1283         int rc;
1284         ENTRY;
1285
1286         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1287
1288         if (IS_MDT(lsi)) {
1289                 /* make sure the MDS is started */
1290                 mutex_lock(&server_start_lock);
1291                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1292                 if (!obd) {
1293                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1294                                                  LUSTRE_MDS_NAME,
1295                                                  LUSTRE_MDS_OBDNAME"_uuid",
1296                                                  NULL, NULL, NULL, NULL);
1297                         if (rc) {
1298                                 mutex_unlock(&server_start_lock);
1299                                 CERROR("failed to start MDS: %d\n", rc);
1300                                 RETURN(rc);
1301                         }
1302                 }
1303                 mutex_unlock(&server_start_lock);
1304         }
1305
1306         /* If we're an OST, make sure the global OSS is running */
1307         if (IS_OST(lsi)) {
1308                 /* make sure OSS is started */
1309                 mutex_lock(&server_start_lock);
1310                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1311                 if (!obd) {
1312                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1313                                                  LUSTRE_OSS_NAME,
1314                                                  LUSTRE_OSS_OBDNAME"_uuid",
1315                                                  NULL, NULL, NULL, NULL);
1316                         if (rc) {
1317                                 mutex_unlock(&server_start_lock);
1318                                 CERROR("failed to start OSS: %d\n", rc);
1319                                 RETURN(rc);
1320                         }
1321                 }
1322                 mutex_unlock(&server_start_lock);
1323         }
1324
1325         rc = lu_env_init(&mgc_env, LCT_MG_THREAD);
1326         if (rc != 0)
1327                 GOTO(out_stop_service, rc);
1328
1329         /* Set the mgc fs to our server disk.  This allows the MGC to
1330          * read and write configs locally, in case it can't talk to the MGS. */
1331         rc = server_mgc_set_fs(&mgc_env, lsi->lsi_mgc, sb);
1332         if (rc)
1333                 GOTO(out_env, rc);
1334
1335         /* Register with MGS */
1336         rc = server_register_target(lsi);
1337         if (rc)
1338                 GOTO(out_mgc, rc);
1339
1340         /* Let the target look up the mount using the target's name
1341            (we can't pass the sb or mnt through class_process_config.) */
1342         rc = server_register_mount(lsi->lsi_svname, sb);
1343         if (rc)
1344                 GOTO(out_mgc, rc);
1345
1346         /* Start targets using the llog named for the target */
1347         memset(&cfg, 0, sizeof(cfg));
1348         cfg.cfg_callback = class_config_llog_handler;
1349         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1350         if (rc) {
1351                 CERROR("failed to start server %s: %d\n",
1352                        lsi->lsi_svname, rc);
1353                 /* Do NOT call server_deregister_mount() here. This makes it
1354                  * impossible to find mount later in cleanup time and leaves
1355                  * @lsi and othder stuff leaked. -umka */
1356                 GOTO(out_mgc, rc);
1357         }
1358
1359         obd = class_name2obd(lsi->lsi_svname);
1360         if (!obd) {
1361                 CERROR("no server named %s was started\n", lsi->lsi_svname);
1362                 GOTO(out_mgc, rc = -ENXIO);
1363         }
1364
1365         if (IS_OST(lsi) || IS_MDT(lsi)) {
1366                 rc = lustre_start_lwp(sb);
1367                 if (rc) {
1368                         CERROR("%s: failed to start LWP: %d\n",
1369                                lsi->lsi_svname, rc);
1370                         GOTO(out_mgc, rc);
1371                 }
1372         }
1373
1374         server_notify_target(sb, obd);
1375
1376         /* calculate recovery timeout, do it after lustre_process_log */
1377         server_calc_timeout(lsi, obd);
1378
1379         /* log has been fully processed */
1380         obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1381
1382         /* log has been fully processed, let clients connect */
1383         dev = obd->obd_lu_dev;
1384         if (dev && dev->ld_ops->ldo_prepare) {
1385                 struct lu_env env;
1386
1387                 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1388                 if (rc == 0) {
1389                         struct lu_context  session_ctx;
1390
1391                         lu_context_init(&session_ctx, LCT_SERVER_SESSION);
1392                         session_ctx.lc_thread = NULL;
1393                         lu_context_enter(&session_ctx);
1394                         env.le_ses = &session_ctx;
1395
1396                         rc = dev->ld_ops->ldo_prepare(&env, NULL, dev);
1397
1398                         lu_env_fini(&env);
1399                         lu_context_exit(&session_ctx);
1400                         lu_context_fini(&session_ctx);
1401                 }
1402         }
1403
1404         /* abort recovery only on the complete stack:
1405          * many devices can be involved */
1406         if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1407             (OBP(obd, iocontrol))) {
1408                 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1409                               NULL, NULL);
1410         }
1411
1412 out_mgc:
1413         /* Release the mgc fs for others to use */
1414         server_mgc_clear_fs(&mgc_env, lsi->lsi_mgc);
1415 out_env:
1416         lu_env_fini(&mgc_env);
1417 out_stop_service:
1418         if (rc != 0)
1419                 server_stop_servers(lsi->lsi_flags);
1420
1421         RETURN(rc);
1422 }
1423
1424 static int lsi_prepare(struct lustre_sb_info *lsi)
1425 {
1426         const char *osd_type;
1427         const char *fstype;
1428         __u32 index;
1429         int rc;
1430         ENTRY;
1431
1432         LASSERT(lsi);
1433         LASSERT(lsi->lsi_lmd);
1434
1435         /* The server name is given as a mount line option */
1436         if (lsi->lsi_lmd->lmd_profile == NULL) {
1437                 LCONSOLE_ERROR("Can't determine server name\n");
1438                 RETURN(-EINVAL);
1439         }
1440
1441         /* Determine osd type */
1442         if (lsi->lsi_lmd->lmd_osd_type == NULL) {
1443                 osd_type = LUSTRE_OSD_LDISKFS_NAME;
1444                 fstype = "ldiskfs";
1445         } else {
1446                 osd_type = lsi->lsi_lmd->lmd_osd_type;
1447                 fstype = lsi->lsi_lmd->lmd_osd_type;
1448         }
1449
1450         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname) ||
1451             strlen(osd_type) >= sizeof(lsi->lsi_osd_type) ||
1452             strlen(fstype) >= sizeof(lsi->lsi_fstype))
1453                 RETURN(-ENAMETOOLONG);
1454
1455         strlcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile,
1456                 sizeof(lsi->lsi_svname));
1457         strlcpy(lsi->lsi_osd_type, osd_type, sizeof(lsi->lsi_osd_type));
1458         /* XXX: a temp. solution for components using ldiskfs
1459          *      to be removed in one of the subsequent patches */
1460         strlcpy(lsi->lsi_fstype, fstype, sizeof(lsi->lsi_fstype));
1461
1462         /* Determine server type */
1463         rc = server_name2index(lsi->lsi_svname, &index, NULL);
1464         if (rc < 0) {
1465                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) {
1466                         /* Assume we're a bare MGS */
1467                         rc = 0;
1468                         lsi->lsi_lmd->lmd_flags |= LMD_FLG_NOSVC;
1469                 } else {
1470                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1471                                        lsi->lsi_svname);
1472                         RETURN(rc);
1473                 }
1474         }
1475         lsi->lsi_flags |= rc;
1476
1477         /* Add mount line flags that used to be in ldd:
1478          * writeconf, mgs, anything else?
1479          */
1480         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_WRITECONF) ?
1481                 LDD_F_WRITECONF : 0;
1482         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_VIRGIN) ?
1483                 LDD_F_VIRGIN : 0;
1484         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_UPDATE) ?
1485                 LDD_F_UPDATE : 0;
1486         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) ?
1487                 LDD_F_SV_TYPE_MGS : 0;
1488         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) ?
1489                 LDD_F_NO_PRIMNODE : 0;
1490
1491         RETURN(0);
1492 }
1493
1494 /*************** server mount ******************/
1495
1496 /** Start the shutdown of servers at umount.
1497  */
1498 static void server_put_super(struct super_block *sb)
1499 {
1500         struct lustre_sb_info *lsi = s2lsi(sb);
1501         struct obd_device     *obd;
1502         char *tmpname, *extraname = NULL;
1503         int tmpname_sz;
1504         int lsiflags = lsi->lsi_flags;
1505         ENTRY;
1506
1507         LASSERT(IS_SERVER(lsi));
1508
1509         tmpname_sz = strlen(lsi->lsi_svname) + 1;
1510         OBD_ALLOC(tmpname, tmpname_sz);
1511         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1512         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1513         if (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC))
1514                 snprintf(tmpname, tmpname_sz, "MGS");
1515
1516         /* disconnect the lwp first to drain off the inflight request */
1517         if (IS_OST(lsi) || IS_MDT(lsi)) {
1518                 int     rc;
1519
1520                 rc = lustre_disconnect_lwp(sb);
1521                 if (rc != 0 && rc != -ETIMEDOUT &&
1522                     rc != -ENOTCONN && rc != -ESHUTDOWN)
1523                         CWARN("%s: failed to disconnect lwp: rc= %d\n",
1524                               tmpname, rc);
1525         }
1526
1527         /* Stop the target */
1528         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1529             (IS_MDT(lsi) || IS_OST(lsi))) {
1530                 struct lustre_profile *lprof = NULL;
1531
1532                 /* tell the mgc to drop the config log */
1533                 lustre_end_log(sb, lsi->lsi_svname, NULL);
1534
1535                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1536                    If there are any setup/cleanup errors, save the lov
1537                    name for safety cleanup later. */
1538                 lprof = class_get_profile(lsi->lsi_svname);
1539                 if (lprof != NULL) {
1540                         if (lprof->lp_dt != NULL) {
1541                                 OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1542                                 strncpy(extraname, lprof->lp_dt,
1543                                         strlen(lprof->lp_dt) + 1);
1544                         }
1545                         class_put_profile(lprof);
1546                 }
1547
1548                 obd = class_name2obd(lsi->lsi_svname);
1549                 if (obd) {
1550                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1551                         if (lsiflags & LSI_UMOUNT_FAILOVER)
1552                                 obd->obd_fail = 1;
1553                         /* We can't seem to give an error return code
1554                          * to .put_super, so we better make sure we clean up! */
1555                         obd->obd_force = 1;
1556                         class_manual_cleanup(obd);
1557                 } else {
1558                         CERROR("no obd %s\n", lsi->lsi_svname);
1559                         server_deregister_mount(lsi->lsi_svname);
1560                 }
1561         }
1562
1563         /* If they wanted the mgs to stop separately from the mdt, they
1564            should have put it on a different device. */
1565         if (IS_MGS(lsi)) {
1566                 /* if MDS start with --nomgs, don't stop MGS then */
1567                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
1568                         server_stop_mgs(sb);
1569         }
1570
1571         if (IS_OST(lsi) || IS_MDT(lsi)) {
1572                 if (lustre_stop_lwp(sb) < 0)
1573                         CERROR("%s: failed to stop lwp!\n", tmpname);
1574         }
1575
1576         /* Clean the mgc and sb */
1577         lustre_common_put_super(sb);
1578
1579         /* wait till all in-progress cleanups are done
1580          * specifically we're interested in ofd cleanup
1581          * as it pins OSS */
1582         obd_zombie_barrier();
1583
1584         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1585            until the target is really gone so that our type refcount check
1586            is right. */
1587         server_stop_servers(lsiflags);
1588
1589         /* In case of startup or cleanup err, stop related obds */
1590         if (extraname) {
1591                 obd = class_name2obd(extraname);
1592                 if (obd) {
1593                         CWARN("Cleaning orphaned obd %s\n", extraname);
1594                         obd->obd_force = 1;
1595                         class_manual_cleanup(obd);
1596                 }
1597                 OBD_FREE(extraname, strlen(extraname) + 1);
1598         }
1599
1600         LCONSOLE_WARN("server umount %s complete\n", tmpname);
1601         OBD_FREE(tmpname, tmpname_sz);
1602         EXIT;
1603 }
1604
1605 /** Called only for 'umount -f'
1606  */
1607 static void server_umount_begin(struct super_block *sb)
1608 {
1609         struct lustre_sb_info *lsi = s2lsi(sb);
1610         ENTRY;
1611
1612         CDEBUG(D_MOUNT, "umount -f\n");
1613         /* umount = failover
1614            umount -f = force
1615            no third way to do non-force, non-failover */
1616         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1617         EXIT;
1618 }
1619
1620 static int server_statfs(struct dentry *dentry, struct kstatfs *buf)
1621 {
1622         struct super_block *sb = dentry->d_sb;
1623         struct lustre_sb_info *lsi = s2lsi(sb);
1624         struct obd_statfs statfs;
1625         int rc;
1626         ENTRY;
1627
1628         if (lsi->lsi_dt_dev) {
1629                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1630                 if (rc == 0) {
1631                         statfs_unpack(buf, &statfs);
1632                         buf->f_type = sb->s_magic;
1633                         RETURN(0);
1634                 }
1635         }
1636
1637         /* just return 0 */
1638         buf->f_type = sb->s_magic;
1639         buf->f_bsize = sb->s_blocksize;
1640         buf->f_blocks = 1;
1641         buf->f_bfree = 0;
1642         buf->f_bavail = 0;
1643         buf->f_files = 1;
1644         buf->f_ffree = 0;
1645         buf->f_namelen = NAME_MAX;
1646         RETURN(0);
1647 }
1648
1649 /** The operations we support directly on the superblock:
1650  * mount, umount, and df.
1651  */
1652 static struct super_operations server_ops = {
1653         .put_super      = server_put_super,
1654         .umount_begin   = server_umount_begin, /* umount -f */
1655         .statfs         = server_statfs,
1656 };
1657
1658 /*
1659  * Xattr support for Lustre servers
1660  */
1661 static ssize_t lustre_getxattr(struct dentry *dentry, const char *name,
1662                                 void *buffer, size_t size)
1663 {
1664         if (!selinux_is_enabled())
1665                 return -EOPNOTSUPP;
1666         return -ENODATA;
1667 }
1668
1669 static int lustre_setxattr(struct dentry *dentry, const char *name,
1670                             const void *value, size_t size, int flags)
1671 {
1672         return -EOPNOTSUPP;
1673 }
1674
1675 static ssize_t lustre_listxattr(struct dentry *d_entry, char *name,
1676                                 size_t size)
1677 {
1678         return -EOPNOTSUPP;
1679 }
1680
1681 static const struct inode_operations server_inode_operations = {
1682         .setxattr       = lustre_setxattr,
1683         .getxattr       = lustre_getxattr,
1684         .listxattr      = lustre_listxattr,
1685 };
1686
1687 #define log2(n) ffz(~(n))
1688 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1689
1690 static int server_fill_super_common(struct super_block *sb)
1691 {
1692         struct inode *root = NULL;
1693         ENTRY;
1694
1695         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1696
1697         sb->s_blocksize = 4096;
1698         sb->s_blocksize_bits = log2(sb->s_blocksize);
1699         sb->s_magic = LUSTRE_SUPER_MAGIC;
1700         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
1701         sb->s_flags |= MS_RDONLY;
1702         sb->s_op = &server_ops;
1703
1704         root = new_inode(sb);
1705         if (!root) {
1706                 CERROR("Can't make root inode\n");
1707                 RETURN(-EIO);
1708         }
1709
1710         /* returns -EIO for every operation */
1711         /* make_bad_inode(root); -- badness - can't umount */
1712         /* apparently we need to be a directory for the mount to finish */
1713         root->i_mode = S_IFDIR;
1714         root->i_op = &server_inode_operations;
1715         sb->s_root = d_make_root(root);
1716         if (!sb->s_root) {
1717                 CERROR("%s: can't make root dentry\n", sb->s_id);
1718                 RETURN(-EIO);
1719         }
1720
1721         RETURN(0);
1722 }
1723
1724 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
1725 {
1726         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1727         struct obd_device        *obd;
1728         struct dt_device_param    p;
1729         char                      flagstr[16];
1730         int                       rc;
1731         ENTRY;
1732
1733         CDEBUG(D_MOUNT,
1734                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
1735                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
1736
1737         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
1738         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
1739         strcat(lsi->lsi_osd_uuid, "_UUID");
1740         sprintf(flagstr, "%lu:%lu", mflags, (unsigned long) lmd->lmd_flags);
1741
1742         obd = class_name2obd(lsi->lsi_osd_obdname);
1743         if (obd == NULL) {
1744                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
1745                                          lsi->lsi_osd_type,
1746                                          lsi->lsi_osd_uuid, lmd->lmd_dev,
1747                                          flagstr, lsi->lsi_lmd->lmd_opts,
1748                                          lsi->lsi_svname);
1749                 if (rc)
1750                         GOTO(out, rc);
1751                 obd = class_name2obd(lsi->lsi_osd_obdname);
1752                 LASSERT(obd);
1753         } else {
1754                 CDEBUG(D_MOUNT, "%s already started\n", lsi->lsi_osd_obdname);
1755                 /* but continue setup to allow special case of MDT and internal
1756                  * MGT being started separately. */
1757                 if (!((IS_MGS(lsi) && (lsi->lsi_lmd->lmd_flags &
1758                                       LMD_FLG_NOMGS)) ||
1759                      (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags &
1760                                       LMD_FLG_NOSVC))))
1761                         RETURN(-EALREADY);
1762         }
1763
1764         rc = obd_connect(NULL, &lsi->lsi_osd_exp,
1765                          obd, &obd->obd_uuid, NULL, NULL);
1766
1767         if (rc) {
1768                 obd->obd_force = 1;
1769                 class_manual_cleanup(obd);
1770                 lsi->lsi_dt_dev = NULL;
1771                 RETURN(rc);
1772         }
1773
1774         LASSERT(obd->obd_lu_dev);
1775         lu_device_get(obd->obd_lu_dev);
1776         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
1777         LASSERT(lsi->lsi_dt_dev);
1778
1779         /* set disk context for llog usage */
1780         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1781         obd->obd_lvfs_ctxt.dt = lsi->lsi_dt_dev;
1782
1783         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
1784 out:
1785         RETURN(rc);
1786 }
1787
1788 /** Fill in the superblock info for a Lustre server.
1789  * Mount the device with the correct options.
1790  * Read the on-disk config file.
1791  * Start the services.
1792  */
1793 int server_fill_super(struct super_block *sb)
1794 {
1795         struct lustre_sb_info *lsi = s2lsi(sb);
1796         int rc;
1797         ENTRY;
1798
1799         /* to simulate target mount race */
1800         OBD_RACE(OBD_FAIL_TGT_MOUNT_RACE);
1801
1802         rc = lsi_prepare(lsi);
1803         if (rc)
1804                 RETURN(rc);
1805
1806         /* Start low level OSD */
1807         rc = osd_start(lsi, sb->s_flags);
1808         if (rc) {
1809                 CERROR("Unable to start osd on %s: %d\n",
1810                        lsi->lsi_lmd->lmd_dev, rc);
1811                 lustre_put_lsi(sb);
1812                 RETURN(rc);
1813         }
1814
1815         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
1816                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
1817
1818         if (class_name2obd(lsi->lsi_svname)) {
1819                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
1820                                    "running. Double-mount may have compromised"
1821                                    " the disk journal.\n",
1822                                    lsi->lsi_svname);
1823                 lustre_put_lsi(sb);
1824                 RETURN(-EALREADY);
1825         }
1826
1827         /* Start MGS before MGC */
1828         if (IS_MGS(lsi) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)) {
1829                 rc = server_start_mgs(sb);
1830                 if (rc)
1831                         GOTO(out_mnt, rc);
1832         }
1833
1834         /* Start MGC before servers */
1835         rc = lustre_start_mgc(sb);
1836         if (rc)
1837                 GOTO(out_mnt, rc);
1838
1839         /* Set up all obd devices for service */
1840         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1841             (IS_OST(lsi) || IS_MDT(lsi))) {
1842                 rc = server_start_targets(sb);
1843                 if (rc < 0) {
1844                         CERROR("Unable to start targets: %d\n", rc);
1845                         GOTO(out_mnt, rc);
1846                 }
1847                 /* FIXME overmount client here, or can we just start a
1848                  * client log and client_fill_super on this sb?  We
1849                  * need to make sure server_put_super gets called too
1850                  * - ll_put_super calls lustre_common_put_super; check
1851                  * there for LSI_SERVER flag, call s_p_s if so.
1852                  *
1853                  * Probably should start client from new thread so we
1854                  * can return.  Client will not finish until all
1855                  * servers are connected.  Note - MGS-only server does
1856                  * NOT get a client, since there is no lustre fs
1857                  * associated - the MGS is for all lustre fs's */
1858         }
1859
1860         rc = server_fill_super_common(sb);
1861         if (rc)
1862                 GOTO(out_mnt, rc);
1863
1864         RETURN(0);
1865 out_mnt:
1866         /* We jump here in case of failure while starting targets or MGS.
1867          * In this case we can't just put @mnt and have to do real cleanup
1868          * with stoping targets, etc. */
1869         server_put_super(sb);
1870         return rc;
1871 }
1872
1873 /*
1874  * Calculate timeout value for a target.
1875  */
1876 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
1877 {
1878         struct lustre_mount_data *lmd;
1879         int soft = 0;
1880         int hard = 0;
1881         int factor = 0;
1882         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
1883         int min = OBD_RECOVERY_TIME_MIN;
1884
1885         LASSERT(IS_SERVER(lsi));
1886
1887         lmd = lsi->lsi_lmd;
1888         if (lmd) {
1889                 soft   = lmd->lmd_recovery_time_soft;
1890                 hard   = lmd->lmd_recovery_time_hard;
1891                 has_ir = has_ir && !(lmd->lmd_flags & LMD_FLG_NOIR);
1892                 obd->obd_no_ir = !has_ir;
1893         }
1894
1895         if (soft == 0)
1896                 soft = OBD_RECOVERY_TIME_SOFT;
1897         if (hard == 0)
1898                 hard = OBD_RECOVERY_TIME_HARD;
1899
1900         /* target may have ir_factor configured. */
1901         factor = OBD_IR_FACTOR_DEFAULT;
1902         if (obd->obd_recovery_ir_factor)
1903                 factor = obd->obd_recovery_ir_factor;
1904
1905         if (has_ir) {
1906                 int new_soft = soft;
1907
1908                 /* adjust timeout value by imperative recovery */
1909                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
1910                 /* make sure the timeout is not too short */
1911                 new_soft = max(min, new_soft);
1912
1913                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery "
1914                               "window shrunk from %d-%d down to %d-%d\n",
1915                               obd->obd_name, soft, hard, new_soft, hard);
1916
1917                 soft = new_soft;
1918         } else {
1919                 LCONSOLE_INFO("%s: Imperative Recovery not enabled, recovery "
1920                               "window %d-%d\n", obd->obd_name, soft, hard);
1921         }
1922
1923         /* we're done */
1924         obd->obd_recovery_timeout   = max(obd->obd_recovery_timeout, soft);
1925         obd->obd_recovery_time_hard = hard;
1926         obd->obd_recovery_ir_factor = factor;
1927 }