Whamcloud - gitweb
LU-1187 lod: Fix config log and setup process for DNE
[fs/lustre-release.git] / lustre / obdclass / obd_mount.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) 2011, 2012, 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.c
37  *
38  * Client/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 <lustre_fsfilt.h>
52 #include <obd_class.h>
53 #include <lustre/lustre_user.h>
54 #include <linux/version.h>
55 #include <lustre_log.h>
56 #include <lustre_disk.h>
57 #include <lustre_param.h>
58 #ifdef HAVE_KERNEL_LOCKED
59 #include <linux/smp_lock.h>
60 #endif
61
62 static int (*client_fill_super)(struct super_block *sb,
63                                 struct vfsmount *mnt) = NULL;
64 static void (*kill_super_cb)(struct super_block *sb) = NULL;
65
66 /*********** mount lookup *********/
67
68 DEFINE_MUTEX(lustre_mount_info_lock);
69 static CFS_LIST_HEAD(server_mount_info_list);
70
71 static struct lustre_mount_info *server_find_mount(const char *name)
72 {
73         cfs_list_t *tmp;
74         struct lustre_mount_info *lmi;
75         ENTRY;
76
77         cfs_list_for_each(tmp, &server_mount_info_list) {
78                 lmi = cfs_list_entry(tmp, struct lustre_mount_info,
79                                      lmi_list_chain);
80                 if (strcmp(name, lmi->lmi_name) == 0)
81                         RETURN(lmi);
82         }
83         RETURN(NULL);
84 }
85
86 /* we must register an obd for a mount before we call the setup routine.
87    *_setup will call lustre_get_mount to get the mnt struct
88    by obd_name, since we can't pass the pointer to setup. */
89 static int server_register_mount(const char *name, struct super_block *sb,
90                           struct vfsmount *mnt)
91 {
92         struct lustre_mount_info *lmi;
93         char *name_cp;
94         ENTRY;
95
96         LASSERT(sb);
97
98         OBD_ALLOC(lmi, sizeof(*lmi));
99         if (!lmi)
100                 RETURN(-ENOMEM);
101         OBD_ALLOC(name_cp, strlen(name) + 1);
102         if (!name_cp) {
103                 OBD_FREE(lmi, sizeof(*lmi));
104                 RETURN(-ENOMEM);
105         }
106         strcpy(name_cp, name);
107
108         mutex_lock(&lustre_mount_info_lock);
109
110         if (server_find_mount(name)) {
111                 mutex_unlock(&lustre_mount_info_lock);
112                 OBD_FREE(lmi, sizeof(*lmi));
113                 OBD_FREE(name_cp, strlen(name) + 1);
114                 CERROR("Already registered %s\n", name);
115                 RETURN(-EEXIST);
116         }
117         lmi->lmi_name = name_cp;
118         lmi->lmi_sb = sb;
119         lmi->lmi_mnt = mnt;
120         cfs_list_add(&lmi->lmi_list_chain, &server_mount_info_list);
121
122         mutex_unlock(&lustre_mount_info_lock);
123
124         CDEBUG(D_MOUNT, "reg_mnt %p from %s\n", lmi->lmi_mnt, name);
125
126         RETURN(0);
127 }
128
129 /* when an obd no longer needs a mount */
130 static int server_deregister_mount(const char *name)
131 {
132         struct lustre_mount_info *lmi;
133         ENTRY;
134
135         mutex_lock(&lustre_mount_info_lock);
136         lmi = server_find_mount(name);
137         if (!lmi) {
138                 mutex_unlock(&lustre_mount_info_lock);
139                 CERROR("%s not registered\n", name);
140                 RETURN(-ENOENT);
141         }
142
143         CDEBUG(D_MOUNT, "dereg_mnt %p from %s\n", lmi->lmi_mnt, name);
144
145         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
146         cfs_list_del(&lmi->lmi_list_chain);
147         OBD_FREE(lmi, sizeof(*lmi));
148         mutex_unlock(&lustre_mount_info_lock);
149
150         RETURN(0);
151 }
152
153 /* obd's look up a registered mount using their obdname. This is just
154    for initial obd setup to find the mount struct.  It should not be
155    called every time you want to mntget. */
156 struct lustre_mount_info *server_get_mount(const char *name)
157 {
158         struct lustre_mount_info *lmi;
159         struct lustre_sb_info *lsi;
160         ENTRY;
161
162         mutex_lock(&lustre_mount_info_lock);
163         lmi = server_find_mount(name);
164         mutex_unlock(&lustre_mount_info_lock);
165         if (!lmi) {
166                 CERROR("Can't find mount for %s\n", name);
167                 RETURN(NULL);
168         }
169         lsi = s2lsi(lmi->lmi_sb);
170
171         cfs_atomic_inc(&lsi->lsi_mounts);
172
173         CDEBUG(D_MOUNT, "get_mnt %p from %s, refs=%d\n", lmi->lmi_mnt,
174                name, cfs_atomic_read(&lsi->lsi_mounts));
175
176         RETURN(lmi);
177 }
178 EXPORT_SYMBOL(server_get_mount);
179
180 /*
181  * Used by mdt to get mount_info from obdname.
182  * There are no blocking when using the mount_info.
183  * Do not use server_get_mount for this purpose.
184  */
185 struct lustre_mount_info *server_get_mount_2(const char *name)
186 {
187         struct lustre_mount_info *lmi;
188         ENTRY;
189
190         mutex_lock(&lustre_mount_info_lock);
191         lmi = server_find_mount(name);
192         mutex_unlock(&lustre_mount_info_lock);
193         if (!lmi)
194                 CERROR("Can't find mount for %s\n", name);
195
196         RETURN(lmi);
197 }
198 EXPORT_SYMBOL(server_get_mount_2);
199
200 static int lustre_put_lsi(struct super_block *sb);
201
202 /* to be called from obd_cleanup methods */
203 int server_put_mount(const char *name, struct vfsmount *mnt)
204 {
205         struct lustre_mount_info *lmi;
206         struct lustre_sb_info *lsi;
207         ENTRY;
208
209         mutex_lock(&lustre_mount_info_lock);
210         lmi = server_find_mount(name);
211         mutex_unlock(&lustre_mount_info_lock);
212         if (!lmi) {
213                 CERROR("Can't find mount for %s\n", name);
214                 RETURN(-ENOENT);
215         }
216         lsi = s2lsi(lmi->lmi_sb);
217
218         CDEBUG(D_MOUNT, "put_mnt %p from %s, refs=%d\n",
219                lmi->lmi_mnt, name, cfs_atomic_read(&lsi->lsi_mounts));
220
221         if (lustre_put_lsi(lmi->lmi_sb))
222                 CDEBUG(D_MOUNT, "Last put of mnt %p from %s\n",
223                        lmi->lmi_mnt, name);
224
225         /* this obd should never need the mount again */
226         server_deregister_mount(name);
227
228         RETURN(0);
229 }
230 EXPORT_SYMBOL(server_put_mount);
231
232 /* Corresponding to server_get_mount_2 */
233 int server_put_mount_2(const char *name, struct vfsmount *mnt)
234 {
235         ENTRY;
236         RETURN(0);
237 }
238 EXPORT_SYMBOL(server_put_mount_2);
239
240 /**************** config llog ********************/
241
242 /** Get a config log from the MGS and process it.
243  * This func is called for both clients and servers.
244  * Continue to process new statements appended to the logs
245  * (whenever the config lock is revoked) until lustre_end_log
246  * is called.
247  * @param sb The superblock is used by the MGC to write to the local copy of
248  *   the config log
249  * @param logname The name of the llog to replicate from the MGS
250  * @param cfg Since the same mgc may be used to follow multiple config logs
251  *   (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
252  *   this log, and is added to the mgc's list of logs to follow.
253  */
254 int lustre_process_log(struct super_block *sb, char *logname,
255                      struct config_llog_instance *cfg)
256 {
257         struct lustre_cfg *lcfg;
258         struct lustre_cfg_bufs *bufs;
259         struct lustre_sb_info *lsi = s2lsi(sb);
260         struct obd_device *mgc = lsi->lsi_mgc;
261         int rc;
262         ENTRY;
263
264         LASSERT(mgc);
265         LASSERT(cfg);
266
267         OBD_ALLOC_PTR(bufs);
268         if (bufs == NULL)
269                 RETURN(-ENOMEM);
270
271         /* mgc_process_config */
272         lustre_cfg_bufs_reset(bufs, mgc->obd_name);
273         lustre_cfg_bufs_set_string(bufs, 1, logname);
274         lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
275         lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
276         lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
277         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
278         lustre_cfg_free(lcfg);
279
280         OBD_FREE_PTR(bufs);
281
282         if (rc == -EINVAL)
283                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
284                                    "failed from the MGS (%d).  Make sure this "
285                                    "client and the MGS are running compatible "
286                                    "versions of Lustre.\n",
287                                    mgc->obd_name, logname, rc);
288
289         if (rc)
290                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
291                                    "failed (%d). This may be the result of "
292                                    "communication errors between this node and "
293                                    "the MGS, a bad configuration, or other "
294                                    "errors. See the syslog for more "
295                                    "information.\n", mgc->obd_name, logname,
296                                    rc);
297
298         /* class_obd_list(); */
299         RETURN(rc);
300 }
301 EXPORT_SYMBOL(lustre_process_log);
302
303 /* Stop watching this config log for updates */
304 int lustre_end_log(struct super_block *sb, char *logname,
305                        struct config_llog_instance *cfg)
306 {
307         struct lustre_cfg *lcfg;
308         struct lustre_cfg_bufs bufs;
309         struct lustre_sb_info *lsi = s2lsi(sb);
310         struct obd_device *mgc = lsi->lsi_mgc;
311         int rc;
312         ENTRY;
313
314         if (!mgc)
315                 RETURN(-ENOENT);
316
317         /* mgc_process_config */
318         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
319         lustre_cfg_bufs_set_string(&bufs, 1, logname);
320         if (cfg)
321                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
322         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
323         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
324         lustre_cfg_free(lcfg);
325         RETURN(rc);
326 }
327 EXPORT_SYMBOL(lustre_end_log);
328
329 /**************** obd start *******************/
330
331 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
332  * lctl (and do for echo cli/srv.
333  */
334 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
335             char *s1, char *s2, char *s3, char *s4)
336 {
337         struct lustre_cfg_bufs bufs;
338         struct lustre_cfg    * lcfg = NULL;
339         int rc;
340
341         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
342                cmd, s1, s2, s3, s4);
343
344         lustre_cfg_bufs_reset(&bufs, cfgname);
345         if (s1)
346                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
347         if (s2)
348                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
349         if (s3)
350                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
351         if (s4)
352                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
353
354         lcfg = lustre_cfg_new(cmd, &bufs);
355         lcfg->lcfg_nid = nid;
356         rc = class_process_config(lcfg);
357         lustre_cfg_free(lcfg);
358         return(rc);
359 }
360 EXPORT_SYMBOL(do_lcfg);
361
362 /** Call class_attach and class_setup.  These methods in turn call
363  * obd type-specific methods.
364  */
365 static int lustre_start_simple(char *obdname, char *type, char *uuid,
366                                char *s1, char *s2, char *s3, char *s4)
367 {
368         int rc;
369         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
370
371         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, 0, 0);
372         if (rc) {
373                 CERROR("%s attach error %d\n", obdname, rc);
374                 return(rc);
375         }
376         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
377         if (rc) {
378                 CERROR("%s setup error %d\n", obdname, rc);
379                 do_lcfg(obdname, 0, LCFG_DETACH, 0, 0, 0, 0);
380         }
381         return rc;
382 }
383
384 /* Set up a MGS to serve startup logs */
385 static int server_start_mgs(struct super_block *sb)
386 {
387         struct lustre_sb_info    *lsi = s2lsi(sb);
388         struct vfsmount          *mnt = lsi->lsi_srv_mnt;
389         struct lustre_mount_info *lmi;
390         int    rc = 0;
391         ENTRY;
392
393         /* It is impossible to have more than 1 MGS per node, since
394            MGC wouldn't know which to connect to */
395         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
396         if (lmi) {
397                 lsi = s2lsi(lmi->lmi_sb);
398                 LCONSOLE_ERROR_MSG(0x15d, "The MGS service was already started"
399                                    " from server\n");
400                 RETURN(-EALREADY);
401         }
402
403         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
404
405         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb, mnt);
406
407         if (!rc) {
408                 rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
409                                          LUSTRE_MGS_OBDNAME, 0, 0,
410                                          lsi->lsi_osd_obdname, 0);
411                 /* Do NOT call server_deregister_mount() here. This leads to
412                  * inability cleanup cleanly and free lsi and other stuff when
413                  * mgs calls server_put_mount() in error handling case. -umka */
414         }
415
416         if (rc)
417                 LCONSOLE_ERROR_MSG(0x15e, "Failed to start MGS '%s' (%d). "
418                                    "Is the 'mgs' module loaded?\n",
419                                    LUSTRE_MGS_OBDNAME, rc);
420         RETURN(rc);
421 }
422
423 static int server_stop_mgs(struct super_block *sb)
424 {
425         struct obd_device *obd;
426         int rc;
427         ENTRY;
428
429         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
430
431         /* There better be only one MGS */
432         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
433         if (!obd) {
434                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
435                 RETURN(-EALREADY);
436         }
437
438         /* The MGS should always stop when we say so */
439         obd->obd_force = 1;
440         rc = class_manual_cleanup(obd);
441         RETURN(rc);
442 }
443
444 DEFINE_MUTEX(mgc_start_lock);
445
446 /** Set up a mgc obd to process startup logs
447  *
448  * \param sb [in] super block of the mgc obd
449  *
450  * \retval 0 success, otherwise error code
451  */
452 static int lustre_start_mgc(struct super_block *sb)
453 {
454         struct obd_connect_data *data = NULL;
455         struct lustre_sb_info *lsi = s2lsi(sb);
456         struct obd_device *obd;
457         struct obd_export *exp;
458         struct obd_uuid *uuid;
459         class_uuid_t uuidc;
460         lnet_nid_t nid;
461         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
462         char *ptr;
463         int recov_bk;
464         int rc = 0, i = 0, j, len;
465         ENTRY;
466
467         LASSERT(lsi->lsi_lmd);
468
469         /* Find the first non-lo MGS nid for our MGC name */
470         if (IS_SERVER(lsi)) {
471                 /* mount -o mgsnode=nid */
472                 ptr = lsi->lsi_lmd->lmd_mgs;
473                 if (lsi->lsi_lmd->lmd_mgs &&
474                     (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
475                         i++;
476                 } else if (IS_MGS(lsi)) {
477                         lnet_process_id_t id;
478                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
479                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
480                                         continue;
481                                 nid = id.nid;
482                                 i++;
483                                 break;
484                         }
485                 }
486         } else { /* client */
487                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
488                 ptr = lsi->lsi_lmd->lmd_dev;
489                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
490                         i++;
491         }
492         if (i == 0) {
493                 CERROR("No valid MGS nids found.\n");
494                 RETURN(-EINVAL);
495         }
496
497         mutex_lock(&mgc_start_lock);
498
499         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(libcfs_nid2str(nid)) + 1;
500         OBD_ALLOC(mgcname, len);
501         OBD_ALLOC(niduuid, len + 2);
502         if (!mgcname || !niduuid)
503                 GOTO(out_free, rc = -ENOMEM);
504         sprintf(mgcname, "%s%s", LUSTRE_MGC_OBDNAME, libcfs_nid2str(nid));
505
506         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
507
508         OBD_ALLOC_PTR(data);
509         if (data == NULL)
510                 GOTO(out_free, rc = -ENOMEM);
511
512         obd = class_name2obd(mgcname);
513         if (obd && !obd->obd_stopping) {
514                 rc = obd_set_info_async(NULL, obd->obd_self_export,
515                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
516                                         strlen(mgssec), mgssec, NULL);
517                 if (rc)
518                         GOTO(out_free, rc);
519
520                 /* Re-using an existing MGC */
521                 cfs_atomic_inc(&obd->u.cli.cl_mgc_refcount);
522
523                 /* IR compatibility check, only for clients */
524                 if (lmd_is_client(lsi->lsi_lmd)) {
525                         int has_ir;
526                         int vallen = sizeof(*data);
527                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
528
529                         rc = obd_get_info(NULL, obd->obd_self_export,
530                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
531                                           &vallen, data, NULL);
532                         LASSERT(rc == 0);
533                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
534                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
535                                 /* LMD_FLG_NOIR is for test purpose only */
536                                 LCONSOLE_WARN(
537                                     "Trying to mount a client with IR setting "
538                                     "not compatible with current mgc. "
539                                     "Force to use current mgc setting that is "
540                                     "IR %s.\n",
541                                     has_ir ? "enabled" : "disabled");
542                                 if (has_ir)
543                                         *flags &= ~LMD_FLG_NOIR;
544                                 else
545                                         *flags |= LMD_FLG_NOIR;
546                         }
547                 }
548
549                 recov_bk = 0;
550                 /* If we are restarting the MGS, don't try to keep the MGC's
551                    old connection, or registration will fail. */
552                 if (IS_MGS(lsi)) {
553                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
554                         recov_bk = 1;
555                 }
556
557                 /* Try all connections, but only once (again).
558                    We don't want to block another target from starting
559                    (using its local copy of the log), but we do want to connect
560                    if at all possible. */
561                 recov_bk++;
562                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
563                 rc = obd_set_info_async(NULL, obd->obd_self_export,
564                                         sizeof(KEY_INIT_RECOV_BACKUP),
565                                         KEY_INIT_RECOV_BACKUP,
566                                         sizeof(recov_bk), &recov_bk, NULL);
567                 GOTO(out, rc = 0);
568         }
569
570         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
571
572         /* Add the primary nids for the MGS */
573         i = 0;
574         sprintf(niduuid, "%s_%x", mgcname, i);
575         if (IS_SERVER(lsi)) {
576                 ptr = lsi->lsi_lmd->lmd_mgs;
577                 if (IS_MGS(lsi)) {
578                         /* Use local nids (including LO) */
579                         lnet_process_id_t id;
580                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
581                                 rc = do_lcfg(mgcname, id.nid,
582                                              LCFG_ADD_UUID, niduuid, 0,0,0);
583                         }
584                 } else {
585                         /* Use mgsnode= nids */
586                         /* mount -o mgsnode=nid */
587                         if (lsi->lsi_lmd->lmd_mgs) {
588                                 ptr = lsi->lsi_lmd->lmd_mgs;
589                         } else if (class_find_param(ptr, PARAM_MGSNODE,
590                                                     &ptr) != 0) {
591                                 CERROR("No MGS nids given.\n");
592                                 GOTO(out_free, rc = -EINVAL);
593                         }
594                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
595                                 rc = do_lcfg(mgcname, nid,
596                                              LCFG_ADD_UUID, niduuid, 0,0,0);
597                                 i++;
598                         }
599                 }
600         } else { /* client */
601                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
602                 ptr = lsi->lsi_lmd->lmd_dev;
603                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
604                         rc = do_lcfg(mgcname, nid,
605                                      LCFG_ADD_UUID, niduuid, 0,0,0);
606                         i++;
607                         /* Stop at the first failover nid */
608                         if (*ptr == ':')
609                                 break;
610                 }
611         }
612         if (i == 0) {
613                 CERROR("No valid MGS nids found.\n");
614                 GOTO(out_free, rc = -EINVAL);
615         }
616         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
617
618         /* Random uuid for MGC allows easier reconnects */
619         OBD_ALLOC_PTR(uuid);
620         ll_generate_random_uuid(uuidc);
621         class_uuid_unparse(uuidc, uuid);
622
623         /* Start the MGC */
624         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
625                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
626                                  niduuid, 0, 0);
627         OBD_FREE_PTR(uuid);
628         if (rc)
629                 GOTO(out_free, rc);
630
631         /* Add any failover MGS nids */
632         i = 1;
633         while (ptr && ((*ptr == ':' ||
634                class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
635                 /* New failover node */
636                 sprintf(niduuid, "%s_%x", mgcname, i);
637                 j = 0;
638                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
639                         j++;
640                         rc = do_lcfg(mgcname, nid,
641                                      LCFG_ADD_UUID, niduuid, 0,0,0);
642                         if (*ptr == ':')
643                                 break;
644                 }
645                 if (j > 0) {
646                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
647                                      niduuid, 0, 0, 0);
648                         i++;
649                 } else {
650                         /* at ":/fsname" */
651                         break;
652                 }
653         }
654         lsi->lsi_lmd->lmd_mgs_failnodes = i;
655
656         obd = class_name2obd(mgcname);
657         if (!obd) {
658                 CERROR("Can't find mgcobd %s\n", mgcname);
659                 GOTO(out_free, rc = -ENOTCONN);
660         }
661
662         rc = obd_set_info_async(NULL, obd->obd_self_export,
663                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
664                                 strlen(mgssec), mgssec, NULL);
665         if (rc)
666                 GOTO(out_free, rc);
667
668         /* Keep a refcount of servers/clients who started with "mount",
669            so we know when we can get rid of the mgc. */
670         cfs_atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
671
672         /* Try all connections, but only once. */
673         recov_bk = 1;
674         rc = obd_set_info_async(NULL, obd->obd_self_export,
675                                 sizeof(KEY_INIT_RECOV_BACKUP),
676                                 KEY_INIT_RECOV_BACKUP,
677                                 sizeof(recov_bk), &recov_bk, NULL);
678         if (rc)
679                 /* nonfatal */
680                 CWARN("can't set %s %d\n", KEY_INIT_RECOV_BACKUP, rc);
681
682         /* We connect to the MGS at setup, and don't disconnect until cleanup */
683         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
684                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
685                                   OBD_CONNECT_LVB_TYPE;
686
687 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
688         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
689 #else
690 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
691 #endif
692
693         if (lmd_is_client(lsi->lsi_lmd) &&
694             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
695                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
696         data->ocd_version = LUSTRE_VERSION_CODE;
697         rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
698         if (rc) {
699                 CERROR("connect failed %d\n", rc);
700                 GOTO(out, rc);
701         }
702
703         obd->u.cli.cl_mgc_mgsexp = exp;
704
705 out:
706         /* Keep the mgc info in the sb. Note that many lsi's can point
707            to the same mgc.*/
708         lsi->lsi_mgc = obd;
709 out_free:
710         mutex_unlock(&mgc_start_lock);
711
712         if (data)
713                 OBD_FREE_PTR(data);
714         if (mgcname)
715                 OBD_FREE(mgcname, len);
716         if (niduuid)
717                 OBD_FREE(niduuid, len + 2);
718         RETURN(rc);
719 }
720
721 static int lustre_stop_mgc(struct super_block *sb)
722 {
723         struct lustre_sb_info *lsi = s2lsi(sb);
724         struct obd_device *obd;
725         char *niduuid = 0, *ptr = 0;
726         int i, rc = 0, len = 0;
727         ENTRY;
728
729         if (!lsi)
730                 RETURN(-ENOENT);
731         obd = lsi->lsi_mgc;
732         if (!obd)
733                 RETURN(-ENOENT);
734         lsi->lsi_mgc = NULL;
735
736         mutex_lock(&mgc_start_lock);
737         LASSERT(cfs_atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
738         if (!cfs_atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
739                 /* This is not fatal, every client that stops
740                    will call in here. */
741                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
742                        cfs_atomic_read(&obd->u.cli.cl_mgc_refcount));
743                 GOTO(out, rc = -EBUSY);
744         }
745
746         /* The MGC has no recoverable data in any case.
747          * force shotdown set in umount_begin */
748         obd->obd_no_recov = 1;
749
750         if (obd->u.cli.cl_mgc_mgsexp) {
751                 /* An error is not fatal, if we are unable to send the
752                    disconnect mgs ping evictor cleans up the export */
753                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
754                 if (rc)
755                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
756         }
757
758         /* Save the obdname for cleaning the nid uuids, which are
759            obdname_XX */
760         len = strlen(obd->obd_name) + 6;
761         OBD_ALLOC(niduuid, len);
762         if (niduuid) {
763                 strcpy(niduuid, obd->obd_name);
764                 ptr = niduuid + strlen(niduuid);
765         }
766
767         rc = class_manual_cleanup(obd);
768         if (rc)
769                 GOTO(out, rc);
770
771         /* Clean the nid uuids */
772         if (!niduuid)
773                 GOTO(out, rc = -ENOMEM);
774
775         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
776                 sprintf(ptr, "_%x", i);
777                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
778                              niduuid, 0, 0, 0);
779                 if (rc)
780                         CERROR("del MDC UUID %s failed: rc = %d\n",
781                                niduuid, rc);
782         }
783 out:
784         if (niduuid)
785                 OBD_FREE(niduuid, len);
786
787         /* class_import_put will get rid of the additional connections */
788         mutex_unlock(&mgc_start_lock);
789         RETURN(rc);
790 }
791
792 /* Since there's only one mgc per node, we have to change it's fs to get
793    access to the right disk. */
794 static int server_mgc_set_fs(struct obd_device *mgc, struct super_block *sb)
795 {
796         struct lustre_sb_info *lsi = s2lsi(sb);
797         int rc;
798         ENTRY;
799
800         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
801
802         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
803         rc = obd_set_info_async(NULL, mgc->obd_self_export,
804                                 sizeof(KEY_SET_FS), KEY_SET_FS,
805                                 sizeof(*sb), sb, NULL);
806         if (rc) {
807                 CERROR("can't set_fs %d\n", rc);
808         }
809
810         RETURN(rc);
811 }
812
813 static int server_mgc_clear_fs(struct obd_device *mgc)
814 {
815         int rc;
816         ENTRY;
817
818         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
819
820         rc = obd_set_info_async(NULL, mgc->obd_self_export,
821                                 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
822                                 0, NULL, NULL);
823         RETURN(rc);
824 }
825
826 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
827  * @param [in] svname server name including type and index
828  * @param [out] fsname Buffer to copy filesystem name prefix into.
829  *  Must have at least 'strlen(fsname) + 1' chars.
830  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
831  * rc < 0  on error
832  */
833 int server_name2fsname(char *svname, char *fsname, char **endptr)
834 {
835         char *dash = strrchr(svname, '-');
836         if (!dash) {
837                 dash = strrchr(svname, ':');
838                 if (!dash)
839                         return -EINVAL;
840         }
841
842         /* interpret <fsname>-MDTXXXXX-mdc as mdt, the better way is to pass
843          * in the fsname, then determine the server index */
844         if (!strcmp(LUSTRE_MDC_NAME, dash + 1)) {
845                 dash--;
846                 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
847                         ;
848                 if (dash == svname)
849                         return -EINVAL;
850         }
851
852         if (fsname != NULL) {
853                 strncpy(fsname, svname, dash - svname);
854                 fsname[dash - svname] = '\0';
855         }
856
857         if (endptr != NULL)
858                 *endptr = dash;
859
860         return 0;
861 }
862 EXPORT_SYMBOL(server_name2fsname);
863
864 static int is_mdc_device(char *devname)
865 {
866         char *ptr;
867         ptr = strrchr(devname, '-');
868         if (ptr != NULL && strcmp(ptr, "-mdc") == 0)
869                 return 1;
870         return 0;
871 }
872
873 static int inline tgt_is_mdt0(char *tgtname)
874 {
875         __u32 idx;
876         int   type;
877
878         type = server_name2index(tgtname, &idx, NULL);
879         if (type != LDD_F_SV_TYPE_MDT)
880                 return 0;
881
882         return (idx == 0) ? 1 :0;
883 }
884
885 static int inline is_mdc_for_mdt0(char *devname)
886 {
887         char   *ptr;
888
889         if (!is_mdc_device(devname))
890                 return 0;
891
892         ptr = strrchr(devname, '-');
893         if (ptr == NULL)
894                 return 0;
895
896         *ptr = 0;
897         if (tgt_is_mdt0(devname)) {
898                 *ptr = '-';
899                 return 1;
900         }
901         *ptr = '-';
902         return 0;
903 }
904
905 /**
906  * Convert OST/MDT name(fsname-OSTxxxx) to an osp name
907  * (fsname-MDT0000-osp-OSTxxxx), which will be used to
908  * communicate with MDT0 for this target.
909  **/
910 int tgt_name2ospname(char *svname, char *ospname)
911 {
912         char *fsname;
913         char *tgt;
914         int   rc;
915         ENTRY;
916
917         OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
918         if (fsname == NULL)
919                 RETURN(-ENOMEM);
920
921         rc = server_name2fsname(svname, fsname, &tgt);
922         if (rc != 0) {
923                 CERROR("%s change fsname error: rc %d\n", svname, rc);
924                 GOTO(cleanup, rc);
925         }
926
927         if (*tgt != '-' && *tgt != ':') {
928                 CERROR("%s wrong svname name!\n", svname);
929                 GOTO(cleanup, rc = -EINVAL);
930         }
931
932         tgt++;
933         if (strncmp(tgt, "OST", 3) != 0 && strncmp(tgt, "MDT", 3) != 0) {
934                 CERROR("%s is not an OST or MDT target!\n", svname);
935                 GOTO(cleanup, rc = -EINVAL);
936         }
937         sprintf(ospname, "%s-MDT0000-%s-%s", fsname, LUSTRE_OSP_NAME, tgt);
938 cleanup:
939         if (fsname != NULL)
940                 OBD_FREE(fsname, MTI_NAME_MAXLEN);
941         RETURN(rc);
942 }
943 EXPORT_SYMBOL(tgt_name2ospname);
944
945 static CFS_LIST_HEAD(osp_register_list);
946 DEFINE_MUTEX(osp_register_list_lock);
947
948 int lustre_register_osp_item(char *ospname, struct obd_export **exp,
949                              register_osp_cb cb_func, void *cb_data)
950 {
951         struct obd_device        *osp;
952         struct osp_register_item *ori;
953         ENTRY;
954
955         LASSERTF(strlen(ospname) < MTI_NAME_MAXLEN, "ospname is too long %s\n",
956                  ospname);
957         LASSERT(exp != NULL && *exp == NULL);
958
959         OBD_ALLOC_PTR(ori);
960         if (ori == NULL)
961                 RETURN(-ENOMEM);
962
963         mutex_lock(&osp_register_list_lock);
964
965         osp = class_name2obd(ospname);
966         if (osp != NULL && osp->obd_set_up == 1) {
967                 struct obd_uuid *uuid;
968
969                 OBD_ALLOC_PTR(uuid);
970                 if (uuid == NULL) {
971                         mutex_unlock(&osp_register_list_lock);
972                         RETURN(-ENOMEM);
973                 }
974                 memcpy(uuid->uuid, ospname, strlen(ospname));
975                 *exp = cfs_hash_lookup(osp->obd_uuid_hash, uuid);
976                 OBD_FREE_PTR(uuid);
977         }
978
979         memcpy(ori->ori_name, ospname, strlen(ospname));
980         ori->ori_exp = exp;
981         ori->ori_cb_func = cb_func;
982         ori->ori_cb_data = cb_data;
983         CFS_INIT_LIST_HEAD(&ori->ori_list);
984         cfs_list_add(&ori->ori_list, &osp_register_list);
985
986         if (*exp != NULL && cb_func != NULL)
987                 cb_func(cb_data);
988
989         mutex_unlock(&osp_register_list_lock);
990         RETURN(0);
991 }
992 EXPORT_SYMBOL(lustre_register_osp_item);
993
994 void lustre_deregister_osp_item(struct obd_export **exp)
995 {
996         struct osp_register_item *ori, *tmp;
997
998         mutex_lock(&osp_register_list_lock);
999         cfs_list_for_each_entry_safe(ori, tmp, &osp_register_list, ori_list) {
1000                 if (exp == ori->ori_exp) {
1001                         if (*exp)
1002                                 class_export_put(*exp);
1003                         cfs_list_del(&ori->ori_list);
1004                         OBD_FREE_PTR(ori);
1005                         break;
1006                 }
1007         }
1008         mutex_unlock(&osp_register_list_lock);
1009 }
1010 EXPORT_SYMBOL(lustre_deregister_osp_item);
1011
1012 static void lustre_notify_osp_list(struct obd_export *exp)
1013 {
1014         struct osp_register_item *ori, *tmp;
1015         LASSERT(exp != NULL);
1016
1017         mutex_lock(&osp_register_list_lock);
1018         cfs_list_for_each_entry_safe(ori, tmp, &osp_register_list, ori_list) {
1019                 if (strcmp(exp->exp_obd->obd_name, ori->ori_name))
1020                         continue;
1021                 if (*ori->ori_exp != NULL)
1022                         continue;
1023                 *ori->ori_exp = class_export_get(exp);
1024                 if (ori->ori_cb_func != NULL)
1025                         ori->ori_cb_func(ori->ori_cb_data);
1026         }
1027         mutex_unlock(&osp_register_list_lock);
1028 }
1029
1030 static int lustre_osp_connect(struct obd_device *osp)
1031 {
1032         struct lu_env            env;
1033         struct lu_context        session_ctx;
1034         struct obd_export       *exp;
1035         struct obd_uuid         *uuid = NULL;
1036         struct obd_connect_data *data = NULL;
1037         int                      rc;
1038         ENTRY;
1039
1040         /* log has been fully processed, let clients connect */
1041         rc = lu_env_init(&env, osp->obd_lu_dev->ld_type->ldt_ctx_tags);
1042         if (rc != 0)
1043                 RETURN(rc);
1044
1045         lu_context_init(&session_ctx, LCT_SESSION);
1046         session_ctx.lc_thread = NULL;
1047         lu_context_enter(&session_ctx);
1048         env.le_ses = &session_ctx;
1049
1050         OBD_ALLOC_PTR(data);
1051         if (data == NULL)
1052                 GOTO(out, rc = -ENOMEM);
1053
1054         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
1055         data->ocd_version = LUSTRE_VERSION_CODE;
1056         data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
1057         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_IBITS |
1058                                    OBD_CONNECT_MDS_MDS | OBD_CONNECT_FID |
1059                                    OBD_CONNECT_AT | OBD_CONNECT_FULL20 |
1060                                    OBD_CONNECT_LVB_TYPE |
1061                                    OBD_CONNECT_LIGHTWEIGHT;
1062         OBD_ALLOC_PTR(uuid);
1063         if (uuid == NULL)
1064                 GOTO(out, rc = -ENOMEM);
1065
1066         if (strlen(osp->obd_name) > sizeof(uuid->uuid)) {
1067                 CERROR("%s: Too long osp name %s, max_size is %d\n",
1068                        osp->obd_name, osp->obd_name, (int)sizeof(uuid->uuid));
1069                 GOTO(out, rc = -EINVAL);
1070         }
1071         /* Use osp name as the uuid, so we find the export by
1072          * osp name later */
1073         memcpy(uuid->uuid, osp->obd_name, strlen(osp->obd_name));
1074         rc = obd_connect(&env, &exp, osp, uuid, data, NULL);
1075         if (rc != 0)
1076                 CERROR("%s: connect failed: rc = %d\n", osp->obd_name, rc);
1077         else
1078                 lustre_notify_osp_list(exp);
1079
1080 out:
1081         if (data != NULL)
1082                 OBD_FREE_PTR(data);
1083         if (uuid != NULL)
1084                 OBD_FREE_PTR(uuid);
1085
1086         lu_env_fini(&env);
1087         lu_context_exit(&session_ctx);
1088         lu_context_fini(&session_ctx);
1089
1090         RETURN(rc);
1091 }
1092
1093 /**
1094  * osp-on-ost is used by slaves (Non-MDT0 targets) to manage the connection
1095  * to MDT0.
1096  *
1097  * The OSTs will communicate with MDT0 by the connection established by the
1098  * osp-on-ost to get quota and fid sequence.
1099  *
1100  **/
1101 static int lustre_osp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi)
1102 {
1103         struct obd_connect_data *data = NULL;
1104         struct obd_device       *obd;
1105         char                    *ospname = NULL;
1106         char                    *ospuuid = NULL;
1107         int                      rc;
1108         ENTRY;
1109
1110         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
1111                             lcfg->lcfg_nid);
1112         if (rc) {
1113                 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
1114                 GOTO(out, rc);
1115         }
1116
1117         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1118         if (ospname == NULL)
1119                 GOTO(out, rc = -ENOMEM);
1120
1121         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1122         if (rc != 0) {
1123                 CERROR("%s change ospname error: rc %d\n",
1124                        lsi->lsi_svname, rc);
1125                 GOTO(out, rc);
1126         }
1127
1128         OBD_ALLOC(ospuuid, MTI_NAME_MAXLEN);
1129         if (ospuuid == NULL)
1130                 GOTO(out, rc = -ENOMEM);
1131
1132         sprintf(ospuuid, "%s_UUID", ospname);
1133         rc = lustre_start_simple(ospname, LUSTRE_OSP_NAME,
1134                                  ospuuid, lustre_cfg_string(lcfg, 1),
1135                                  0, 0, 0);
1136         if (rc) {
1137                 CERROR("%s: setup up failed: rc %d\n", ospname, rc);
1138                 GOTO(out, rc);
1139         }
1140
1141         obd = class_name2obd(ospname);
1142         LASSERT(obd != NULL);
1143
1144         rc = lustre_osp_connect(obd);
1145         if (rc != 0)
1146                 CERROR("%s: connect failed: rc = %d\n", ospname, rc);
1147 out:
1148         if (data != NULL)
1149                 OBD_FREE_PTR(data);
1150         if (ospname != NULL)
1151                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1152         if (ospuuid != NULL)
1153                 OBD_FREE(ospuuid, MTI_NAME_MAXLEN);
1154
1155         RETURN(rc);
1156 }
1157
1158 static int lustre_osp_add_conn(struct lustre_cfg *cfg,
1159                                struct lustre_sb_info *lsi)
1160 {
1161         struct lustre_cfg_bufs *bufs = NULL;
1162         struct lustre_cfg      *lcfg = NULL;
1163         char                   *ospname = NULL;
1164         struct obd_device      *osp;
1165         int                     rc;
1166         ENTRY;
1167
1168         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1169         if (ospname == NULL)
1170                 GOTO(out, rc = -ENOMEM);
1171
1172         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1173         if (rc != 0) {
1174                 CERROR("%s change ospname error: rc %d\n",
1175                        lsi->lsi_svname, rc);
1176                 GOTO(out, rc);
1177         }
1178
1179         OBD_ALLOC_PTR(bufs);
1180         if (bufs == NULL)
1181                 RETURN(-ENOMEM);
1182
1183         lustre_cfg_bufs_reset(bufs, ospname);
1184         lustre_cfg_bufs_set_string(bufs, 1,
1185                                    lustre_cfg_string(cfg, 1));
1186
1187         lcfg = lustre_cfg_new(LCFG_ADD_CONN, bufs);
1188
1189         osp = class_name2obd(ospname);
1190         if (osp == NULL) {
1191                 CERROR("Can not find %s\n", ospname);
1192                 GOTO(out, rc = -EINVAL);
1193         }
1194
1195         rc = class_add_conn(osp, lcfg);
1196         if (rc)
1197                 CERROR("%s: can't add conn: rc = %d\n", ospname, rc);
1198
1199 out:
1200         if (bufs != NULL)
1201                 OBD_FREE_PTR(bufs);
1202         if (lcfg != NULL)
1203                 lustre_cfg_free(lcfg);
1204         if (ospname != NULL)
1205                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1206
1207         RETURN(rc);
1208 }
1209
1210 /**
1211  * Retrieve MDT nids from the client log, then start the osp-on-ost device.
1212  * there are only two scenarios which would include mdt nid.
1213  * 1.
1214  * marker   5 (flags=0x01, v2.1.54.0) lustre-MDT0000  'add mdc' xxx-
1215  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:192.168.122.162@tcp
1216  * attach    0:lustre-MDT0000-mdc  1:mdc  2:lustre-clilmv_UUID
1217  * setup     0:lustre-MDT0000-mdc  1:lustre-MDT0000_UUID  2:192.168.122.162@tcp
1218  * add_uuid  nid=192.168.172.1@tcp(0x20000c0a8ac01)  0:  1:192.168.172.1@tcp
1219  * add_conn  0:lustre-MDT0000-mdc  1:192.168.172.1@tcp
1220  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID xxxx
1221  * marker   5 (flags=0x02, v2.1.54.0) lustre-MDT0000  'add mdc' xxxx-
1222  * 2.
1223  * marker   7 (flags=0x01, v2.1.54.0) lustre-MDT0000  'add failnid' xxxx-
1224  * add_uuid  nid=192.168.122.2@tcp(0x20000c0a87a02)  0:  1:192.168.122.2@tcp
1225  * add_conn  0:lustre-MDT0000-mdc  1:192.168.122.2@tcp
1226  * marker   7 (flags=0x02, v2.1.54.0) lustre-MDT0000  'add failnid' xxxx-
1227 **/
1228 static int client_osp_config_process(const struct lu_env *env,
1229                                      struct llog_handle *handle,
1230                                      struct llog_rec_hdr *rec, void *data)
1231 {
1232         struct config_llog_instance *clli = data;
1233         int                          cfg_len = rec->lrh_len;
1234         char                        *cfg_buf = (char *) (rec + 1);
1235         struct lustre_cfg           *lcfg = NULL;
1236         struct lustre_sb_info       *lsi;
1237         int                          rc = 0, swab = 0;
1238         ENTRY;
1239
1240         if (rec->lrh_type != OBD_CFG_REC) {
1241                 CERROR("Unknown llog record type %#x encountered\n",
1242                        rec->lrh_type);
1243                 RETURN(-EINVAL);
1244         }
1245
1246         LASSERT(clli->cfg_sb != NULL);
1247         lsi = s2lsi(clli->cfg_sb);
1248
1249         lcfg = (struct lustre_cfg *)cfg_buf;
1250         if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1251                 lustre_swab_lustre_cfg(lcfg);
1252                 swab = 1;
1253         }
1254
1255         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1256         if (rc)
1257                 GOTO(out, rc);
1258
1259         switch (lcfg->lcfg_command) {
1260         case LCFG_MARKER: {
1261                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1262
1263                 lustre_swab_cfg_marker(marker, swab,
1264                                        LUSTRE_CFG_BUFLEN(lcfg, 1));
1265                 if (marker->cm_flags & CM_SKIP ||
1266                     marker->cm_flags & CM_EXCLUDE)
1267                         GOTO(out, rc = 0);
1268
1269                 if (!tgt_is_mdt0(marker->cm_tgtname))
1270                         GOTO(out, rc = 0);
1271
1272                 if(!strncmp(marker->cm_comment, "add mdc", 7) ||
1273                    !strncmp(marker->cm_comment, "add failnid", 11)) {
1274                         if (marker->cm_flags & CM_START) {
1275                                 clli->cfg_flags = CFG_F_MARKER;
1276                                 /* This hack is to differentiate the
1277                                  * ADD_UUID is come from "add mdc" record
1278                                  * or from "add failnid" record. */
1279                                 if (!strncmp(marker->cm_comment,
1280                                              "add failnid", 11))
1281                                         clli->cfg_flags |= CFG_F_SKIP;
1282                         } else if (marker->cm_flags & CM_END) {
1283                                 clli->cfg_flags = 0;
1284                         }
1285                 }
1286                 break;
1287         }
1288         case LCFG_ADD_UUID: {
1289                 if (clli->cfg_flags == CFG_F_MARKER) {
1290                         rc = lustre_osp_setup(lcfg, lsi);
1291                         /* XXX: process only the first nid as
1292                          * we don't need another instance of osp */
1293                         clli->cfg_flags |= CFG_F_SKIP;
1294                 } else if (clli->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
1295                         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
1296                                         lcfg->lcfg_nid);
1297                         if (rc)
1298                                 CERROR("%s: Fail to add uuid, rc:%d\n",
1299                                        lsi->lsi_svname, rc);
1300                 }
1301                 break;
1302         }
1303         case LCFG_ADD_CONN: {
1304                 if (is_mdc_for_mdt0(lustre_cfg_string(lcfg, 0)))
1305                         rc = lustre_osp_add_conn(lcfg, lsi);
1306                 break;
1307         }
1308         default:
1309                 break;
1310         }
1311 out:
1312         RETURN(rc);
1313 }
1314
1315 static int lustre_disconnect_osp(struct super_block *sb)
1316 {
1317         struct lustre_sb_info           *lsi = s2lsi(sb);
1318         struct obd_device               *osp;
1319         char                            *ospname = NULL;
1320         char                            *logname = NULL;
1321         struct lustre_cfg               *lcfg = NULL;
1322         struct lustre_cfg_bufs          *bufs = NULL;
1323         struct config_llog_instance     *cfg = NULL;
1324         int                              rc;
1325         ENTRY;
1326
1327         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1328         if (IS_MDT(lsi)) {
1329                 int     index;
1330
1331                 /* Only disconnect MDT0-osp-MDT0 here, other osp on MDT
1332                  * will be disconnect during MDT stack cleanup.
1333                  * FIXME: remove later when quota on DNE is finished */
1334                 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1335                 if (rc < 0)
1336                         RETURN(rc);
1337                 if (index != 0)
1338                         RETURN(0);
1339         }
1340         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1341         if (logname == NULL)
1342                 RETURN(-ENOMEM);
1343
1344         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1345         if (ospname == NULL)
1346                 GOTO(out, rc = -ENOMEM);
1347
1348         rc = server_name2fsname(lsi->lsi_svname, ospname, NULL);
1349         if (rc != 0) {
1350                 CERROR("%s: get fsname error: %d\n",
1351                        lsi->lsi_svname, rc);
1352                 GOTO(out, rc);
1353         }
1354         sprintf(logname, "%s-client", ospname);
1355
1356         OBD_ALLOC_PTR(cfg);
1357         if (cfg == NULL)
1358                 GOTO(out, rc = -ENOMEM);
1359
1360         /* end log first */
1361         cfg->cfg_instance = sb;
1362         rc = lustre_end_log(sb, logname, cfg);
1363         if (rc != 0) {
1364                 CERROR("Can't end config log %s\n", ospname);
1365                 GOTO(out, rc);
1366         }
1367
1368         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1369         if (rc != 0) {
1370                 CERROR("%s: get osp name error: %d\n",
1371                        lsi->lsi_svname, rc);
1372                 GOTO(out, rc);
1373         }
1374
1375         osp = class_name2obd(ospname);
1376         if (osp == NULL) {
1377                 CERROR("Can't find osp-on-ost %s\n", ospname);
1378                 GOTO(out, rc = -ENOENT);
1379         }
1380
1381         OBD_ALLOC_PTR(bufs);
1382         if (bufs == NULL)
1383                 GOTO(out, rc = -ENOMEM);
1384
1385         lustre_cfg_bufs_reset(bufs, osp->obd_name);
1386         lustre_cfg_bufs_set_string(bufs, 1, NULL);
1387         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
1388         if (!lcfg)
1389                 GOTO(out, rc = -ENOMEM);
1390
1391         /* Disconnect import first. NULL is passed for the '@env', since
1392          * it will not be used for the 'osp-on-ost'. (see osp_shutdown()) */
1393         rc = osp->obd_lu_dev->ld_ops->ldo_process_config(NULL, osp->obd_lu_dev,
1394                                                          lcfg);
1395 out:
1396         if (lcfg)
1397                 lustre_cfg_free(lcfg);
1398         if (bufs)
1399                 OBD_FREE_PTR(bufs);
1400         if (cfg)
1401                 OBD_FREE_PTR(cfg);
1402         if (ospname)
1403                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1404         if (logname)
1405                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1406         RETURN(rc);
1407 }
1408
1409 /**
1410  * Stop the osp(fsname-MDT0000-osp-OSTxxxx) or (fsname-MDT0000-osp-MDT0000) for an OST target.
1411  **/
1412 static int lustre_stop_osp(struct super_block *sb)
1413 {
1414         struct lustre_sb_info   *lsi = s2lsi(sb);
1415         struct obd_device       *osp = NULL;
1416         char                    *ospname = NULL;
1417         int                      rc = 0;
1418         ENTRY;
1419
1420         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1421         if (IS_MDT(lsi)) {
1422                 int     index;
1423
1424                 /* Only disconnect MDT0-osp-MDT0 here, other osp on MDT
1425                  * will be disconnect during MDT stack cleanup.
1426                  * FIXME: remove later when quota on DNE is finished */
1427                 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1428                 if (rc < 0)
1429                         RETURN(rc);
1430                 if (index != 0)
1431                         RETURN(0);
1432         }
1433
1434         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1435         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1436         if (rc != 0) {
1437                 CERROR("%s get fsname error: rc %d\n",
1438                        lsi->lsi_svname, rc);
1439                 GOTO(cleanup, rc);
1440         }
1441
1442         osp = class_name2obd(ospname);
1443         if (osp == NULL) {
1444                 CERROR("Can not find osp-on-ost %s\n", ospname);
1445                 GOTO(cleanup, rc = -ENOENT);
1446         }
1447
1448         osp->obd_force = 1;
1449         rc = class_manual_cleanup(osp);
1450
1451 cleanup:
1452         if (ospname != NULL)
1453                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1454         RETURN(rc);
1455 }
1456
1457 DEFINE_MUTEX(server_start_lock);
1458
1459 /* Stop MDS/OSS if nobody is using them */
1460 static int server_stop_servers(int lsiflags)
1461 {
1462         struct obd_device *obd = NULL;
1463         struct obd_type *type = NULL;
1464         int rc = 0;
1465         ENTRY;
1466
1467         mutex_lock(&server_start_lock);
1468
1469         /* Either an MDT or an OST or neither  */
1470         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1471         if ((lsiflags & LDD_F_SV_TYPE_MDT) &&
1472             (obd = class_name2obd(LUSTRE_MDS_OBDNAME))) {
1473                 type = class_search_type(LUSTRE_MDT_NAME);
1474         }
1475        /* if this was an OST, and there are no more OST's, clean up the OSS */
1476         if ((lsiflags & LDD_F_SV_TYPE_OST) &&
1477             (obd = class_name2obd(LUSTRE_OSS_OBDNAME))) {
1478                 type = class_search_type(LUSTRE_OST_NAME);
1479         }
1480
1481         if (obd && (!type || !type->typ_refcnt)) {
1482                 int err;
1483                 obd->obd_force = 1;
1484                 /* obd_fail doesn't mean much on a server obd */
1485                 err = class_manual_cleanup(obd);
1486                 if (!rc)
1487                         rc = err;
1488         }
1489
1490         mutex_unlock(&server_start_lock);
1491
1492         RETURN(rc);
1493 }
1494
1495 int server_mti_print(char *title, struct mgs_target_info *mti)
1496 {
1497         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1498         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1499         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
1500         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
1501         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
1502                   mti->mti_config_ver, mti->mti_flags);
1503         return(0);
1504 }
1505
1506 /**
1507  * Get service name (svname) from string
1508  * rc < 0 on error
1509  * if endptr isn't NULL it is set to end of fsname *
1510  */
1511 int server_name2svname(char *label, char *svname, char **endptr)
1512 {
1513         int rc;
1514         char *dash;
1515
1516         /* We use server_name2fsname() just for parsing */
1517         rc = server_name2fsname(label, NULL, &dash);
1518         if (rc != 0)
1519                 return rc;
1520
1521         if (*dash != '-')
1522                 return -1;
1523
1524         strncpy(svname, dash + 1, MTI_NAME_MAXLEN);
1525
1526         return 0;
1527 }
1528 EXPORT_SYMBOL(server_name2svname);
1529
1530
1531 /* Get the index from the obd name.
1532    rc = server type, or
1533    rc < 0  on error
1534    if endptr isn't NULL it is set to end of name */
1535 int server_name2index(char *svname, __u32 *idx, char **endptr)
1536 {
1537         unsigned long index;
1538         int rc;
1539         char *dash;
1540
1541         /* We use server_name2fsname() just for parsing */
1542         rc = server_name2fsname(svname, NULL, &dash);
1543         if (rc != 0)
1544                 return rc;
1545
1546         if (*dash != '-')
1547                 return -EINVAL;
1548
1549         dash++;
1550
1551         if (strncmp(dash, "MDT", 3) == 0)
1552                 rc = LDD_F_SV_TYPE_MDT;
1553         else if (strncmp(dash, "OST", 3) == 0)
1554                 rc = LDD_F_SV_TYPE_OST;
1555         else
1556                 return -EINVAL;
1557
1558         dash += 3;
1559
1560         if (strcmp(dash, "all") == 0)
1561                 return rc | LDD_F_SV_ALL;
1562
1563         index = simple_strtoul(dash, endptr, 16);
1564         *idx = index;
1565
1566         return rc;
1567 }
1568 EXPORT_SYMBOL(server_name2index);
1569
1570 /* Generate data for registration */
1571 static int server_lsi2mti(struct lustre_sb_info *lsi,
1572                           struct mgs_target_info *mti)
1573 {
1574         lnet_process_id_t id;
1575         int rc, i = 0;
1576         ENTRY;
1577
1578         if (!IS_SERVER(lsi))
1579                 RETURN(-EINVAL);
1580
1581         strncpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname));
1582
1583         mti->mti_nid_count = 0;
1584         while (LNetGetId(i++, &id) != -ENOENT) {
1585                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
1586                         continue;
1587
1588                 /* server use --servicenode param, only allow specified
1589                  * nids be registered */
1590                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) != 0 &&
1591                     class_match_nid(lsi->lsi_lmd->lmd_params,
1592                                     PARAM_FAILNODE, id.nid) < 1)
1593                         continue;
1594
1595                 /* match specified network */
1596                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1597                                      PARAM_NETWORK, LNET_NIDNET(id.nid)))
1598                         continue;
1599
1600                 mti->mti_nids[mti->mti_nid_count] = id.nid;
1601                 mti->mti_nid_count++;
1602                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1603                         CWARN("Only using first %d nids for %s\n",
1604                               mti->mti_nid_count, mti->mti_svname);
1605                         break;
1606                 }
1607         }
1608
1609         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1610         mti->mti_config_ver = 0;
1611
1612         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1613         if (rc != 0)
1614                 return rc;
1615
1616         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1617         if (rc < 0)
1618                 return rc;
1619         /* Orion requires index to be set */
1620         LASSERT(!(rc & LDD_F_NEED_INDEX));
1621         /* keep only LDD flags */
1622         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1623         if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1624                 mti->mti_flags |= LDD_F_UPDATE;
1625         strncpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1626                 sizeof(mti->mti_params));
1627         return 0;
1628 }
1629
1630 /* Register an old or new target with the MGS. If needed MGS will construct
1631    startup logs and assign index */
1632 static int server_register_target(struct lustre_sb_info *lsi)
1633 {
1634         struct obd_device *mgc = lsi->lsi_mgc;
1635         struct mgs_target_info *mti = NULL;
1636         bool writeconf;
1637         int rc;
1638         ENTRY;
1639
1640         LASSERT(mgc);
1641
1642         if (!IS_SERVER(lsi))
1643                 RETURN(-EINVAL);
1644
1645         OBD_ALLOC_PTR(mti);
1646         if (!mti)
1647                 RETURN(-ENOMEM);
1648
1649         rc = server_lsi2mti(lsi, mti);
1650         if (rc)
1651                 GOTO(out, rc);
1652
1653         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1654                mti->mti_svname, mti->mti_fsname,
1655                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1656                mti->mti_flags);
1657
1658         /* if write_conf is true, the registration must succeed */
1659         writeconf = !!(lsi->lsi_flags & (LDD_F_NEED_INDEX | LDD_F_UPDATE));
1660         mti->mti_flags |= LDD_F_OPC_REG;
1661
1662         /* Register the target */
1663         /* FIXME use mgc_process_config instead */
1664         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1665                                 sizeof(KEY_REGISTER_TARGET), KEY_REGISTER_TARGET,
1666                                 sizeof(*mti), mti, NULL);
1667         if (rc) {
1668                 if (mti->mti_flags & LDD_F_ERROR) {
1669                         LCONSOLE_ERROR_MSG(0x160,
1670                                 "The MGS is refusing to allow this "
1671                                 "server (%s) to start. Please see messages"
1672                                 " on the MGS node.\n", lsi->lsi_svname);
1673                 } else if (writeconf) {
1674                         LCONSOLE_ERROR_MSG(0x15f,
1675                                 "Communication to the MGS return error %d. "
1676                                 "Is the MGS running?\n", rc);
1677                 } else {
1678                         CERROR("Cannot talk to the MGS: %d, not fatal\n", rc);
1679                         /* reset the error code for non-fatal error. */
1680                         rc = 0;
1681                 }
1682                 GOTO(out, rc);
1683         }
1684
1685 out:
1686         if (mti)
1687                 OBD_FREE_PTR(mti);
1688         RETURN(rc);
1689 }
1690
1691 /**
1692  * Notify the MGS that this target is ready.
1693  * Used by IR - if the MGS receives this message, it will notify clients.
1694  */
1695 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1696 {
1697         struct lustre_sb_info *lsi = s2lsi(sb);
1698         struct obd_device *mgc = lsi->lsi_mgc;
1699         struct mgs_target_info *mti = NULL;
1700         int rc;
1701         ENTRY;
1702
1703         LASSERT(mgc);
1704
1705         if (!(IS_SERVER(lsi)))
1706                 RETURN(-EINVAL);
1707
1708         OBD_ALLOC_PTR(mti);
1709         if (!mti)
1710                 RETURN(-ENOMEM);
1711         rc = server_lsi2mti(lsi, mti);
1712         if (rc)
1713                 GOTO(out, rc);
1714
1715         mti->mti_instance = obd->u.obt.obt_instance;
1716         mti->mti_flags |= LDD_F_OPC_READY;
1717
1718         /* FIXME use mgc_process_config instead */
1719         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1720                                 sizeof(KEY_REGISTER_TARGET),
1721                                 KEY_REGISTER_TARGET,
1722                                 sizeof(*mti), mti, NULL);
1723
1724         /* Imperative recovery: if the mgs informs us to use IR? */
1725         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1726             (mti->mti_flags & LDD_F_IR_CAPABLE))
1727                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1728
1729 out:
1730         if (mti)
1731                 OBD_FREE_PTR(mti);
1732         RETURN(rc);
1733
1734 }
1735
1736 /**
1737  * Start the osp(fsname-MDT0000-osp-OSTxxxx) for an OST target,
1738  * which would be used to communicate with MDT0 for quota and FID.
1739  **/
1740 static int lustre_start_osp(struct super_block *sb)
1741 {
1742         struct lustre_sb_info       *lsi = s2lsi(sb);
1743         struct config_llog_instance *cfg = NULL;
1744         struct obd_device           *osp;
1745         char                        *ospname = NULL;
1746         char                        *logname = NULL;
1747         char                        *tgt;
1748         int                          rc;
1749         ENTRY;
1750
1751         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1752         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1753         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1754         if (ospname == NULL || logname == NULL)
1755                 GOTO(cleanup, rc = -ENOMEM);
1756
1757         rc = server_name2fsname(lsi->lsi_svname, ospname, &tgt);
1758         if (rc != 0) {
1759                 CERROR("%s change fsname error: rc %d\n",
1760                        lsi->lsi_svname, rc);
1761                 GOTO(cleanup, rc);
1762         }
1763         sprintf(logname, "%s-client", ospname);
1764
1765         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1766         if (rc != 0) {
1767                 CERROR("%s change ospname error: rc %d\n",
1768                        lsi->lsi_svname, rc);
1769                 GOTO(cleanup, rc);
1770         }
1771
1772         osp = class_name2obd(ospname);
1773         if (osp != NULL)
1774                 GOTO(cleanup, rc = 0);
1775
1776         OBD_ALLOC_PTR(cfg);
1777         if (cfg == NULL)
1778                 GOTO(cleanup, rc = -ENOMEM);
1779
1780         cfg->cfg_callback = client_osp_config_process;
1781         cfg->cfg_instance = sb;
1782
1783         rc = lustre_process_log(sb, logname, cfg);
1784
1785 cleanup:
1786         if (ospname != NULL)
1787                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1788         if (logname != NULL)
1789                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1790         if (cfg != NULL)
1791                 OBD_FREE_PTR(cfg);
1792
1793         RETURN(rc);
1794 }
1795
1796 /** Start server targets: MDTs and OSTs
1797  */
1798 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
1799 {
1800         struct obd_device *obd;
1801         struct lustre_sb_info *lsi = s2lsi(sb);
1802         struct config_llog_instance cfg;
1803         struct lu_env env;
1804         struct lu_device *dev;
1805         int rc;
1806         ENTRY;
1807
1808         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1809
1810         if (IS_MDT(lsi)) {
1811                 /* make sure the MDS is started */
1812                 mutex_lock(&server_start_lock);
1813                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1814                 if (!obd) {
1815                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1816                                                  LUSTRE_MDS_NAME,
1817                                                  LUSTRE_MDS_OBDNAME"_uuid",
1818                                                  0, 0, 0, 0);
1819                         if (rc) {
1820                                 mutex_unlock(&server_start_lock);
1821                                 CERROR("failed to start MDS: %d\n", rc);
1822                                 RETURN(rc);
1823                         }
1824                 }
1825                 mutex_unlock(&server_start_lock);
1826         }
1827
1828         /* If we're an OST, make sure the global OSS is running */
1829         if (IS_OST(lsi)) {
1830                 /* make sure OSS is started */
1831                 mutex_lock(&server_start_lock);
1832                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1833                 if (!obd) {
1834                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1835                                                  LUSTRE_OSS_NAME,
1836                                                  LUSTRE_OSS_OBDNAME"_uuid",
1837                                                  0, 0, 0, 0);
1838                         if (rc) {
1839                                 mutex_unlock(&server_start_lock);
1840                                 CERROR("failed to start OSS: %d\n", rc);
1841                                 RETURN(rc);
1842                         }
1843                 }
1844                 mutex_unlock(&server_start_lock);
1845         }
1846
1847         /* Set the mgc fs to our server disk.  This allows the MGC to
1848          * read and write configs locally, in case it can't talk to the MGS. */
1849         if (lsi->lsi_srv_mnt) {
1850                 rc = server_mgc_set_fs(lsi->lsi_mgc, sb);
1851                 if (rc)
1852                         GOTO(out_stop_service, rc);
1853         }
1854
1855         /* Register with MGS */
1856         rc = server_register_target(lsi);
1857         if (rc)
1858                 GOTO(out_mgc, rc);
1859
1860         /* Let the target look up the mount using the target's name
1861            (we can't pass the sb or mnt through class_process_config.) */
1862         rc = server_register_mount(lsi->lsi_svname, sb, mnt);
1863         if (rc)
1864                 GOTO(out_mgc, rc);
1865
1866         /* Start targets using the llog named for the target */
1867         memset(&cfg, 0, sizeof(cfg));
1868         cfg.cfg_callback = class_config_llog_handler;
1869         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1870         if (rc) {
1871                 CERROR("failed to start server %s: %d\n",
1872                        lsi->lsi_svname, rc);
1873                 /* Do NOT call server_deregister_mount() here. This makes it
1874                  * impossible to find mount later in cleanup time and leaves
1875                  * @lsi and othder stuff leaked. -umka */
1876                 GOTO(out_mgc, rc);
1877         }
1878
1879         obd = class_name2obd(lsi->lsi_svname);
1880         if (!obd) {
1881                 CERROR("no server named %s was started\n", lsi->lsi_svname);
1882                 GOTO(out_mgc, rc = -ENXIO);
1883         }
1884
1885         if (IS_OST(lsi) || IS_MDT(lsi)) {
1886                 rc = lustre_start_osp(sb);
1887                 if (rc) {
1888                         CERROR("%s: failed to start OSP: %d\n",
1889                                lsi->lsi_svname, rc);
1890                         GOTO(out_mgc, rc);
1891                 }
1892         }
1893
1894         server_notify_target(sb, obd);
1895
1896         /* calculate recovery timeout, do it after lustre_process_log */
1897         server_calc_timeout(lsi, obd);
1898
1899         /* log has been fully processed */
1900         obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1901
1902         /* log has been fully processed, let clients connect */
1903         dev = obd->obd_lu_dev;
1904         if (dev && dev->ld_ops->ldo_prepare) {
1905                 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1906                 if (rc == 0) {
1907                         struct lu_context  session_ctx;
1908
1909                         lu_context_init(&session_ctx, LCT_SESSION);
1910                         session_ctx.lc_thread = NULL;
1911                         lu_context_enter(&session_ctx);
1912                         env.le_ses = &session_ctx;
1913
1914                         dev->ld_ops->ldo_prepare(&env, NULL, dev);
1915
1916                         lu_env_fini(&env);
1917                         lu_context_exit(&session_ctx);
1918                         lu_context_fini(&session_ctx);
1919                 }
1920         }
1921
1922         /* abort recovery only on the complete stack:
1923          * many devices can be involved */
1924         if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1925             (OBP(obd, iocontrol))) {
1926                 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1927                               NULL, NULL);
1928         }
1929
1930 out_mgc:
1931         /* Release the mgc fs for others to use */
1932         if (lsi->lsi_srv_mnt)
1933                 server_mgc_clear_fs(lsi->lsi_mgc);
1934
1935 out_stop_service:
1936         if (rc != 0)
1937                 server_stop_servers(lsi->lsi_flags);
1938
1939         RETURN(rc);
1940 }
1941
1942 /***************** lustre superblock **************/
1943
1944 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
1945 {
1946         struct lustre_sb_info *lsi;
1947         ENTRY;
1948
1949         OBD_ALLOC_PTR(lsi);
1950         if (!lsi)
1951                 RETURN(NULL);
1952         OBD_ALLOC_PTR(lsi->lsi_lmd);
1953         if (!lsi->lsi_lmd) {
1954                 OBD_FREE_PTR(lsi);
1955                 RETURN(NULL);
1956         }
1957
1958         lsi->lsi_lmd->lmd_exclude_count = 0;
1959         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
1960         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
1961         s2lsi_nocast(sb) = lsi;
1962         /* we take 1 extra ref for our setup */
1963         cfs_atomic_set(&lsi->lsi_mounts, 1);
1964
1965         /* Default umount style */
1966         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
1967
1968         RETURN(lsi);
1969 }
1970
1971 static int lustre_free_lsi(struct super_block *sb)
1972 {
1973         struct lustre_sb_info *lsi = s2lsi(sb);
1974         ENTRY;
1975
1976         LASSERT(lsi != NULL);
1977         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
1978
1979         /* someone didn't call server_put_mount. */
1980         LASSERT(cfs_atomic_read(&lsi->lsi_mounts) == 0);
1981
1982         if (lsi->lsi_lmd != NULL) {
1983                 if (lsi->lsi_lmd->lmd_dev != NULL)
1984                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
1985                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1986                 if (lsi->lsi_lmd->lmd_profile != NULL)
1987                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
1988                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
1989                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
1990                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
1991                                  strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
1992                 if (lsi->lsi_lmd->lmd_opts != NULL)
1993                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
1994                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1995                 if (lsi->lsi_lmd->lmd_exclude_count)
1996                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
1997                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
1998                                  lsi->lsi_lmd->lmd_exclude_count);
1999                 if (lsi->lsi_lmd->lmd_mgs != NULL)
2000                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
2001                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
2002                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
2003                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
2004                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
2005                 if (lsi->lsi_lmd->lmd_params != NULL)
2006                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
2007
2008                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
2009         }
2010
2011         LASSERT(lsi->lsi_llsbi == NULL);
2012         OBD_FREE(lsi, sizeof(*lsi));
2013         s2lsi_nocast(sb) = NULL;
2014
2015         RETURN(0);
2016 }
2017
2018 /* The lsi has one reference for every server that is using the disk -
2019    e.g. MDT, MGS, and potentially MGC */
2020 static int lustre_put_lsi(struct super_block *sb)
2021 {
2022         struct lustre_sb_info *lsi = s2lsi(sb);
2023         ENTRY;
2024
2025         LASSERT(lsi != NULL);
2026
2027         CDEBUG(D_MOUNT, "put %p %d\n", sb, cfs_atomic_read(&lsi->lsi_mounts));
2028         if (cfs_atomic_dec_and_test(&lsi->lsi_mounts)) {
2029                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
2030                         obd_disconnect(lsi->lsi_osd_exp);
2031                         /* wait till OSD is gone */
2032                         obd_zombie_barrier();
2033                 }
2034                 lustre_free_lsi(sb);
2035                 RETURN(1);
2036         }
2037         RETURN(0);
2038 }
2039
2040 static int lsi_prepare(struct lustre_sb_info *lsi)
2041 {
2042         __u32 index;
2043         int rc;
2044         ENTRY;
2045
2046         LASSERT(lsi);
2047         LASSERT(lsi->lsi_lmd);
2048
2049         /* The server name is given as a mount line option */
2050         if (lsi->lsi_lmd->lmd_profile == NULL) {
2051                 LCONSOLE_ERROR("Can't determine server name\n");
2052                 RETURN(-EINVAL);
2053         }
2054
2055         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname))
2056                 RETURN(-ENAMETOOLONG);
2057
2058         strcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile);
2059
2060         /* Determine osd type */
2061         if (lsi->lsi_lmd->lmd_osd_type != NULL) {
2062                 if (strlen(lsi->lsi_lmd->lmd_osd_type) >=
2063                            sizeof(lsi->lsi_osd_type))
2064                         RETURN(-ENAMETOOLONG);
2065
2066                 strcpy(lsi->lsi_osd_type, lsi->lsi_lmd->lmd_osd_type);
2067         } else {
2068                 strcpy(lsi->lsi_osd_type, LUSTRE_OSD_LDISKFS_NAME);
2069         }
2070
2071         /* XXX: a temp. solution for components using fsfilt
2072          *      to be removed in one of the subsequent patches */
2073         if (!strcmp(lsi->lsi_lmd->lmd_osd_type, "osd-ldiskfs")) {
2074                 strcpy(lsi->lsi_fstype, "ldiskfs");
2075         } else {
2076                 strcpy(lsi->lsi_fstype, lsi->lsi_lmd->lmd_osd_type);
2077         }
2078
2079         /* Determine server type */
2080         rc = server_name2index(lsi->lsi_svname, &index, NULL);
2081         if (rc < 0) {
2082                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) {
2083                         /* Assume we're a bare MGS */
2084                         rc = 0;
2085                         lsi->lsi_lmd->lmd_flags |= LMD_FLG_NOSVC;
2086                 } else {
2087                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
2088                                        lsi->lsi_svname);
2089                         RETURN(rc);
2090                 }
2091         }
2092         lsi->lsi_flags |= rc;
2093
2094         /* Add mount line flags that used to be in ldd:
2095          * writeconf, mgs, anything else?
2096          */
2097         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_WRITECONF) ?
2098                 LDD_F_WRITECONF : 0;
2099         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_VIRGIN) ?
2100                 LDD_F_VIRGIN : 0;
2101         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) ?
2102                 LDD_F_SV_TYPE_MGS : 0;
2103         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) ?
2104                 LDD_F_NO_PRIMNODE : 0;
2105
2106         RETURN(0);
2107 }
2108
2109 /*************** server mount ******************/
2110
2111 /** Start the shutdown of servers at umount.
2112  */
2113 static void server_put_super(struct super_block *sb)
2114 {
2115         struct lustre_sb_info *lsi = s2lsi(sb);
2116         struct obd_device     *obd;
2117         char *tmpname, *extraname = NULL;
2118         int tmpname_sz;
2119         int lsiflags = lsi->lsi_flags;
2120         ENTRY;
2121
2122         LASSERT(IS_SERVER(lsi));
2123
2124         tmpname_sz = strlen(lsi->lsi_svname) + 1;
2125         OBD_ALLOC(tmpname, tmpname_sz);
2126         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
2127         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
2128         if (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC))
2129                 snprintf(tmpname, tmpname_sz, "MGS");
2130
2131         /* disconnect the osp-on-ost first to drain off the inflight request */
2132         if (IS_OST(lsi) || IS_MDT(lsi)) {
2133                 int     rc;
2134
2135                 rc = lustre_disconnect_osp(sb);
2136                 if (rc && rc != ETIMEDOUT)
2137                         CERROR("%s: failed to disconnect osp-on-ost (rc=%d)!\n",
2138                                tmpname, rc);
2139         }
2140
2141         /* Stop the target */
2142         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
2143             (IS_MDT(lsi) || IS_OST(lsi))) {
2144                 struct lustre_profile *lprof = NULL;
2145
2146                 /* tell the mgc to drop the config log */
2147                 lustre_end_log(sb, lsi->lsi_svname, NULL);
2148
2149                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
2150                    If there are any setup/cleanup errors, save the lov
2151                    name for safety cleanup later. */
2152                 lprof = class_get_profile(lsi->lsi_svname);
2153                 if (lprof && lprof->lp_dt) {
2154                         OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
2155                         strcpy(extraname, lprof->lp_dt);
2156                 }
2157
2158                 obd = class_name2obd(lsi->lsi_svname);
2159                 if (obd) {
2160                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
2161                         if (lsiflags & LSI_UMOUNT_FAILOVER)
2162                                 obd->obd_fail = 1;
2163                         /* We can't seem to give an error return code
2164                          * to .put_super, so we better make sure we clean up! */
2165                         obd->obd_force = 1;
2166                         class_manual_cleanup(obd);
2167                 } else {
2168                         CERROR("no obd %s\n", lsi->lsi_svname);
2169                         server_deregister_mount(lsi->lsi_svname);
2170                 }
2171         }
2172
2173         /* If they wanted the mgs to stop separately from the mdt, they
2174            should have put it on a different device. */
2175         if (IS_MGS(lsi)) {
2176                 /* if MDS start with --nomgs, don't stop MGS then */
2177                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
2178                         server_stop_mgs(sb);
2179         }
2180
2181         if (IS_OST(lsi) || IS_MDT(lsi)) {
2182                 if (lustre_stop_osp(sb) < 0)
2183                         CERROR("%s: Fail to stop osp-on-ost!\n", tmpname);
2184         }
2185
2186         /* Clean the mgc and sb */
2187         lustre_common_put_super(sb);
2188
2189         /* wait till all in-progress cleanups are done
2190          * specifically we're interested in ofd cleanup
2191          * as it pins OSS */
2192         obd_zombie_barrier();
2193
2194         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
2195            until the target is really gone so that our type refcount check
2196            is right. */
2197         server_stop_servers(lsiflags);
2198
2199         /* In case of startup or cleanup err, stop related obds */
2200         if (extraname) {
2201                 obd = class_name2obd(extraname);
2202                 if (obd) {
2203                         CWARN("Cleaning orphaned obd %s\n", extraname);
2204                         obd->obd_force = 1;
2205                         class_manual_cleanup(obd);
2206                 }
2207                 OBD_FREE(extraname, strlen(extraname) + 1);
2208         }
2209
2210         LCONSOLE_WARN("server umount %s complete\n", tmpname);
2211         OBD_FREE(tmpname, tmpname_sz);
2212         EXIT;
2213 }
2214
2215 /** Called only for 'umount -f'
2216  */
2217 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2218 static void server_umount_begin(struct vfsmount *vfsmnt, int flags)
2219 {
2220         struct super_block *sb = vfsmnt->mnt_sb;
2221 #else
2222 static void server_umount_begin(struct super_block *sb)
2223 {
2224 #endif
2225         struct lustre_sb_info *lsi = s2lsi(sb);
2226         ENTRY;
2227
2228 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2229         if (!(flags & MNT_FORCE)) {
2230                 EXIT;
2231                 return;
2232         }
2233 #endif
2234
2235         CDEBUG(D_MOUNT, "umount -f\n");
2236         /* umount = failover
2237            umount -f = force
2238            no third way to do non-force, non-failover */
2239         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
2240         EXIT;
2241 }
2242
2243 static int server_statfs (struct dentry *dentry, cfs_kstatfs_t *buf)
2244 {
2245         struct super_block *sb = dentry->d_sb;
2246         struct lustre_sb_info *lsi = s2lsi(sb);
2247         struct obd_statfs statfs;
2248         int rc;
2249         ENTRY;
2250
2251         if (lsi->lsi_dt_dev) {
2252                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
2253                 if (rc == 0) {
2254                         statfs_unpack(buf, &statfs);
2255                         buf->f_type = sb->s_magic;
2256                         RETURN(0);
2257                 }
2258         }
2259
2260         /* just return 0 */
2261         buf->f_type = sb->s_magic;
2262         buf->f_bsize = sb->s_blocksize;
2263         buf->f_blocks = 1;
2264         buf->f_bfree = 0;
2265         buf->f_bavail = 0;
2266         buf->f_files = 1;
2267         buf->f_ffree = 0;
2268         buf->f_namelen = NAME_MAX;
2269         RETURN(0);
2270 }
2271
2272 /** The operations we support directly on the superblock:
2273  * mount, umount, and df.
2274  */
2275 static struct super_operations server_ops =
2276 {
2277         .put_super      = server_put_super,
2278         .umount_begin   = server_umount_begin, /* umount -f */
2279         .statfs         = server_statfs,
2280 };
2281
2282 #define log2(n) ffz(~(n))
2283 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
2284
2285 static int server_fill_super_common(struct super_block *sb)
2286 {
2287         struct inode *root = 0;
2288         ENTRY;
2289
2290         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
2291
2292         sb->s_blocksize = 4096;
2293         sb->s_blocksize_bits = log2(sb->s_blocksize);
2294         sb->s_magic = LUSTRE_SUPER_MAGIC;
2295         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
2296         sb->s_flags |= MS_RDONLY;
2297         sb->s_op = &server_ops;
2298
2299         root = new_inode(sb);
2300         if (!root) {
2301                 CERROR("Can't make root inode\n");
2302                 RETURN(-EIO);
2303         }
2304
2305         /* returns -EIO for every operation */
2306         /* make_bad_inode(root); -- badness - can't umount */
2307         /* apparently we need to be a directory for the mount to finish */
2308         root->i_mode = S_IFDIR;
2309
2310         sb->s_root = d_make_root(root);
2311         if (!sb->s_root) {
2312                 CERROR("%s: can't make root dentry\n", sb->s_id);
2313                 RETURN(-EIO);
2314         }
2315
2316         RETURN(0);
2317 }
2318
2319 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
2320 {
2321         struct lustre_mount_data *lmd = lsi->lsi_lmd;
2322         struct obd_device        *obd;
2323         struct dt_device_param    p;
2324         char                      flagstr[16];
2325         int                       rc;
2326         ENTRY;
2327
2328         CDEBUG(D_MOUNT,
2329                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
2330                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
2331
2332         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
2333         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
2334         strcat(lsi->lsi_osd_uuid, "_UUID");
2335         sprintf(flagstr, "%lu:%lu", mflags, (unsigned long) lmd->lmd_flags);
2336
2337         obd = class_name2obd(lsi->lsi_osd_obdname);
2338         if (obd == NULL) {
2339                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
2340                                 lsi->lsi_osd_type,
2341                                 lsi->lsi_osd_uuid, lmd->lmd_dev,
2342                                 flagstr, lsi->lsi_lmd->lmd_opts,
2343                                 lsi->lsi_svname);
2344                 if (rc)
2345                         GOTO(out, rc);
2346                 obd = class_name2obd(lsi->lsi_osd_obdname);
2347                 LASSERT(obd);
2348         }
2349
2350         rc = obd_connect(NULL, &lsi->lsi_osd_exp, obd, &obd->obd_uuid, NULL, NULL);
2351         if (rc) {
2352                 obd->obd_force = 1;
2353                 class_manual_cleanup(obd);
2354                 lsi->lsi_dt_dev = NULL;
2355         }
2356
2357         /* XXX: to keep support old components relying on lsi_srv_mnt
2358          *      we get this info from OSD just started */
2359         LASSERT(obd->obd_lu_dev);
2360         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
2361         LASSERT(lsi->lsi_dt_dev);
2362
2363         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
2364
2365         lsi->lsi_srv_mnt = p.ddp_mnt;
2366
2367 out:
2368         RETURN(rc);
2369 }
2370
2371 /** Fill in the superblock info for a Lustre server.
2372  * Mount the device with the correct options.
2373  * Read the on-disk config file.
2374  * Start the services.
2375  */
2376 static int server_fill_super(struct super_block *sb)
2377 {
2378         struct lustre_sb_info *lsi = s2lsi(sb);
2379         int rc;
2380         ENTRY;
2381
2382         rc = lsi_prepare(lsi);
2383         if (rc)
2384                 RETURN(rc);
2385
2386         /* Start low level OSD */
2387         rc = osd_start(lsi, sb->s_flags);
2388         if (rc) {
2389                 CERROR("Unable to start osd on %s: %d\n",
2390                        lsi->lsi_lmd->lmd_dev, rc);
2391                 lustre_put_lsi(sb);
2392                 RETURN(rc);
2393         }
2394
2395         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2396                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2397
2398         if (class_name2obd(lsi->lsi_svname)) {
2399                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
2400                                    "running. Double-mount may have compromised"
2401                                    " the disk journal.\n",
2402                                    lsi->lsi_svname);
2403                 lustre_put_lsi(sb);
2404                 RETURN(-EALREADY);
2405         }
2406
2407         /* Start MGS before MGC */
2408         if (IS_MGS(lsi) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)){
2409                 rc = server_start_mgs(sb);
2410                 if (rc)
2411                         GOTO(out_mnt, rc);
2412         }
2413
2414         /* Start MGC before servers */
2415         rc = lustre_start_mgc(sb);
2416         if (rc)
2417                 GOTO(out_mnt, rc);
2418
2419         /* Set up all obd devices for service */
2420         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
2421                         (IS_OST(lsi) || IS_MDT(lsi))) {
2422                 rc = server_start_targets(sb, lsi->lsi_srv_mnt);
2423                 if (rc < 0) {
2424                         CERROR("Unable to start targets: %d\n", rc);
2425                         GOTO(out_mnt, rc);
2426                 }
2427         /* FIXME overmount client here,
2428            or can we just start a client log and client_fill_super on this sb?
2429            We need to make sure server_put_super gets called too - ll_put_super
2430            calls lustre_common_put_super; check there for LSI_SERVER flag,
2431            call s_p_s if so.
2432            Probably should start client from new thread so we can return.
2433            Client will not finish until all servers are connected.
2434            Note - MGS-only server does NOT get a client, since there is no
2435            lustre fs associated - the MGS is for all lustre fs's */
2436         }
2437
2438         rc = server_fill_super_common(sb);
2439         if (rc)
2440                 GOTO(out_mnt, rc);
2441
2442         RETURN(0);
2443 out_mnt:
2444         /* We jump here in case of failure while starting targets or MGS.
2445          * In this case we can't just put @mnt and have to do real cleanup
2446          * with stoping targets, etc. */
2447         server_put_super(sb);
2448         return rc;
2449 }
2450
2451 /*
2452  * Calculate timeout value for a target.
2453  */
2454 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2455 {
2456         struct lustre_mount_data *lmd;
2457         int soft = 0;
2458         int hard = 0;
2459         int factor = 0;
2460         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2461         int min = OBD_RECOVERY_TIME_MIN;
2462
2463         LASSERT(IS_SERVER(lsi));
2464
2465         lmd = lsi->lsi_lmd;
2466         if (lmd) {
2467                 soft   = lmd->lmd_recovery_time_soft;
2468                 hard   = lmd->lmd_recovery_time_hard;
2469                 has_ir = has_ir && !(lmd->lmd_flags & LMD_FLG_NOIR);
2470                 obd->obd_no_ir = !has_ir;
2471         }
2472
2473         if (soft == 0)
2474                 soft = OBD_RECOVERY_TIME_SOFT;
2475         if (hard == 0)
2476                 hard = OBD_RECOVERY_TIME_HARD;
2477
2478         /* target may have ir_factor configured. */
2479         factor = OBD_IR_FACTOR_DEFAULT;
2480         if (obd->obd_recovery_ir_factor)
2481                 factor = obd->obd_recovery_ir_factor;
2482
2483         if (has_ir) {
2484                 int new_soft = soft;
2485                 int new_hard = hard;
2486
2487                 /* adjust timeout value by imperative recovery */
2488
2489                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2490                 new_hard = (hard * factor) / OBD_IR_FACTOR_MAX;
2491
2492                 /* make sure the timeout is not too short */
2493                 new_soft = max(min, new_soft);
2494                 new_hard = max(new_soft, new_hard);
2495
2496                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery "
2497                               "window shrunk from %d-%d down to %d-%d\n",
2498                               obd->obd_name, soft, hard, new_soft, new_hard);
2499
2500                 soft = new_soft;
2501                 hard = new_hard;
2502         }
2503
2504         /* we're done */
2505         obd->obd_recovery_timeout   = max(obd->obd_recovery_timeout, soft);
2506         obd->obd_recovery_time_hard = hard;
2507         obd->obd_recovery_ir_factor = factor;
2508 }
2509 EXPORT_SYMBOL(server_calc_timeout);
2510
2511 /*************** mount common betweeen server and client ***************/
2512
2513 /* Common umount */
2514 int lustre_common_put_super(struct super_block *sb)
2515 {
2516         int rc;
2517         ENTRY;
2518
2519         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
2520
2521         /* Drop a ref to the MGC */
2522         rc = lustre_stop_mgc(sb);
2523         if (rc && (rc != -ENOENT)) {
2524                 if (rc != -EBUSY) {
2525                         CERROR("Can't stop MGC: %d\n", rc);
2526                         RETURN(rc);
2527                 }
2528                 /* BUSY just means that there's some other obd that
2529                    needs the mgc.  Let him clean it up. */
2530                 CDEBUG(D_MOUNT, "MGC still in use\n");
2531         }
2532         /* Drop a ref to the mounted disk */
2533         lustre_put_lsi(sb);
2534         lu_types_stop();
2535         RETURN(rc);
2536 }
2537 EXPORT_SYMBOL(lustre_common_put_super);
2538
2539 static void lmd_print(struct lustre_mount_data *lmd)
2540 {
2541         int i;
2542
2543         PRINT_CMD(PRINT_MASK, "  mount data:\n");
2544         if (lmd_is_client(lmd))
2545                 PRINT_CMD(PRINT_MASK, "profile: %s\n", lmd->lmd_profile);
2546         PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
2547         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
2548
2549         if (lmd->lmd_opts)
2550                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
2551
2552         if (lmd->lmd_recovery_time_soft)
2553                 PRINT_CMD(PRINT_MASK, "recovery time soft: %d\n",
2554                           lmd->lmd_recovery_time_soft);
2555
2556         if (lmd->lmd_recovery_time_hard)
2557                 PRINT_CMD(PRINT_MASK, "recovery time hard: %d\n",
2558                           lmd->lmd_recovery_time_hard);
2559
2560         for (i = 0; i < lmd->lmd_exclude_count; i++) {
2561                 PRINT_CMD(PRINT_MASK, "exclude %d:  OST%04x\n", i,
2562                           lmd->lmd_exclude[i]);
2563         }
2564 }
2565
2566 /* Is this server on the exclusion list */
2567 int lustre_check_exclusion(struct super_block *sb, char *svname)
2568 {
2569         struct lustre_sb_info *lsi = s2lsi(sb);
2570         struct lustre_mount_data *lmd = lsi->lsi_lmd;
2571         __u32 index;
2572         int i, rc;
2573         ENTRY;
2574
2575         rc = server_name2index(svname, &index, NULL);
2576         if (rc != LDD_F_SV_TYPE_OST)
2577                 /* Only exclude OSTs */
2578                 RETURN(0);
2579
2580         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
2581                index, lmd->lmd_exclude_count, lmd->lmd_dev);
2582
2583         for(i = 0; i < lmd->lmd_exclude_count; i++) {
2584                 if (index == lmd->lmd_exclude[i]) {
2585                         CWARN("Excluding %s (on exclusion list)\n", svname);
2586                         RETURN(1);
2587                 }
2588         }
2589         RETURN(0);
2590 }
2591
2592 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
2593 static int lmd_make_exclusion(struct lustre_mount_data *lmd, char *ptr)
2594 {
2595         char *s1 = ptr, *s2;
2596         __u32 index, *exclude_list;
2597         int rc = 0, devmax;
2598         ENTRY;
2599
2600         /* The shortest an ost name can be is 8 chars: -OST0000.
2601            We don't actually know the fsname at this time, so in fact
2602            a user could specify any fsname. */
2603         devmax = strlen(ptr) / 8 + 1;
2604
2605         /* temp storage until we figure out how many we have */
2606         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
2607         if (!exclude_list)
2608                 RETURN(-ENOMEM);
2609
2610         /* we enter this fn pointing at the '=' */
2611         while (*s1 && *s1 != ' ' && *s1 != ',') {
2612                 s1++;
2613                 rc = server_name2index(s1, &index, &s2);
2614                 if (rc < 0) {
2615                         CERROR("Can't parse server name '%s'\n", s1);
2616                         break;
2617                 }
2618                 if (rc == LDD_F_SV_TYPE_OST)
2619                         exclude_list[lmd->lmd_exclude_count++] = index;
2620                 else
2621                         CDEBUG(D_MOUNT, "ignoring exclude %.7s\n", s1);
2622                 s1 = s2;
2623                 /* now we are pointing at ':' (next exclude)
2624                    or ',' (end of excludes) */
2625                 if (lmd->lmd_exclude_count >= devmax)
2626                         break;
2627         }
2628         if (rc >= 0) /* non-err */
2629                 rc = 0;
2630
2631         if (lmd->lmd_exclude_count) {
2632                 /* permanent, freed in lustre_free_lsi */
2633                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
2634                           lmd->lmd_exclude_count);
2635                 if (lmd->lmd_exclude) {
2636                         memcpy(lmd->lmd_exclude, exclude_list,
2637                                sizeof(index) * lmd->lmd_exclude_count);
2638                 } else {
2639                         rc = -ENOMEM;
2640                         lmd->lmd_exclude_count = 0;
2641                 }
2642         }
2643         OBD_FREE(exclude_list, sizeof(index) * devmax);
2644         RETURN(rc);
2645 }
2646
2647 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
2648 {
2649         char   *tail;
2650         int     length;
2651
2652         if (lmd->lmd_mgssec != NULL) {
2653                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
2654                 lmd->lmd_mgssec = NULL;
2655         }
2656
2657         tail = strchr(ptr, ',');
2658         if (tail == NULL)
2659                 length = strlen(ptr);
2660         else
2661                 length = tail - ptr;
2662
2663         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
2664         if (lmd->lmd_mgssec == NULL)
2665                 return -ENOMEM;
2666
2667         memcpy(lmd->lmd_mgssec, ptr, length);
2668         lmd->lmd_mgssec[length] = '\0';
2669         return 0;
2670 }
2671
2672 static int lmd_parse_string(char **handle, char *ptr)
2673 {
2674         char   *tail;
2675         int     length;
2676
2677         if ((handle == NULL) || (ptr == NULL))
2678                 return -EINVAL;
2679
2680         if (*handle != NULL) {
2681                 OBD_FREE(*handle, strlen(*handle) + 1);
2682                 *handle = NULL;
2683         }
2684
2685         tail = strchr(ptr, ',');
2686         if (tail == NULL)
2687                 length = strlen(ptr);
2688         else
2689                 length = tail - ptr;
2690
2691         OBD_ALLOC(*handle, length + 1);
2692         if (*handle == NULL)
2693                 return -ENOMEM;
2694
2695         memcpy(*handle, ptr, length);
2696         (*handle)[length] = '\0';
2697
2698         return 0;
2699 }
2700
2701 /* Collect multiple values for mgsnid specifiers */
2702 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
2703 {
2704         lnet_nid_t nid;
2705         char *tail = *ptr;
2706         char *mgsnid;
2707         int   length;
2708         int   oldlen = 0;
2709
2710         /* Find end of nidlist */
2711         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
2712         length = tail - *ptr;
2713         if (length == 0) {
2714                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
2715                 return -EINVAL;
2716         }
2717
2718         if (lmd->lmd_mgs != NULL)
2719                 oldlen = strlen(lmd->lmd_mgs) + 1;
2720
2721         OBD_ALLOC(mgsnid, oldlen + length + 1);
2722         if (mgsnid == NULL)
2723                 return -ENOMEM;
2724
2725         if (lmd->lmd_mgs != NULL) {
2726                 /* Multiple mgsnid= are taken to mean failover locations */
2727                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
2728                 mgsnid[oldlen - 1] = ':';
2729                 OBD_FREE(lmd->lmd_mgs, oldlen);
2730         }
2731         memcpy(mgsnid + oldlen, *ptr, length);
2732         mgsnid[oldlen + length] = '\0';
2733         lmd->lmd_mgs = mgsnid;
2734         *ptr = tail;
2735
2736         return 0;
2737 }
2738
2739 /** Parse mount line options
2740  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
2741  * dev is passed as device=uml1:/lustre by mount.lustre
2742  */
2743 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
2744 {
2745         char *s1, *s2, *devname = NULL;
2746         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
2747         int rc = 0;
2748         ENTRY;
2749
2750         LASSERT(lmd);
2751         if (!options) {
2752                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
2753                                    "/sbin/mount.lustre is installed.\n");
2754                 RETURN(-EINVAL);
2755         }
2756
2757         /* Options should be a string - try to detect old lmd data */
2758         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
2759                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
2760                                    "/sbin/mount.lustre.  Please install "
2761                                    "version %s\n", LUSTRE_VERSION_STRING);
2762                 RETURN(-EINVAL);
2763         }
2764         lmd->lmd_magic = LMD_MAGIC;
2765
2766         OBD_ALLOC(lmd->lmd_params, 4096);
2767         if (lmd->lmd_params == NULL)
2768                 RETURN(-ENOMEM);
2769         lmd->lmd_params[0] = '\0';
2770
2771         /* Set default flags here */
2772
2773         s1 = options;
2774         while (*s1) {
2775                 int clear = 0;
2776                 int time_min = OBD_RECOVERY_TIME_MIN;
2777
2778                 /* Skip whitespace and extra commas */
2779                 while (*s1 == ' ' || *s1 == ',')
2780                         s1++;
2781
2782                 /* Client options are parsed in ll_options: eg. flock,
2783                    user_xattr, acl */
2784
2785                 /* Parse non-ldiskfs options here. Rather than modifying
2786                    ldiskfs, we just zero these out here */
2787                 if (strncmp(s1, "abort_recov", 11) == 0) {
2788                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
2789                         clear++;
2790                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
2791                         lmd->lmd_recovery_time_soft = max_t(int,
2792                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2793                         clear++;
2794                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
2795                         lmd->lmd_recovery_time_hard = max_t(int,
2796                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2797                         clear++;
2798                 } else if (strncmp(s1, "noir", 4) == 0) {
2799                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
2800                         clear++;
2801                 } else if (strncmp(s1, "nosvc", 5) == 0) {
2802                         lmd->lmd_flags |= LMD_FLG_NOSVC;
2803                         clear++;
2804                 } else if (strncmp(s1, "nomgs", 5) == 0) {
2805                         lmd->lmd_flags |= LMD_FLG_NOMGS;
2806                         clear++;
2807                 } else if (strncmp(s1, "noscrub", 7) == 0) {
2808                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
2809                         clear++;
2810                 } else if (strncmp(s1, PARAM_MGSNODE,
2811                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
2812                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
2813                         /* Assume the next mount opt is the first
2814                            invalid nid we get to. */
2815                         rc = lmd_parse_mgs(lmd, &s2);
2816                         if (rc)
2817                                 goto invalid;
2818                         clear++;
2819                 } else if (strncmp(s1, "writeconf", 9) == 0) {
2820                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
2821                         clear++;
2822                 } else if (strncmp(s1, "virgin", 6) == 0) {
2823                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
2824                         clear++;
2825                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
2826                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
2827                         clear++;
2828                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
2829                         rc = lmd_parse_mgssec(lmd, s1 + 7);
2830                         if (rc)
2831                                 goto invalid;
2832                         clear++;
2833                 /* ost exclusion list */
2834                 } else if (strncmp(s1, "exclude=", 8) == 0) {
2835                         rc = lmd_make_exclusion(lmd, s1 + 7);
2836                         if (rc)
2837                                 goto invalid;
2838                         clear++;
2839                 } else if (strncmp(s1, "mgs", 3) == 0) {
2840                         /* We are an MGS */
2841                         lmd->lmd_flags |= LMD_FLG_MGS;
2842                         clear++;
2843                 } else if (strncmp(s1, "svname=", 7) == 0) {
2844                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
2845                         if (rc)
2846                                 goto invalid;
2847                         clear++;
2848                 } else if (strncmp(s1, "param=", 6) == 0) {
2849                         int length;
2850                         char *tail = strchr(s1 + 6, ',');
2851                         if (tail == NULL)
2852                                 length = strlen(s1);
2853                         else
2854                                 length = tail - s1;
2855                         length -= 6;
2856                         strncat(lmd->lmd_params, s1 + 6, length);
2857                         strcat(lmd->lmd_params, " ");
2858                         clear++;
2859                 } else if (strncmp(s1, "osd=", 4) == 0) {
2860                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
2861                         if (rc)
2862                                 goto invalid;
2863                         clear++;
2864                 }
2865                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
2866                    end of the options. */
2867                 else if (strncmp(s1, "device=", 7) == 0) {
2868                         devname = s1 + 7;
2869                         /* terminate options right before device.  device
2870                            must be the last one. */
2871                         *s1 = '\0';
2872                         break;
2873                 }
2874
2875                 /* Find next opt */
2876                 s2 = strchr(s1, ',');
2877                 if (s2 == NULL) {
2878                         if (clear)
2879                                 *s1 = '\0';
2880                         break;
2881                 }
2882                 s2++;
2883                 if (clear)
2884                         memmove(s1, s2, strlen(s2) + 1);
2885                 else
2886                         s1 = s2;
2887         }
2888
2889         if (!devname) {
2890                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
2891                                    "(need mount option 'device=...')\n");
2892                 goto invalid;
2893         }
2894
2895         s1 = strstr(devname, ":/");
2896         if (s1) {
2897                 ++s1;
2898                 lmd->lmd_flags |= LMD_FLG_CLIENT;
2899                 /* Remove leading /s from fsname */
2900                 while (*++s1 == '/') ;
2901                 /* Freed in lustre_free_lsi */
2902                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
2903                 if (!lmd->lmd_profile)
2904                         RETURN(-ENOMEM);
2905                 sprintf(lmd->lmd_profile, "%s-client", s1);
2906         }
2907
2908         /* Freed in lustre_free_lsi */
2909         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
2910         if (!lmd->lmd_dev)
2911                 RETURN(-ENOMEM);
2912         strcpy(lmd->lmd_dev, devname);
2913
2914         /* Save mount options */
2915         s1 = options + strlen(options) - 1;
2916         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
2917                 *s1-- = 0;
2918         if (*options != 0) {
2919                 /* Freed in lustre_free_lsi */
2920                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
2921                 if (!lmd->lmd_opts)
2922                         RETURN(-ENOMEM);
2923                 strcpy(lmd->lmd_opts, options);
2924         }
2925
2926         lmd_print(lmd);
2927         lmd->lmd_magic = LMD_MAGIC;
2928
2929         RETURN(rc);
2930
2931 invalid:
2932         CERROR("Bad mount options %s\n", options);
2933         RETURN(-EINVAL);
2934 }
2935
2936 struct lustre_mount_data2 {
2937         void *lmd2_data;
2938         struct vfsmount *lmd2_mnt;
2939 };
2940
2941 /** This is the entry point for the mount call into Lustre.
2942  * This is called when a server or client is mounted,
2943  * and this is where we start setting things up.
2944  * @param data Mount options (e.g. -o flock,abort_recov)
2945  */
2946 int lustre_fill_super(struct super_block *sb, void *data, int silent)
2947 {
2948         struct lustre_mount_data *lmd;
2949         struct lustre_mount_data2 *lmd2 = data;
2950         struct lustre_sb_info *lsi;
2951         int rc;
2952         ENTRY;
2953
2954         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2955
2956         lsi = lustre_init_lsi(sb);
2957         if (!lsi)
2958                 RETURN(-ENOMEM);
2959         lmd = lsi->lsi_lmd;
2960
2961         /*
2962          * Disable lockdep during mount, because mount locking patterns are
2963          * `special'.
2964          */
2965         lockdep_off();
2966
2967         /*
2968          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
2969          */
2970         obd_zombie_barrier();
2971
2972         /* Figure out the lmd from the mount options */
2973         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
2974                 lustre_put_lsi(sb);
2975                 GOTO(out, rc = -EINVAL);
2976         }
2977
2978         if (lmd_is_client(lmd)) {
2979                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
2980                 if (!client_fill_super) {
2981                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
2982                                            "client mount! Is the 'lustre' "
2983                                            "module loaded?\n");
2984                         lustre_put_lsi(sb);
2985                         rc = -ENODEV;
2986                 } else {
2987                         rc = lustre_start_mgc(sb);
2988                         if (rc) {
2989                                 lustre_put_lsi(sb);
2990                                 GOTO(out, rc);
2991                         }
2992                         /* Connect and start */
2993                         /* (should always be ll_fill_super) */
2994                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
2995                         /* c_f_s will call lustre_common_put_super on failure */
2996                 }
2997         } else {
2998                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2999                 rc = server_fill_super(sb);
3000                 /* s_f_s calls lustre_start_mgc after the mount because we need
3001                    the MGS nids which are stored on disk.  Plus, we may
3002                    need to start the MGS first. */
3003                 /* s_f_s will call server_put_super on failure */
3004         }
3005
3006         /* If error happens in fill_super() call, @lsi will be killed there.
3007          * This is why we do not put it here. */
3008         GOTO(out, rc);
3009 out:
3010         if (rc) {
3011                 CERROR("Unable to mount %s (%d)\n",
3012                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
3013         } else {
3014                 CDEBUG(D_SUPER, "Mount %s complete\n",
3015                        lmd->lmd_dev);
3016         }
3017         lockdep_on();
3018         return rc;
3019 }
3020
3021
3022 /* We can't call ll_fill_super by name because it lives in a module that
3023    must be loaded after this one. */
3024 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
3025                                                   struct vfsmount *mnt))
3026 {
3027         client_fill_super = cfs;
3028 }
3029 EXPORT_SYMBOL(lustre_register_client_fill_super);
3030
3031 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
3032 {
3033         kill_super_cb = cfs;
3034 }
3035 EXPORT_SYMBOL(lustre_register_kill_super_cb);
3036
3037 /***************** FS registration ******************/
3038 #ifdef HAVE_FSTYPE_MOUNT
3039 struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
3040                                 const char *devname, void *data)
3041 {
3042         struct lustre_mount_data2 lmd2 = { data, NULL };
3043
3044         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
3045 }
3046 #else
3047 int lustre_get_sb(struct file_system_type *fs_type, int flags,
3048                   const char *devname, void * data, struct vfsmount *mnt)
3049 {
3050         struct lustre_mount_data2 lmd2 = { data, mnt };
3051
3052         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
3053 }
3054 #endif
3055
3056 void lustre_kill_super(struct super_block *sb)
3057 {
3058         struct lustre_sb_info *lsi = s2lsi(sb);
3059
3060         if (kill_super_cb && lsi && !IS_SERVER(lsi))
3061                 (*kill_super_cb)(sb);
3062
3063         kill_anon_super(sb);
3064 }
3065
3066 /** Register the "lustre" fs type
3067  */
3068 struct file_system_type lustre_fs_type = {
3069         .owner        = THIS_MODULE,
3070         .name         = "lustre",
3071 #ifdef HAVE_FSTYPE_MOUNT
3072         .mount        = lustre_mount,
3073 #else
3074         .get_sb       = lustre_get_sb,
3075 #endif
3076         .kill_sb      = lustre_kill_super,
3077         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
3078                         FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
3079 };
3080
3081 int lustre_register_fs(void)
3082 {
3083         return register_filesystem(&lustre_fs_type);
3084 }
3085
3086 int lustre_unregister_fs(void)
3087 {
3088         return unregister_filesystem(&lustre_fs_type);
3089 }
3090
3091 EXPORT_SYMBOL(server_mti_print);