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