Whamcloud - gitweb
LU-2644 build: fix 'resource leak' errors
[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                         OBD_FREE_PTR(ori);
973                         RETURN(-ENOMEM);
974                 }
975                 memcpy(uuid->uuid, ospname, strlen(ospname));
976                 *exp = cfs_hash_lookup(osp->obd_uuid_hash, uuid);
977                 OBD_FREE_PTR(uuid);
978         }
979
980         memcpy(ori->ori_name, ospname, strlen(ospname));
981         ori->ori_exp = exp;
982         ori->ori_cb_func = cb_func;
983         ori->ori_cb_data = cb_data;
984         CFS_INIT_LIST_HEAD(&ori->ori_list);
985         cfs_list_add(&ori->ori_list, &osp_register_list);
986
987         if (*exp != NULL && cb_func != NULL)
988                 cb_func(cb_data);
989
990         mutex_unlock(&osp_register_list_lock);
991         RETURN(0);
992 }
993 EXPORT_SYMBOL(lustre_register_osp_item);
994
995 void lustre_deregister_osp_item(struct obd_export **exp)
996 {
997         struct osp_register_item *ori, *tmp;
998
999         mutex_lock(&osp_register_list_lock);
1000         cfs_list_for_each_entry_safe(ori, tmp, &osp_register_list, ori_list) {
1001                 if (exp == ori->ori_exp) {
1002                         if (*exp)
1003                                 class_export_put(*exp);
1004                         cfs_list_del(&ori->ori_list);
1005                         OBD_FREE_PTR(ori);
1006                         break;
1007                 }
1008         }
1009         mutex_unlock(&osp_register_list_lock);
1010 }
1011 EXPORT_SYMBOL(lustre_deregister_osp_item);
1012
1013 static void lustre_notify_osp_list(struct obd_export *exp)
1014 {
1015         struct osp_register_item *ori, *tmp;
1016         LASSERT(exp != NULL);
1017
1018         mutex_lock(&osp_register_list_lock);
1019         cfs_list_for_each_entry_safe(ori, tmp, &osp_register_list, ori_list) {
1020                 if (strcmp(exp->exp_obd->obd_name, ori->ori_name))
1021                         continue;
1022                 if (*ori->ori_exp != NULL)
1023                         continue;
1024                 *ori->ori_exp = class_export_get(exp);
1025                 if (ori->ori_cb_func != NULL)
1026                         ori->ori_cb_func(ori->ori_cb_data);
1027         }
1028         mutex_unlock(&osp_register_list_lock);
1029 }
1030
1031 static int lustre_osp_connect(struct obd_device *osp)
1032 {
1033         struct lu_env            env;
1034         struct lu_context        session_ctx;
1035         struct obd_export       *exp;
1036         struct obd_uuid         *uuid = NULL;
1037         struct obd_connect_data *data = NULL;
1038         int                      rc;
1039         ENTRY;
1040
1041         /* log has been fully processed, let clients connect */
1042         rc = lu_env_init(&env, osp->obd_lu_dev->ld_type->ldt_ctx_tags);
1043         if (rc != 0)
1044                 RETURN(rc);
1045
1046         lu_context_init(&session_ctx, LCT_SESSION);
1047         session_ctx.lc_thread = NULL;
1048         lu_context_enter(&session_ctx);
1049         env.le_ses = &session_ctx;
1050
1051         OBD_ALLOC_PTR(data);
1052         if (data == NULL)
1053                 GOTO(out, rc = -ENOMEM);
1054
1055         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
1056         data->ocd_version = LUSTRE_VERSION_CODE;
1057         data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
1058         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_IBITS |
1059                                    OBD_CONNECT_MDS_MDS | OBD_CONNECT_FID |
1060                                    OBD_CONNECT_AT | OBD_CONNECT_FULL20 |
1061                                    OBD_CONNECT_LVB_TYPE |
1062                                    OBD_CONNECT_LIGHTWEIGHT;
1063         OBD_ALLOC_PTR(uuid);
1064         if (uuid == NULL)
1065                 GOTO(out, rc = -ENOMEM);
1066
1067         if (strlen(osp->obd_name) > sizeof(uuid->uuid)) {
1068                 CERROR("%s: Too long osp name %s, max_size is %d\n",
1069                        osp->obd_name, osp->obd_name, (int)sizeof(uuid->uuid));
1070                 GOTO(out, rc = -EINVAL);
1071         }
1072         /* Use osp name as the uuid, so we find the export by
1073          * osp name later */
1074         memcpy(uuid->uuid, osp->obd_name, strlen(osp->obd_name));
1075         rc = obd_connect(&env, &exp, osp, uuid, data, NULL);
1076         if (rc != 0)
1077                 CERROR("%s: connect failed: rc = %d\n", osp->obd_name, rc);
1078         else
1079                 lustre_notify_osp_list(exp);
1080
1081 out:
1082         if (data != NULL)
1083                 OBD_FREE_PTR(data);
1084         if (uuid != NULL)
1085                 OBD_FREE_PTR(uuid);
1086
1087         lu_env_fini(&env);
1088         lu_context_exit(&session_ctx);
1089         lu_context_fini(&session_ctx);
1090
1091         RETURN(rc);
1092 }
1093
1094 /**
1095  * osp-on-ost is used by slaves (Non-MDT0 targets) to manage the connection
1096  * to MDT0.
1097  *
1098  * The OSTs will communicate with MDT0 by the connection established by the
1099  * osp-on-ost to get quota and fid sequence.
1100  *
1101  **/
1102 static int lustre_osp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi)
1103 {
1104         struct obd_connect_data *data = NULL;
1105         struct obd_device       *obd;
1106         char                    *ospname = NULL;
1107         char                    *ospuuid = NULL;
1108         int                      rc;
1109         ENTRY;
1110
1111         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
1112                             lcfg->lcfg_nid);
1113         if (rc) {
1114                 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
1115                 GOTO(out, rc);
1116         }
1117
1118         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1119         if (ospname == NULL)
1120                 GOTO(out, rc = -ENOMEM);
1121
1122         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1123         if (rc != 0) {
1124                 CERROR("%s change ospname error: rc %d\n",
1125                        lsi->lsi_svname, rc);
1126                 GOTO(out, rc);
1127         }
1128
1129         OBD_ALLOC(ospuuid, MTI_NAME_MAXLEN);
1130         if (ospuuid == NULL)
1131                 GOTO(out, rc = -ENOMEM);
1132
1133         sprintf(ospuuid, "%s_UUID", ospname);
1134         rc = lustre_start_simple(ospname, LUSTRE_OSP_NAME,
1135                                  ospuuid, lustre_cfg_string(lcfg, 1),
1136                                  0, 0, 0);
1137         if (rc) {
1138                 CERROR("%s: setup up failed: rc %d\n", ospname, rc);
1139                 GOTO(out, rc);
1140         }
1141
1142         obd = class_name2obd(ospname);
1143         LASSERT(obd != NULL);
1144
1145         rc = lustre_osp_connect(obd);
1146         if (rc != 0)
1147                 CERROR("%s: connect failed: rc = %d\n", ospname, rc);
1148 out:
1149         if (data != NULL)
1150                 OBD_FREE_PTR(data);
1151         if (ospname != NULL)
1152                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1153         if (ospuuid != NULL)
1154                 OBD_FREE(ospuuid, MTI_NAME_MAXLEN);
1155
1156         RETURN(rc);
1157 }
1158
1159 static int lustre_osp_add_conn(struct lustre_cfg *cfg,
1160                                struct lustre_sb_info *lsi)
1161 {
1162         struct lustre_cfg_bufs *bufs = NULL;
1163         struct lustre_cfg      *lcfg = NULL;
1164         char                   *ospname = NULL;
1165         struct obd_device      *osp;
1166         int                     rc;
1167         ENTRY;
1168
1169         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1170         if (ospname == NULL)
1171                 GOTO(out, rc = -ENOMEM);
1172
1173         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1174         if (rc != 0) {
1175                 CERROR("%s change ospname error: rc %d\n",
1176                        lsi->lsi_svname, rc);
1177                 GOTO(out, rc);
1178         }
1179
1180         OBD_ALLOC_PTR(bufs);
1181         if (bufs == NULL)
1182                 GOTO(out, rc = -ENOMEM);
1183
1184         lustre_cfg_bufs_reset(bufs, ospname);
1185         lustre_cfg_bufs_set_string(bufs, 1,
1186                                    lustre_cfg_string(cfg, 1));
1187
1188         lcfg = lustre_cfg_new(LCFG_ADD_CONN, bufs);
1189
1190         osp = class_name2obd(ospname);
1191         if (osp == NULL) {
1192                 CERROR("Can not find %s\n", ospname);
1193                 GOTO(out, rc = -EINVAL);
1194         }
1195
1196         rc = class_add_conn(osp, lcfg);
1197         if (rc)
1198                 CERROR("%s: can't add conn: rc = %d\n", ospname, rc);
1199
1200 out:
1201         if (bufs != NULL)
1202                 OBD_FREE_PTR(bufs);
1203         if (lcfg != NULL)
1204                 lustre_cfg_free(lcfg);
1205         if (ospname != NULL)
1206                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1207
1208         RETURN(rc);
1209 }
1210
1211 /**
1212  * Retrieve MDT nids from the client log, then start the osp-on-ost device.
1213  * there are only two scenarios which would include mdt nid.
1214  * 1.
1215  * marker   5 (flags=0x01, v2.1.54.0) lustre-MDT0000  'add mdc' xxx-
1216  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:192.168.122.162@tcp
1217  * attach    0:lustre-MDT0000-mdc  1:mdc  2:lustre-clilmv_UUID
1218  * setup     0:lustre-MDT0000-mdc  1:lustre-MDT0000_UUID  2:192.168.122.162@tcp
1219  * add_uuid  nid=192.168.172.1@tcp(0x20000c0a8ac01)  0:  1:192.168.172.1@tcp
1220  * add_conn  0:lustre-MDT0000-mdc  1:192.168.172.1@tcp
1221  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID xxxx
1222  * marker   5 (flags=0x02, v2.1.54.0) lustre-MDT0000  'add mdc' xxxx-
1223  * 2.
1224  * marker   7 (flags=0x01, v2.1.54.0) lustre-MDT0000  'add failnid' xxxx-
1225  * add_uuid  nid=192.168.122.2@tcp(0x20000c0a87a02)  0:  1:192.168.122.2@tcp
1226  * add_conn  0:lustre-MDT0000-mdc  1:192.168.122.2@tcp
1227  * marker   7 (flags=0x02, v2.1.54.0) lustre-MDT0000  'add failnid' xxxx-
1228 **/
1229 static int client_osp_config_process(const struct lu_env *env,
1230                                      struct llog_handle *handle,
1231                                      struct llog_rec_hdr *rec, void *data)
1232 {
1233         struct config_llog_instance *clli = data;
1234         int                          cfg_len = rec->lrh_len;
1235         char                        *cfg_buf = (char *) (rec + 1);
1236         struct lustre_cfg           *lcfg = NULL;
1237         struct lustre_sb_info       *lsi;
1238         int                          rc = 0, swab = 0;
1239         ENTRY;
1240
1241         if (rec->lrh_type != OBD_CFG_REC) {
1242                 CERROR("Unknown llog record type %#x encountered\n",
1243                        rec->lrh_type);
1244                 RETURN(-EINVAL);
1245         }
1246
1247         LASSERT(clli->cfg_sb != NULL);
1248         lsi = s2lsi(clli->cfg_sb);
1249
1250         lcfg = (struct lustre_cfg *)cfg_buf;
1251         if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1252                 lustre_swab_lustre_cfg(lcfg);
1253                 swab = 1;
1254         }
1255
1256         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1257         if (rc)
1258                 GOTO(out, rc);
1259
1260         switch (lcfg->lcfg_command) {
1261         case LCFG_MARKER: {
1262                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1263
1264                 lustre_swab_cfg_marker(marker, swab,
1265                                        LUSTRE_CFG_BUFLEN(lcfg, 1));
1266                 if (marker->cm_flags & CM_SKIP ||
1267                     marker->cm_flags & CM_EXCLUDE)
1268                         GOTO(out, rc = 0);
1269
1270                 if (!tgt_is_mdt0(marker->cm_tgtname))
1271                         GOTO(out, rc = 0);
1272
1273                 if(!strncmp(marker->cm_comment, "add mdc", 7) ||
1274                    !strncmp(marker->cm_comment, "add failnid", 11)) {
1275                         if (marker->cm_flags & CM_START) {
1276                                 clli->cfg_flags = CFG_F_MARKER;
1277                                 /* This hack is to differentiate the
1278                                  * ADD_UUID is come from "add mdc" record
1279                                  * or from "add failnid" record. */
1280                                 if (!strncmp(marker->cm_comment,
1281                                              "add failnid", 11))
1282                                         clli->cfg_flags |= CFG_F_SKIP;
1283                         } else if (marker->cm_flags & CM_END) {
1284                                 clli->cfg_flags = 0;
1285                         }
1286                 }
1287                 break;
1288         }
1289         case LCFG_ADD_UUID: {
1290                 if (clli->cfg_flags == CFG_F_MARKER) {
1291                         rc = lustre_osp_setup(lcfg, lsi);
1292                         /* XXX: process only the first nid as
1293                          * we don't need another instance of osp */
1294                         clli->cfg_flags |= CFG_F_SKIP;
1295                 } else if (clli->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
1296                         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
1297                                         lcfg->lcfg_nid);
1298                         if (rc)
1299                                 CERROR("%s: Fail to add uuid, rc:%d\n",
1300                                        lsi->lsi_svname, rc);
1301                 }
1302                 break;
1303         }
1304         case LCFG_ADD_CONN: {
1305                 if (is_mdc_for_mdt0(lustre_cfg_string(lcfg, 0)))
1306                         rc = lustre_osp_add_conn(lcfg, lsi);
1307                 break;
1308         }
1309         default:
1310                 break;
1311         }
1312 out:
1313         RETURN(rc);
1314 }
1315
1316 static int lustre_disconnect_osp(struct super_block *sb)
1317 {
1318         struct lustre_sb_info           *lsi = s2lsi(sb);
1319         struct obd_device               *osp;
1320         char                            *ospname = NULL;
1321         char                            *logname = NULL;
1322         struct lustre_cfg               *lcfg = NULL;
1323         struct lustre_cfg_bufs          *bufs = NULL;
1324         struct config_llog_instance     *cfg = NULL;
1325         int                              rc;
1326         ENTRY;
1327
1328         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1329         if (IS_MDT(lsi)) {
1330                 int     index;
1331
1332                 /* Only disconnect MDT0-osp-MDT0 here, other osp on MDT
1333                  * will be disconnect during MDT stack cleanup.
1334                  * FIXME: remove later when quota on DNE is finished */
1335                 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1336                 if (rc < 0)
1337                         RETURN(rc);
1338                 if (index != 0)
1339                         RETURN(0);
1340         }
1341         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1342         if (logname == NULL)
1343                 RETURN(-ENOMEM);
1344
1345         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1346         if (ospname == NULL)
1347                 GOTO(out, rc = -ENOMEM);
1348
1349         rc = server_name2fsname(lsi->lsi_svname, ospname, NULL);
1350         if (rc != 0) {
1351                 CERROR("%s: get fsname error: %d\n",
1352                        lsi->lsi_svname, rc);
1353                 GOTO(out, rc);
1354         }
1355         sprintf(logname, "%s-client", ospname);
1356
1357         OBD_ALLOC_PTR(cfg);
1358         if (cfg == NULL)
1359                 GOTO(out, rc = -ENOMEM);
1360
1361         /* end log first */
1362         cfg->cfg_instance = sb;
1363         rc = lustre_end_log(sb, logname, cfg);
1364         if (rc != 0) {
1365                 CERROR("Can't end config log %s\n", ospname);
1366                 GOTO(out, rc);
1367         }
1368
1369         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1370         if (rc != 0) {
1371                 CERROR("%s: get osp name error: %d\n",
1372                        lsi->lsi_svname, rc);
1373                 GOTO(out, rc);
1374         }
1375
1376         osp = class_name2obd(ospname);
1377         if (osp == NULL) {
1378                 CERROR("Can't find osp-on-ost %s\n", ospname);
1379                 GOTO(out, rc = -ENOENT);
1380         }
1381
1382         OBD_ALLOC_PTR(bufs);
1383         if (bufs == NULL)
1384                 GOTO(out, rc = -ENOMEM);
1385
1386         lustre_cfg_bufs_reset(bufs, osp->obd_name);
1387         lustre_cfg_bufs_set_string(bufs, 1, NULL);
1388         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
1389         if (!lcfg)
1390                 GOTO(out, rc = -ENOMEM);
1391
1392         /* Disconnect import first. NULL is passed for the '@env', since
1393          * it will not be used for the 'osp-on-ost'. (see osp_shutdown()) */
1394         rc = osp->obd_lu_dev->ld_ops->ldo_process_config(NULL, osp->obd_lu_dev,
1395                                                          lcfg);
1396 out:
1397         if (lcfg)
1398                 lustre_cfg_free(lcfg);
1399         if (bufs)
1400                 OBD_FREE_PTR(bufs);
1401         if (cfg)
1402                 OBD_FREE_PTR(cfg);
1403         if (ospname)
1404                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1405         if (logname)
1406                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1407         RETURN(rc);
1408 }
1409
1410 /**
1411  * Stop the osp(fsname-MDT0000-osp-OSTxxxx) or (fsname-MDT0000-osp-MDT0000) for an OST target.
1412  **/
1413 static int lustre_stop_osp(struct super_block *sb)
1414 {
1415         struct lustre_sb_info   *lsi = s2lsi(sb);
1416         struct obd_device       *osp = NULL;
1417         char                    *ospname = NULL;
1418         int                      rc = 0;
1419         ENTRY;
1420
1421         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1422         if (IS_MDT(lsi)) {
1423                 int     index;
1424
1425                 /* Only disconnect MDT0-osp-MDT0 here, other osp on MDT
1426                  * will be disconnect during MDT stack cleanup.
1427                  * FIXME: remove later when quota on DNE is finished */
1428                 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1429                 if (rc < 0)
1430                         RETURN(rc);
1431                 if (index != 0)
1432                         RETURN(0);
1433         }
1434
1435         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1436         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1437         if (rc != 0) {
1438                 CERROR("%s get fsname error: rc %d\n",
1439                        lsi->lsi_svname, rc);
1440                 GOTO(cleanup, rc);
1441         }
1442
1443         osp = class_name2obd(ospname);
1444         if (osp == NULL) {
1445                 CERROR("Can not find osp-on-ost %s\n", ospname);
1446                 GOTO(cleanup, rc = -ENOENT);
1447         }
1448
1449         osp->obd_force = 1;
1450         rc = class_manual_cleanup(osp);
1451
1452 cleanup:
1453         if (ospname != NULL)
1454                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1455         RETURN(rc);
1456 }
1457
1458 DEFINE_MUTEX(server_start_lock);
1459
1460 /* Stop MDS/OSS if nobody is using them */
1461 static int server_stop_servers(int lsiflags)
1462 {
1463         struct obd_device *obd = NULL;
1464         struct obd_type *type = NULL;
1465         int rc = 0;
1466         ENTRY;
1467
1468         mutex_lock(&server_start_lock);
1469
1470         /* Either an MDT or an OST or neither  */
1471         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1472         if ((lsiflags & LDD_F_SV_TYPE_MDT) &&
1473             (obd = class_name2obd(LUSTRE_MDS_OBDNAME))) {
1474                 type = class_search_type(LUSTRE_MDT_NAME);
1475         }
1476        /* if this was an OST, and there are no more OST's, clean up the OSS */
1477         if ((lsiflags & LDD_F_SV_TYPE_OST) &&
1478             (obd = class_name2obd(LUSTRE_OSS_OBDNAME))) {
1479                 type = class_search_type(LUSTRE_OST_NAME);
1480         }
1481
1482         if (obd && (!type || !type->typ_refcnt)) {
1483                 int err;
1484                 obd->obd_force = 1;
1485                 /* obd_fail doesn't mean much on a server obd */
1486                 err = class_manual_cleanup(obd);
1487                 if (!rc)
1488                         rc = err;
1489         }
1490
1491         mutex_unlock(&server_start_lock);
1492
1493         RETURN(rc);
1494 }
1495
1496 int server_mti_print(char *title, struct mgs_target_info *mti)
1497 {
1498         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1499         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1500         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
1501         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
1502         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
1503                   mti->mti_config_ver, mti->mti_flags);
1504         return(0);
1505 }
1506
1507 /**
1508  * Get service name (svname) from string
1509  * rc < 0 on error
1510  * if endptr isn't NULL it is set to end of fsname *
1511  */
1512 int server_name2svname(char *label, char *svname, char **endptr)
1513 {
1514         int rc;
1515         char *dash;
1516
1517         /* We use server_name2fsname() just for parsing */
1518         rc = server_name2fsname(label, NULL, &dash);
1519         if (rc != 0)
1520                 return rc;
1521
1522         if (*dash != '-')
1523                 return -1;
1524
1525         strncpy(svname, dash + 1, MTI_NAME_MAXLEN);
1526
1527         return 0;
1528 }
1529 EXPORT_SYMBOL(server_name2svname);
1530
1531
1532 /* Get the index from the obd name.
1533    rc = server type, or
1534    rc < 0  on error
1535    if endptr isn't NULL it is set to end of name */
1536 int server_name2index(char *svname, __u32 *idx, char **endptr)
1537 {
1538         unsigned long index;
1539         int rc;
1540         char *dash;
1541
1542         /* We use server_name2fsname() just for parsing */
1543         rc = server_name2fsname(svname, NULL, &dash);
1544         if (rc != 0)
1545                 return rc;
1546
1547         if (*dash != '-')
1548                 return -EINVAL;
1549
1550         dash++;
1551
1552         if (strncmp(dash, "MDT", 3) == 0)
1553                 rc = LDD_F_SV_TYPE_MDT;
1554         else if (strncmp(dash, "OST", 3) == 0)
1555                 rc = LDD_F_SV_TYPE_OST;
1556         else
1557                 return -EINVAL;
1558
1559         dash += 3;
1560
1561         if (strcmp(dash, "all") == 0)
1562                 return rc | LDD_F_SV_ALL;
1563
1564         index = simple_strtoul(dash, endptr, 16);
1565         *idx = index;
1566
1567         return rc;
1568 }
1569 EXPORT_SYMBOL(server_name2index);
1570
1571 /* Generate data for registration */
1572 static int server_lsi2mti(struct lustre_sb_info *lsi,
1573                           struct mgs_target_info *mti)
1574 {
1575         lnet_process_id_t id;
1576         int rc, i = 0;
1577         ENTRY;
1578
1579         if (!IS_SERVER(lsi))
1580                 RETURN(-EINVAL);
1581
1582         strncpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname));
1583
1584         mti->mti_nid_count = 0;
1585         while (LNetGetId(i++, &id) != -ENOENT) {
1586                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
1587                         continue;
1588
1589                 /* server use --servicenode param, only allow specified
1590                  * nids be registered */
1591                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) != 0 &&
1592                     class_match_nid(lsi->lsi_lmd->lmd_params,
1593                                     PARAM_FAILNODE, id.nid) < 1)
1594                         continue;
1595
1596                 /* match specified network */
1597                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1598                                      PARAM_NETWORK, LNET_NIDNET(id.nid)))
1599                         continue;
1600
1601                 mti->mti_nids[mti->mti_nid_count] = id.nid;
1602                 mti->mti_nid_count++;
1603                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1604                         CWARN("Only using first %d nids for %s\n",
1605                               mti->mti_nid_count, mti->mti_svname);
1606                         break;
1607                 }
1608         }
1609
1610         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1611         mti->mti_config_ver = 0;
1612
1613         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1614         if (rc != 0)
1615                 return rc;
1616
1617         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1618         if (rc < 0)
1619                 return rc;
1620         /* Orion requires index to be set */
1621         LASSERT(!(rc & LDD_F_NEED_INDEX));
1622         /* keep only LDD flags */
1623         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1624         if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1625                 mti->mti_flags |= LDD_F_UPDATE;
1626         strncpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1627                 sizeof(mti->mti_params));
1628         return 0;
1629 }
1630
1631 /* Register an old or new target with the MGS. If needed MGS will construct
1632    startup logs and assign index */
1633 static int server_register_target(struct lustre_sb_info *lsi)
1634 {
1635         struct obd_device *mgc = lsi->lsi_mgc;
1636         struct mgs_target_info *mti = NULL;
1637         bool writeconf;
1638         int rc;
1639         ENTRY;
1640
1641         LASSERT(mgc);
1642
1643         if (!IS_SERVER(lsi))
1644                 RETURN(-EINVAL);
1645
1646         OBD_ALLOC_PTR(mti);
1647         if (!mti)
1648                 RETURN(-ENOMEM);
1649
1650         rc = server_lsi2mti(lsi, mti);
1651         if (rc)
1652                 GOTO(out, rc);
1653
1654         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1655                mti->mti_svname, mti->mti_fsname,
1656                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1657                mti->mti_flags);
1658
1659         /* if write_conf is true, the registration must succeed */
1660         writeconf = !!(lsi->lsi_flags & (LDD_F_NEED_INDEX | LDD_F_UPDATE));
1661         mti->mti_flags |= LDD_F_OPC_REG;
1662
1663         /* Register the target */
1664         /* FIXME use mgc_process_config instead */
1665         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1666                                 sizeof(KEY_REGISTER_TARGET), KEY_REGISTER_TARGET,
1667                                 sizeof(*mti), mti, NULL);
1668         if (rc) {
1669                 if (mti->mti_flags & LDD_F_ERROR) {
1670                         LCONSOLE_ERROR_MSG(0x160,
1671                                 "The MGS is refusing to allow this "
1672                                 "server (%s) to start. Please see messages"
1673                                 " on the MGS node.\n", lsi->lsi_svname);
1674                 } else if (writeconf) {
1675                         LCONSOLE_ERROR_MSG(0x15f,
1676                                 "Communication to the MGS return error %d. "
1677                                 "Is the MGS running?\n", rc);
1678                 } else {
1679                         CERROR("Cannot talk to the MGS: %d, not fatal\n", rc);
1680                         /* reset the error code for non-fatal error. */
1681                         rc = 0;
1682                 }
1683                 GOTO(out, rc);
1684         }
1685
1686 out:
1687         if (mti)
1688                 OBD_FREE_PTR(mti);
1689         RETURN(rc);
1690 }
1691
1692 /**
1693  * Notify the MGS that this target is ready.
1694  * Used by IR - if the MGS receives this message, it will notify clients.
1695  */
1696 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1697 {
1698         struct lustre_sb_info *lsi = s2lsi(sb);
1699         struct obd_device *mgc = lsi->lsi_mgc;
1700         struct mgs_target_info *mti = NULL;
1701         int rc;
1702         ENTRY;
1703
1704         LASSERT(mgc);
1705
1706         if (!(IS_SERVER(lsi)))
1707                 RETURN(-EINVAL);
1708
1709         OBD_ALLOC_PTR(mti);
1710         if (!mti)
1711                 RETURN(-ENOMEM);
1712         rc = server_lsi2mti(lsi, mti);
1713         if (rc)
1714                 GOTO(out, rc);
1715
1716         mti->mti_instance = obd->u.obt.obt_instance;
1717         mti->mti_flags |= LDD_F_OPC_READY;
1718
1719         /* FIXME use mgc_process_config instead */
1720         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1721                                 sizeof(KEY_REGISTER_TARGET),
1722                                 KEY_REGISTER_TARGET,
1723                                 sizeof(*mti), mti, NULL);
1724
1725         /* Imperative recovery: if the mgs informs us to use IR? */
1726         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1727             (mti->mti_flags & LDD_F_IR_CAPABLE))
1728                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1729
1730 out:
1731         if (mti)
1732                 OBD_FREE_PTR(mti);
1733         RETURN(rc);
1734
1735 }
1736
1737 /**
1738  * Start the osp(fsname-MDT0000-osp-OSTxxxx) for an OST target,
1739  * which would be used to communicate with MDT0 for quota and FID.
1740  **/
1741 static int lustre_start_osp(struct super_block *sb)
1742 {
1743         struct lustre_sb_info       *lsi = s2lsi(sb);
1744         struct config_llog_instance *cfg = NULL;
1745         struct obd_device           *osp;
1746         char                        *ospname = NULL;
1747         char                        *logname = NULL;
1748         char                        *tgt;
1749         int                          rc;
1750         ENTRY;
1751
1752         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
1753         OBD_ALLOC(ospname, MTI_NAME_MAXLEN);
1754         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1755         if (ospname == NULL || logname == NULL)
1756                 GOTO(cleanup, rc = -ENOMEM);
1757
1758         rc = server_name2fsname(lsi->lsi_svname, ospname, &tgt);
1759         if (rc != 0) {
1760                 CERROR("%s change fsname error: rc %d\n",
1761                        lsi->lsi_svname, rc);
1762                 GOTO(cleanup, rc);
1763         }
1764         sprintf(logname, "%s-client", ospname);
1765
1766         rc = tgt_name2ospname(lsi->lsi_svname, ospname);
1767         if (rc != 0) {
1768                 CERROR("%s change ospname error: rc %d\n",
1769                        lsi->lsi_svname, rc);
1770                 GOTO(cleanup, rc);
1771         }
1772
1773         osp = class_name2obd(ospname);
1774         if (osp != NULL)
1775                 GOTO(cleanup, rc = 0);
1776
1777         OBD_ALLOC_PTR(cfg);
1778         if (cfg == NULL)
1779                 GOTO(cleanup, rc = -ENOMEM);
1780
1781         cfg->cfg_callback = client_osp_config_process;
1782         cfg->cfg_instance = sb;
1783
1784         rc = lustre_process_log(sb, logname, cfg);
1785
1786 cleanup:
1787         if (ospname != NULL)
1788                 OBD_FREE(ospname, MTI_NAME_MAXLEN);
1789         if (logname != NULL)
1790                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1791         if (cfg != NULL)
1792                 OBD_FREE_PTR(cfg);
1793
1794         RETURN(rc);
1795 }
1796
1797 /** Start server targets: MDTs and OSTs
1798  */
1799 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
1800 {
1801         struct obd_device *obd;
1802         struct lustre_sb_info *lsi = s2lsi(sb);
1803         struct config_llog_instance cfg;
1804         struct lu_env env;
1805         struct lu_device *dev;
1806         int rc;
1807         ENTRY;
1808
1809         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1810
1811         if (IS_MDT(lsi)) {
1812                 /* make sure the MDS is started */
1813                 mutex_lock(&server_start_lock);
1814                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1815                 if (!obd) {
1816                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1817                                                  LUSTRE_MDS_NAME,
1818                                                  LUSTRE_MDS_OBDNAME"_uuid",
1819                                                  0, 0, 0, 0);
1820                         if (rc) {
1821                                 mutex_unlock(&server_start_lock);
1822                                 CERROR("failed to start MDS: %d\n", rc);
1823                                 RETURN(rc);
1824                         }
1825                 }
1826                 mutex_unlock(&server_start_lock);
1827         }
1828
1829         /* If we're an OST, make sure the global OSS is running */
1830         if (IS_OST(lsi)) {
1831                 /* make sure OSS is started */
1832                 mutex_lock(&server_start_lock);
1833                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1834                 if (!obd) {
1835                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1836                                                  LUSTRE_OSS_NAME,
1837                                                  LUSTRE_OSS_OBDNAME"_uuid",
1838                                                  0, 0, 0, 0);
1839                         if (rc) {
1840                                 mutex_unlock(&server_start_lock);
1841                                 CERROR("failed to start OSS: %d\n", rc);
1842                                 RETURN(rc);
1843                         }
1844                 }
1845                 mutex_unlock(&server_start_lock);
1846         }
1847
1848         /* Set the mgc fs to our server disk.  This allows the MGC to
1849          * read and write configs locally, in case it can't talk to the MGS. */
1850         if (lsi->lsi_srv_mnt) {
1851                 rc = server_mgc_set_fs(lsi->lsi_mgc, sb);
1852                 if (rc)
1853                         GOTO(out_stop_service, rc);
1854         }
1855
1856         /* Register with MGS */
1857         rc = server_register_target(lsi);
1858         if (rc)
1859                 GOTO(out_mgc, rc);
1860
1861         /* Let the target look up the mount using the target's name
1862            (we can't pass the sb or mnt through class_process_config.) */
1863         rc = server_register_mount(lsi->lsi_svname, sb, mnt);
1864         if (rc)
1865                 GOTO(out_mgc, rc);
1866
1867         /* Start targets using the llog named for the target */
1868         memset(&cfg, 0, sizeof(cfg));
1869         cfg.cfg_callback = class_config_llog_handler;
1870         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1871         if (rc) {
1872                 CERROR("failed to start server %s: %d\n",
1873                        lsi->lsi_svname, rc);
1874                 /* Do NOT call server_deregister_mount() here. This makes it
1875                  * impossible to find mount later in cleanup time and leaves
1876                  * @lsi and othder stuff leaked. -umka */
1877                 GOTO(out_mgc, rc);
1878         }
1879
1880         obd = class_name2obd(lsi->lsi_svname);
1881         if (!obd) {
1882                 CERROR("no server named %s was started\n", lsi->lsi_svname);
1883                 GOTO(out_mgc, rc = -ENXIO);
1884         }
1885
1886         if (IS_OST(lsi) || IS_MDT(lsi)) {
1887                 rc = lustre_start_osp(sb);
1888                 if (rc) {
1889                         CERROR("%s: failed to start OSP: %d\n",
1890                                lsi->lsi_svname, rc);
1891                         GOTO(out_mgc, rc);
1892                 }
1893         }
1894
1895         server_notify_target(sb, obd);
1896
1897         /* calculate recovery timeout, do it after lustre_process_log */
1898         server_calc_timeout(lsi, obd);
1899
1900         /* log has been fully processed */
1901         obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1902
1903         /* log has been fully processed, let clients connect */
1904         dev = obd->obd_lu_dev;
1905         if (dev && dev->ld_ops->ldo_prepare) {
1906                 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1907                 if (rc == 0) {
1908                         struct lu_context  session_ctx;
1909
1910                         lu_context_init(&session_ctx, LCT_SESSION);
1911                         session_ctx.lc_thread = NULL;
1912                         lu_context_enter(&session_ctx);
1913                         env.le_ses = &session_ctx;
1914
1915                         dev->ld_ops->ldo_prepare(&env, NULL, dev);
1916
1917                         lu_env_fini(&env);
1918                         lu_context_exit(&session_ctx);
1919                         lu_context_fini(&session_ctx);
1920                 }
1921         }
1922
1923         /* abort recovery only on the complete stack:
1924          * many devices can be involved */
1925         if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1926             (OBP(obd, iocontrol))) {
1927                 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1928                               NULL, NULL);
1929         }
1930
1931 out_mgc:
1932         /* Release the mgc fs for others to use */
1933         if (lsi->lsi_srv_mnt)
1934                 server_mgc_clear_fs(lsi->lsi_mgc);
1935
1936 out_stop_service:
1937         if (rc != 0)
1938                 server_stop_servers(lsi->lsi_flags);
1939
1940         RETURN(rc);
1941 }
1942
1943 /***************** lustre superblock **************/
1944
1945 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
1946 {
1947         struct lustre_sb_info *lsi;
1948         ENTRY;
1949
1950         OBD_ALLOC_PTR(lsi);
1951         if (!lsi)
1952                 RETURN(NULL);
1953         OBD_ALLOC_PTR(lsi->lsi_lmd);
1954         if (!lsi->lsi_lmd) {
1955                 OBD_FREE_PTR(lsi);
1956                 RETURN(NULL);
1957         }
1958
1959         lsi->lsi_lmd->lmd_exclude_count = 0;
1960         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
1961         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
1962         s2lsi_nocast(sb) = lsi;
1963         /* we take 1 extra ref for our setup */
1964         cfs_atomic_set(&lsi->lsi_mounts, 1);
1965
1966         /* Default umount style */
1967         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
1968
1969         RETURN(lsi);
1970 }
1971
1972 static int lustre_free_lsi(struct super_block *sb)
1973 {
1974         struct lustre_sb_info *lsi = s2lsi(sb);
1975         ENTRY;
1976
1977         LASSERT(lsi != NULL);
1978         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
1979
1980         /* someone didn't call server_put_mount. */
1981         LASSERT(cfs_atomic_read(&lsi->lsi_mounts) == 0);
1982
1983         if (lsi->lsi_lmd != NULL) {
1984                 if (lsi->lsi_lmd->lmd_dev != NULL)
1985                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
1986                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1987                 if (lsi->lsi_lmd->lmd_profile != NULL)
1988                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
1989                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
1990                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
1991                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
1992                                  strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
1993                 if (lsi->lsi_lmd->lmd_opts != NULL)
1994                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
1995                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1996                 if (lsi->lsi_lmd->lmd_exclude_count)
1997                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
1998                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
1999                                  lsi->lsi_lmd->lmd_exclude_count);
2000                 if (lsi->lsi_lmd->lmd_mgs != NULL)
2001                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
2002                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
2003                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
2004                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
2005                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
2006                 if (lsi->lsi_lmd->lmd_params != NULL)
2007                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
2008
2009                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
2010         }
2011
2012         LASSERT(lsi->lsi_llsbi == NULL);
2013         OBD_FREE(lsi, sizeof(*lsi));
2014         s2lsi_nocast(sb) = NULL;
2015
2016         RETURN(0);
2017 }
2018
2019 /* The lsi has one reference for every server that is using the disk -
2020    e.g. MDT, MGS, and potentially MGC */
2021 static int lustre_put_lsi(struct super_block *sb)
2022 {
2023         struct lustre_sb_info *lsi = s2lsi(sb);
2024         ENTRY;
2025
2026         LASSERT(lsi != NULL);
2027
2028         CDEBUG(D_MOUNT, "put %p %d\n", sb, cfs_atomic_read(&lsi->lsi_mounts));
2029         if (cfs_atomic_dec_and_test(&lsi->lsi_mounts)) {
2030                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
2031                         obd_disconnect(lsi->lsi_osd_exp);
2032                         /* wait till OSD is gone */
2033                         obd_zombie_barrier();
2034                 }
2035                 lustre_free_lsi(sb);
2036                 RETURN(1);
2037         }
2038         RETURN(0);
2039 }
2040
2041 static int lsi_prepare(struct lustre_sb_info *lsi)
2042 {
2043         __u32 index;
2044         int rc;
2045         ENTRY;
2046
2047         LASSERT(lsi);
2048         LASSERT(lsi->lsi_lmd);
2049
2050         /* The server name is given as a mount line option */
2051         if (lsi->lsi_lmd->lmd_profile == NULL) {
2052                 LCONSOLE_ERROR("Can't determine server name\n");
2053                 RETURN(-EINVAL);
2054         }
2055
2056         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname))
2057                 RETURN(-ENAMETOOLONG);
2058
2059         strcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile);
2060
2061         /* Determine osd type */
2062         if (lsi->lsi_lmd->lmd_osd_type != NULL) {
2063                 if (strlen(lsi->lsi_lmd->lmd_osd_type) >=
2064                            sizeof(lsi->lsi_osd_type))
2065                         RETURN(-ENAMETOOLONG);
2066
2067                 strcpy(lsi->lsi_osd_type, lsi->lsi_lmd->lmd_osd_type);
2068         } else {
2069                 strcpy(lsi->lsi_osd_type, LUSTRE_OSD_LDISKFS_NAME);
2070         }
2071
2072         /* XXX: a temp. solution for components using fsfilt
2073          *      to be removed in one of the subsequent patches */
2074         if (!strcmp(lsi->lsi_lmd->lmd_osd_type, "osd-ldiskfs")) {
2075                 strcpy(lsi->lsi_fstype, "ldiskfs");
2076         } else {
2077                 strcpy(lsi->lsi_fstype, lsi->lsi_lmd->lmd_osd_type);
2078         }
2079
2080         /* Determine server type */
2081         rc = server_name2index(lsi->lsi_svname, &index, NULL);
2082         if (rc < 0) {
2083                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) {
2084                         /* Assume we're a bare MGS */
2085                         rc = 0;
2086                         lsi->lsi_lmd->lmd_flags |= LMD_FLG_NOSVC;
2087                 } else {
2088                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
2089                                        lsi->lsi_svname);
2090                         RETURN(rc);
2091                 }
2092         }
2093         lsi->lsi_flags |= rc;
2094
2095         /* Add mount line flags that used to be in ldd:
2096          * writeconf, mgs, anything else?
2097          */
2098         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_WRITECONF) ?
2099                 LDD_F_WRITECONF : 0;
2100         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_VIRGIN) ?
2101                 LDD_F_VIRGIN : 0;
2102         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) ?
2103                 LDD_F_SV_TYPE_MGS : 0;
2104         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) ?
2105                 LDD_F_NO_PRIMNODE : 0;
2106
2107         RETURN(0);
2108 }
2109
2110 /*************** server mount ******************/
2111
2112 /** Start the shutdown of servers at umount.
2113  */
2114 static void server_put_super(struct super_block *sb)
2115 {
2116         struct lustre_sb_info *lsi = s2lsi(sb);
2117         struct obd_device     *obd;
2118         char *tmpname, *extraname = NULL;
2119         int tmpname_sz;
2120         int lsiflags = lsi->lsi_flags;
2121         ENTRY;
2122
2123         LASSERT(IS_SERVER(lsi));
2124
2125         tmpname_sz = strlen(lsi->lsi_svname) + 1;
2126         OBD_ALLOC(tmpname, tmpname_sz);
2127         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
2128         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
2129         if (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC))
2130                 snprintf(tmpname, tmpname_sz, "MGS");
2131
2132         /* disconnect the osp-on-ost first to drain off the inflight request */
2133         if (IS_OST(lsi) || IS_MDT(lsi)) {
2134                 int     rc;
2135
2136                 rc = lustre_disconnect_osp(sb);
2137                 if (rc && rc != ETIMEDOUT)
2138                         CERROR("%s: failed to disconnect osp-on-ost (rc=%d)!\n",
2139                                tmpname, rc);
2140         }
2141
2142         /* Stop the target */
2143         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
2144             (IS_MDT(lsi) || IS_OST(lsi))) {
2145                 struct lustre_profile *lprof = NULL;
2146
2147                 /* tell the mgc to drop the config log */
2148                 lustre_end_log(sb, lsi->lsi_svname, NULL);
2149
2150                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
2151                    If there are any setup/cleanup errors, save the lov
2152                    name for safety cleanup later. */
2153                 lprof = class_get_profile(lsi->lsi_svname);
2154                 if (lprof && lprof->lp_dt) {
2155                         OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
2156                         strcpy(extraname, lprof->lp_dt);
2157                 }
2158
2159                 obd = class_name2obd(lsi->lsi_svname);
2160                 if (obd) {
2161                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
2162                         if (lsiflags & LSI_UMOUNT_FAILOVER)
2163                                 obd->obd_fail = 1;
2164                         /* We can't seem to give an error return code
2165                          * to .put_super, so we better make sure we clean up! */
2166                         obd->obd_force = 1;
2167                         class_manual_cleanup(obd);
2168                 } else {
2169                         CERROR("no obd %s\n", lsi->lsi_svname);
2170                         server_deregister_mount(lsi->lsi_svname);
2171                 }
2172         }
2173
2174         /* If they wanted the mgs to stop separately from the mdt, they
2175            should have put it on a different device. */
2176         if (IS_MGS(lsi)) {
2177                 /* if MDS start with --nomgs, don't stop MGS then */
2178                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
2179                         server_stop_mgs(sb);
2180         }
2181
2182         if (IS_OST(lsi) || IS_MDT(lsi)) {
2183                 if (lustre_stop_osp(sb) < 0)
2184                         CERROR("%s: Fail to stop osp-on-ost!\n", tmpname);
2185         }
2186
2187         /* Clean the mgc and sb */
2188         lustre_common_put_super(sb);
2189
2190         /* wait till all in-progress cleanups are done
2191          * specifically we're interested in ofd cleanup
2192          * as it pins OSS */
2193         obd_zombie_barrier();
2194
2195         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
2196            until the target is really gone so that our type refcount check
2197            is right. */
2198         server_stop_servers(lsiflags);
2199
2200         /* In case of startup or cleanup err, stop related obds */
2201         if (extraname) {
2202                 obd = class_name2obd(extraname);
2203                 if (obd) {
2204                         CWARN("Cleaning orphaned obd %s\n", extraname);
2205                         obd->obd_force = 1;
2206                         class_manual_cleanup(obd);
2207                 }
2208                 OBD_FREE(extraname, strlen(extraname) + 1);
2209         }
2210
2211         LCONSOLE_WARN("server umount %s complete\n", tmpname);
2212         OBD_FREE(tmpname, tmpname_sz);
2213         EXIT;
2214 }
2215
2216 /** Called only for 'umount -f'
2217  */
2218 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2219 static void server_umount_begin(struct vfsmount *vfsmnt, int flags)
2220 {
2221         struct super_block *sb = vfsmnt->mnt_sb;
2222 #else
2223 static void server_umount_begin(struct super_block *sb)
2224 {
2225 #endif
2226         struct lustre_sb_info *lsi = s2lsi(sb);
2227         ENTRY;
2228
2229 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2230         if (!(flags & MNT_FORCE)) {
2231                 EXIT;
2232                 return;
2233         }
2234 #endif
2235
2236         CDEBUG(D_MOUNT, "umount -f\n");
2237         /* umount = failover
2238            umount -f = force
2239            no third way to do non-force, non-failover */
2240         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
2241         EXIT;
2242 }
2243
2244 static int server_statfs (struct dentry *dentry, cfs_kstatfs_t *buf)
2245 {
2246         struct super_block *sb = dentry->d_sb;
2247         struct lustre_sb_info *lsi = s2lsi(sb);
2248         struct obd_statfs statfs;
2249         int rc;
2250         ENTRY;
2251
2252         if (lsi->lsi_dt_dev) {
2253                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
2254                 if (rc == 0) {
2255                         statfs_unpack(buf, &statfs);
2256                         buf->f_type = sb->s_magic;
2257                         RETURN(0);
2258                 }
2259         }
2260
2261         /* just return 0 */
2262         buf->f_type = sb->s_magic;
2263         buf->f_bsize = sb->s_blocksize;
2264         buf->f_blocks = 1;
2265         buf->f_bfree = 0;
2266         buf->f_bavail = 0;
2267         buf->f_files = 1;
2268         buf->f_ffree = 0;
2269         buf->f_namelen = NAME_MAX;
2270         RETURN(0);
2271 }
2272
2273 /** The operations we support directly on the superblock:
2274  * mount, umount, and df.
2275  */
2276 static struct super_operations server_ops =
2277 {
2278         .put_super      = server_put_super,
2279         .umount_begin   = server_umount_begin, /* umount -f */
2280         .statfs         = server_statfs,
2281 };
2282
2283 #define log2(n) ffz(~(n))
2284 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
2285
2286 static int server_fill_super_common(struct super_block *sb)
2287 {
2288         struct inode *root = 0;
2289         ENTRY;
2290
2291         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
2292
2293         sb->s_blocksize = 4096;
2294         sb->s_blocksize_bits = log2(sb->s_blocksize);
2295         sb->s_magic = LUSTRE_SUPER_MAGIC;
2296         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
2297         sb->s_flags |= MS_RDONLY;
2298         sb->s_op = &server_ops;
2299
2300         root = new_inode(sb);
2301         if (!root) {
2302                 CERROR("Can't make root inode\n");
2303                 RETURN(-EIO);
2304         }
2305
2306         /* returns -EIO for every operation */
2307         /* make_bad_inode(root); -- badness - can't umount */
2308         /* apparently we need to be a directory for the mount to finish */
2309         root->i_mode = S_IFDIR;
2310
2311         sb->s_root = d_make_root(root);
2312         if (!sb->s_root) {
2313                 CERROR("%s: can't make root dentry\n", sb->s_id);
2314                 RETURN(-EIO);
2315         }
2316
2317         RETURN(0);
2318 }
2319
2320 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
2321 {
2322         struct lustre_mount_data *lmd = lsi->lsi_lmd;
2323         struct obd_device        *obd;
2324         struct dt_device_param    p;
2325         char                      flagstr[16];
2326         int                       rc;
2327         ENTRY;
2328
2329         CDEBUG(D_MOUNT,
2330                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
2331                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
2332
2333         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
2334         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
2335         strcat(lsi->lsi_osd_uuid, "_UUID");
2336         sprintf(flagstr, "%lu:%lu", mflags, (unsigned long) lmd->lmd_flags);
2337
2338         obd = class_name2obd(lsi->lsi_osd_obdname);
2339         if (obd == NULL) {
2340                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
2341                                 lsi->lsi_osd_type,
2342                                 lsi->lsi_osd_uuid, lmd->lmd_dev,
2343                                 flagstr, lsi->lsi_lmd->lmd_opts,
2344                                 lsi->lsi_svname);
2345                 if (rc)
2346                         GOTO(out, rc);
2347                 obd = class_name2obd(lsi->lsi_osd_obdname);
2348                 LASSERT(obd);
2349         }
2350
2351         rc = obd_connect(NULL, &lsi->lsi_osd_exp, obd, &obd->obd_uuid, NULL, NULL);
2352         if (rc) {
2353                 obd->obd_force = 1;
2354                 class_manual_cleanup(obd);
2355                 lsi->lsi_dt_dev = NULL;
2356         }
2357
2358         /* XXX: to keep support old components relying on lsi_srv_mnt
2359          *      we get this info from OSD just started */
2360         LASSERT(obd->obd_lu_dev);
2361         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
2362         LASSERT(lsi->lsi_dt_dev);
2363
2364         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
2365
2366         lsi->lsi_srv_mnt = p.ddp_mnt;
2367
2368 out:
2369         RETURN(rc);
2370 }
2371
2372 /** Fill in the superblock info for a Lustre server.
2373  * Mount the device with the correct options.
2374  * Read the on-disk config file.
2375  * Start the services.
2376  */
2377 static int server_fill_super(struct super_block *sb)
2378 {
2379         struct lustre_sb_info *lsi = s2lsi(sb);
2380         int rc;
2381         ENTRY;
2382
2383         rc = lsi_prepare(lsi);
2384         if (rc)
2385                 RETURN(rc);
2386
2387         /* Start low level OSD */
2388         rc = osd_start(lsi, sb->s_flags);
2389         if (rc) {
2390                 CERROR("Unable to start osd on %s: %d\n",
2391                        lsi->lsi_lmd->lmd_dev, rc);
2392                 lustre_put_lsi(sb);
2393                 RETURN(rc);
2394         }
2395
2396         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2397                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2398
2399         if (class_name2obd(lsi->lsi_svname)) {
2400                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
2401                                    "running. Double-mount may have compromised"
2402                                    " the disk journal.\n",
2403                                    lsi->lsi_svname);
2404                 lustre_put_lsi(sb);
2405                 RETURN(-EALREADY);
2406         }
2407
2408         /* Start MGS before MGC */
2409         if (IS_MGS(lsi) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)){
2410                 rc = server_start_mgs(sb);
2411                 if (rc)
2412                         GOTO(out_mnt, rc);
2413         }
2414
2415         /* Start MGC before servers */
2416         rc = lustre_start_mgc(sb);
2417         if (rc)
2418                 GOTO(out_mnt, rc);
2419
2420         /* Set up all obd devices for service */
2421         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
2422                         (IS_OST(lsi) || IS_MDT(lsi))) {
2423                 rc = server_start_targets(sb, lsi->lsi_srv_mnt);
2424                 if (rc < 0) {
2425                         CERROR("Unable to start targets: %d\n", rc);
2426                         GOTO(out_mnt, rc);
2427                 }
2428         /* FIXME overmount client here,
2429            or can we just start a client log and client_fill_super on this sb?
2430            We need to make sure server_put_super gets called too - ll_put_super
2431            calls lustre_common_put_super; check there for LSI_SERVER flag,
2432            call s_p_s if so.
2433            Probably should start client from new thread so we can return.
2434            Client will not finish until all servers are connected.
2435            Note - MGS-only server does NOT get a client, since there is no
2436            lustre fs associated - the MGS is for all lustre fs's */
2437         }
2438
2439         rc = server_fill_super_common(sb);
2440         if (rc)
2441                 GOTO(out_mnt, rc);
2442
2443         RETURN(0);
2444 out_mnt:
2445         /* We jump here in case of failure while starting targets or MGS.
2446          * In this case we can't just put @mnt and have to do real cleanup
2447          * with stoping targets, etc. */
2448         server_put_super(sb);
2449         return rc;
2450 }
2451
2452 /*
2453  * Calculate timeout value for a target.
2454  */
2455 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2456 {
2457         struct lustre_mount_data *lmd;
2458         int soft = 0;
2459         int hard = 0;
2460         int factor = 0;
2461         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2462         int min = OBD_RECOVERY_TIME_MIN;
2463
2464         LASSERT(IS_SERVER(lsi));
2465
2466         lmd = lsi->lsi_lmd;
2467         if (lmd) {
2468                 soft   = lmd->lmd_recovery_time_soft;
2469                 hard   = lmd->lmd_recovery_time_hard;
2470                 has_ir = has_ir && !(lmd->lmd_flags & LMD_FLG_NOIR);
2471                 obd->obd_no_ir = !has_ir;
2472         }
2473
2474         if (soft == 0)
2475                 soft = OBD_RECOVERY_TIME_SOFT;
2476         if (hard == 0)
2477                 hard = OBD_RECOVERY_TIME_HARD;
2478
2479         /* target may have ir_factor configured. */
2480         factor = OBD_IR_FACTOR_DEFAULT;
2481         if (obd->obd_recovery_ir_factor)
2482                 factor = obd->obd_recovery_ir_factor;
2483
2484         if (has_ir) {
2485                 int new_soft = soft;
2486                 int new_hard = hard;
2487
2488                 /* adjust timeout value by imperative recovery */
2489
2490                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2491                 new_hard = (hard * factor) / OBD_IR_FACTOR_MAX;
2492
2493                 /* make sure the timeout is not too short */
2494                 new_soft = max(min, new_soft);
2495                 new_hard = max(new_soft, new_hard);
2496
2497                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery "
2498                               "window shrunk from %d-%d down to %d-%d\n",
2499                               obd->obd_name, soft, hard, new_soft, new_hard);
2500
2501                 soft = new_soft;
2502                 hard = new_hard;
2503         }
2504
2505         /* we're done */
2506         obd->obd_recovery_timeout   = max(obd->obd_recovery_timeout, soft);
2507         obd->obd_recovery_time_hard = hard;
2508         obd->obd_recovery_ir_factor = factor;
2509 }
2510 EXPORT_SYMBOL(server_calc_timeout);
2511
2512 /*************** mount common betweeen server and client ***************/
2513
2514 /* Common umount */
2515 int lustre_common_put_super(struct super_block *sb)
2516 {
2517         int rc;
2518         ENTRY;
2519
2520         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
2521
2522         /* Drop a ref to the MGC */
2523         rc = lustre_stop_mgc(sb);
2524         if (rc && (rc != -ENOENT)) {
2525                 if (rc != -EBUSY) {
2526                         CERROR("Can't stop MGC: %d\n", rc);
2527                         RETURN(rc);
2528                 }
2529                 /* BUSY just means that there's some other obd that
2530                    needs the mgc.  Let him clean it up. */
2531                 CDEBUG(D_MOUNT, "MGC still in use\n");
2532         }
2533         /* Drop a ref to the mounted disk */
2534         lustre_put_lsi(sb);
2535         lu_types_stop();
2536         RETURN(rc);
2537 }
2538 EXPORT_SYMBOL(lustre_common_put_super);
2539
2540 static void lmd_print(struct lustre_mount_data *lmd)
2541 {
2542         int i;
2543
2544         PRINT_CMD(PRINT_MASK, "  mount data:\n");
2545         if (lmd_is_client(lmd))
2546                 PRINT_CMD(PRINT_MASK, "profile: %s\n", lmd->lmd_profile);
2547         PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
2548         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
2549
2550         if (lmd->lmd_opts)
2551                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
2552
2553         if (lmd->lmd_recovery_time_soft)
2554                 PRINT_CMD(PRINT_MASK, "recovery time soft: %d\n",
2555                           lmd->lmd_recovery_time_soft);
2556
2557         if (lmd->lmd_recovery_time_hard)
2558                 PRINT_CMD(PRINT_MASK, "recovery time hard: %d\n",
2559                           lmd->lmd_recovery_time_hard);
2560
2561         for (i = 0; i < lmd->lmd_exclude_count; i++) {
2562                 PRINT_CMD(PRINT_MASK, "exclude %d:  OST%04x\n", i,
2563                           lmd->lmd_exclude[i]);
2564         }
2565 }
2566
2567 /* Is this server on the exclusion list */
2568 int lustre_check_exclusion(struct super_block *sb, char *svname)
2569 {
2570         struct lustre_sb_info *lsi = s2lsi(sb);
2571         struct lustre_mount_data *lmd = lsi->lsi_lmd;
2572         __u32 index;
2573         int i, rc;
2574         ENTRY;
2575
2576         rc = server_name2index(svname, &index, NULL);
2577         if (rc != LDD_F_SV_TYPE_OST)
2578                 /* Only exclude OSTs */
2579                 RETURN(0);
2580
2581         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
2582                index, lmd->lmd_exclude_count, lmd->lmd_dev);
2583
2584         for(i = 0; i < lmd->lmd_exclude_count; i++) {
2585                 if (index == lmd->lmd_exclude[i]) {
2586                         CWARN("Excluding %s (on exclusion list)\n", svname);
2587                         RETURN(1);
2588                 }
2589         }
2590         RETURN(0);
2591 }
2592
2593 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
2594 static int lmd_make_exclusion(struct lustre_mount_data *lmd, char *ptr)
2595 {
2596         char *s1 = ptr, *s2;
2597         __u32 index, *exclude_list;
2598         int rc = 0, devmax;
2599         ENTRY;
2600
2601         /* The shortest an ost name can be is 8 chars: -OST0000.
2602            We don't actually know the fsname at this time, so in fact
2603            a user could specify any fsname. */
2604         devmax = strlen(ptr) / 8 + 1;
2605
2606         /* temp storage until we figure out how many we have */
2607         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
2608         if (!exclude_list)
2609                 RETURN(-ENOMEM);
2610
2611         /* we enter this fn pointing at the '=' */
2612         while (*s1 && *s1 != ' ' && *s1 != ',') {
2613                 s1++;
2614                 rc = server_name2index(s1, &index, &s2);
2615                 if (rc < 0) {
2616                         CERROR("Can't parse server name '%s'\n", s1);
2617                         break;
2618                 }
2619                 if (rc == LDD_F_SV_TYPE_OST)
2620                         exclude_list[lmd->lmd_exclude_count++] = index;
2621                 else
2622                         CDEBUG(D_MOUNT, "ignoring exclude %.7s\n", s1);
2623                 s1 = s2;
2624                 /* now we are pointing at ':' (next exclude)
2625                    or ',' (end of excludes) */
2626                 if (lmd->lmd_exclude_count >= devmax)
2627                         break;
2628         }
2629         if (rc >= 0) /* non-err */
2630                 rc = 0;
2631
2632         if (lmd->lmd_exclude_count) {
2633                 /* permanent, freed in lustre_free_lsi */
2634                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
2635                           lmd->lmd_exclude_count);
2636                 if (lmd->lmd_exclude) {
2637                         memcpy(lmd->lmd_exclude, exclude_list,
2638                                sizeof(index) * lmd->lmd_exclude_count);
2639                 } else {
2640                         rc = -ENOMEM;
2641                         lmd->lmd_exclude_count = 0;
2642                 }
2643         }
2644         OBD_FREE(exclude_list, sizeof(index) * devmax);
2645         RETURN(rc);
2646 }
2647
2648 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
2649 {
2650         char   *tail;
2651         int     length;
2652
2653         if (lmd->lmd_mgssec != NULL) {
2654                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
2655                 lmd->lmd_mgssec = NULL;
2656         }
2657
2658         tail = strchr(ptr, ',');
2659         if (tail == NULL)
2660                 length = strlen(ptr);
2661         else
2662                 length = tail - ptr;
2663
2664         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
2665         if (lmd->lmd_mgssec == NULL)
2666                 return -ENOMEM;
2667
2668         memcpy(lmd->lmd_mgssec, ptr, length);
2669         lmd->lmd_mgssec[length] = '\0';
2670         return 0;
2671 }
2672
2673 static int lmd_parse_string(char **handle, char *ptr)
2674 {
2675         char   *tail;
2676         int     length;
2677
2678         if ((handle == NULL) || (ptr == NULL))
2679                 return -EINVAL;
2680
2681         if (*handle != NULL) {
2682                 OBD_FREE(*handle, strlen(*handle) + 1);
2683                 *handle = NULL;
2684         }
2685
2686         tail = strchr(ptr, ',');
2687         if (tail == NULL)
2688                 length = strlen(ptr);
2689         else
2690                 length = tail - ptr;
2691
2692         OBD_ALLOC(*handle, length + 1);
2693         if (*handle == NULL)
2694                 return -ENOMEM;
2695
2696         memcpy(*handle, ptr, length);
2697         (*handle)[length] = '\0';
2698
2699         return 0;
2700 }
2701
2702 /* Collect multiple values for mgsnid specifiers */
2703 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
2704 {
2705         lnet_nid_t nid;
2706         char *tail = *ptr;
2707         char *mgsnid;
2708         int   length;
2709         int   oldlen = 0;
2710
2711         /* Find end of nidlist */
2712         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
2713         length = tail - *ptr;
2714         if (length == 0) {
2715                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
2716                 return -EINVAL;
2717         }
2718
2719         if (lmd->lmd_mgs != NULL)
2720                 oldlen = strlen(lmd->lmd_mgs) + 1;
2721
2722         OBD_ALLOC(mgsnid, oldlen + length + 1);
2723         if (mgsnid == NULL)
2724                 return -ENOMEM;
2725
2726         if (lmd->lmd_mgs != NULL) {
2727                 /* Multiple mgsnid= are taken to mean failover locations */
2728                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
2729                 mgsnid[oldlen - 1] = ':';
2730                 OBD_FREE(lmd->lmd_mgs, oldlen);
2731         }
2732         memcpy(mgsnid + oldlen, *ptr, length);
2733         mgsnid[oldlen + length] = '\0';
2734         lmd->lmd_mgs = mgsnid;
2735         *ptr = tail;
2736
2737         return 0;
2738 }
2739
2740 /** Parse mount line options
2741  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
2742  * dev is passed as device=uml1:/lustre by mount.lustre
2743  */
2744 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
2745 {
2746         char *s1, *s2, *devname = NULL;
2747         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
2748         int rc = 0;
2749         ENTRY;
2750
2751         LASSERT(lmd);
2752         if (!options) {
2753                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
2754                                    "/sbin/mount.lustre is installed.\n");
2755                 RETURN(-EINVAL);
2756         }
2757
2758         /* Options should be a string - try to detect old lmd data */
2759         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
2760                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
2761                                    "/sbin/mount.lustre.  Please install "
2762                                    "version %s\n", LUSTRE_VERSION_STRING);
2763                 RETURN(-EINVAL);
2764         }
2765         lmd->lmd_magic = LMD_MAGIC;
2766
2767         OBD_ALLOC(lmd->lmd_params, 4096);
2768         if (lmd->lmd_params == NULL)
2769                 RETURN(-ENOMEM);
2770         lmd->lmd_params[0] = '\0';
2771
2772         /* Set default flags here */
2773
2774         s1 = options;
2775         while (*s1) {
2776                 int clear = 0;
2777                 int time_min = OBD_RECOVERY_TIME_MIN;
2778
2779                 /* Skip whitespace and extra commas */
2780                 while (*s1 == ' ' || *s1 == ',')
2781                         s1++;
2782
2783                 /* Client options are parsed in ll_options: eg. flock,
2784                    user_xattr, acl */
2785
2786                 /* Parse non-ldiskfs options here. Rather than modifying
2787                    ldiskfs, we just zero these out here */
2788                 if (strncmp(s1, "abort_recov", 11) == 0) {
2789                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
2790                         clear++;
2791                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
2792                         lmd->lmd_recovery_time_soft = max_t(int,
2793                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2794                         clear++;
2795                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
2796                         lmd->lmd_recovery_time_hard = max_t(int,
2797                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2798                         clear++;
2799                 } else if (strncmp(s1, "noir", 4) == 0) {
2800                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
2801                         clear++;
2802                 } else if (strncmp(s1, "nosvc", 5) == 0) {
2803                         lmd->lmd_flags |= LMD_FLG_NOSVC;
2804                         clear++;
2805                 } else if (strncmp(s1, "nomgs", 5) == 0) {
2806                         lmd->lmd_flags |= LMD_FLG_NOMGS;
2807                         clear++;
2808                 } else if (strncmp(s1, "noscrub", 7) == 0) {
2809                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
2810                         clear++;
2811                 } else if (strncmp(s1, PARAM_MGSNODE,
2812                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
2813                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
2814                         /* Assume the next mount opt is the first
2815                            invalid nid we get to. */
2816                         rc = lmd_parse_mgs(lmd, &s2);
2817                         if (rc)
2818                                 goto invalid;
2819                         clear++;
2820                 } else if (strncmp(s1, "writeconf", 9) == 0) {
2821                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
2822                         clear++;
2823                 } else if (strncmp(s1, "virgin", 6) == 0) {
2824                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
2825                         clear++;
2826                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
2827                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
2828                         clear++;
2829                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
2830                         rc = lmd_parse_mgssec(lmd, s1 + 7);
2831                         if (rc)
2832                                 goto invalid;
2833                         clear++;
2834                 /* ost exclusion list */
2835                 } else if (strncmp(s1, "exclude=", 8) == 0) {
2836                         rc = lmd_make_exclusion(lmd, s1 + 7);
2837                         if (rc)
2838                                 goto invalid;
2839                         clear++;
2840                 } else if (strncmp(s1, "mgs", 3) == 0) {
2841                         /* We are an MGS */
2842                         lmd->lmd_flags |= LMD_FLG_MGS;
2843                         clear++;
2844                 } else if (strncmp(s1, "svname=", 7) == 0) {
2845                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
2846                         if (rc)
2847                                 goto invalid;
2848                         clear++;
2849                 } else if (strncmp(s1, "param=", 6) == 0) {
2850                         int length;
2851                         char *tail = strchr(s1 + 6, ',');
2852                         if (tail == NULL)
2853                                 length = strlen(s1);
2854                         else
2855                                 length = tail - s1;
2856                         length -= 6;
2857                         strncat(lmd->lmd_params, s1 + 6, length);
2858                         strcat(lmd->lmd_params, " ");
2859                         clear++;
2860                 } else if (strncmp(s1, "osd=", 4) == 0) {
2861                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
2862                         if (rc)
2863                                 goto invalid;
2864                         clear++;
2865                 }
2866                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
2867                    end of the options. */
2868                 else if (strncmp(s1, "device=", 7) == 0) {
2869                         devname = s1 + 7;
2870                         /* terminate options right before device.  device
2871                            must be the last one. */
2872                         *s1 = '\0';
2873                         break;
2874                 }
2875
2876                 /* Find next opt */
2877                 s2 = strchr(s1, ',');
2878                 if (s2 == NULL) {
2879                         if (clear)
2880                                 *s1 = '\0';
2881                         break;
2882                 }
2883                 s2++;
2884                 if (clear)
2885                         memmove(s1, s2, strlen(s2) + 1);
2886                 else
2887                         s1 = s2;
2888         }
2889
2890         if (!devname) {
2891                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
2892                                    "(need mount option 'device=...')\n");
2893                 goto invalid;
2894         }
2895
2896         s1 = strstr(devname, ":/");
2897         if (s1) {
2898                 ++s1;
2899                 lmd->lmd_flags |= LMD_FLG_CLIENT;
2900                 /* Remove leading /s from fsname */
2901                 while (*++s1 == '/') ;
2902                 /* Freed in lustre_free_lsi */
2903                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
2904                 if (!lmd->lmd_profile)
2905                         RETURN(-ENOMEM);
2906                 sprintf(lmd->lmd_profile, "%s-client", s1);
2907         }
2908
2909         /* Freed in lustre_free_lsi */
2910         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
2911         if (!lmd->lmd_dev)
2912                 RETURN(-ENOMEM);
2913         strcpy(lmd->lmd_dev, devname);
2914
2915         /* Save mount options */
2916         s1 = options + strlen(options) - 1;
2917         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
2918                 *s1-- = 0;
2919         if (*options != 0) {
2920                 /* Freed in lustre_free_lsi */
2921                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
2922                 if (!lmd->lmd_opts)
2923                         RETURN(-ENOMEM);
2924                 strcpy(lmd->lmd_opts, options);
2925         }
2926
2927         lmd_print(lmd);
2928         lmd->lmd_magic = LMD_MAGIC;
2929
2930         RETURN(rc);
2931
2932 invalid:
2933         CERROR("Bad mount options %s\n", options);
2934         RETURN(-EINVAL);
2935 }
2936
2937 struct lustre_mount_data2 {
2938         void *lmd2_data;
2939         struct vfsmount *lmd2_mnt;
2940 };
2941
2942 /** This is the entry point for the mount call into Lustre.
2943  * This is called when a server or client is mounted,
2944  * and this is where we start setting things up.
2945  * @param data Mount options (e.g. -o flock,abort_recov)
2946  */
2947 int lustre_fill_super(struct super_block *sb, void *data, int silent)
2948 {
2949         struct lustre_mount_data *lmd;
2950         struct lustre_mount_data2 *lmd2 = data;
2951         struct lustre_sb_info *lsi;
2952         int rc;
2953         ENTRY;
2954
2955         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2956
2957         lsi = lustre_init_lsi(sb);
2958         if (!lsi)
2959                 RETURN(-ENOMEM);
2960         lmd = lsi->lsi_lmd;
2961
2962         /*
2963          * Disable lockdep during mount, because mount locking patterns are
2964          * `special'.
2965          */
2966         lockdep_off();
2967
2968         /*
2969          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
2970          */
2971         obd_zombie_barrier();
2972
2973         /* Figure out the lmd from the mount options */
2974         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
2975                 lustre_put_lsi(sb);
2976                 GOTO(out, rc = -EINVAL);
2977         }
2978
2979         if (lmd_is_client(lmd)) {
2980                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
2981                 if (!client_fill_super) {
2982                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
2983                                            "client mount! Is the 'lustre' "
2984                                            "module loaded?\n");
2985                         lustre_put_lsi(sb);
2986                         rc = -ENODEV;
2987                 } else {
2988                         rc = lustre_start_mgc(sb);
2989                         if (rc) {
2990                                 lustre_put_lsi(sb);
2991                                 GOTO(out, rc);
2992                         }
2993                         /* Connect and start */
2994                         /* (should always be ll_fill_super) */
2995                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
2996                         /* c_f_s will call lustre_common_put_super on failure */
2997                 }
2998         } else {
2999                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
3000                 rc = server_fill_super(sb);
3001                 /* s_f_s calls lustre_start_mgc after the mount because we need
3002                    the MGS nids which are stored on disk.  Plus, we may
3003                    need to start the MGS first. */
3004                 /* s_f_s will call server_put_super on failure */
3005         }
3006
3007         /* If error happens in fill_super() call, @lsi will be killed there.
3008          * This is why we do not put it here. */
3009         GOTO(out, rc);
3010 out:
3011         if (rc) {
3012                 CERROR("Unable to mount %s (%d)\n",
3013                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
3014         } else {
3015                 CDEBUG(D_SUPER, "Mount %s complete\n",
3016                        lmd->lmd_dev);
3017         }
3018         lockdep_on();
3019         return rc;
3020 }
3021
3022
3023 /* We can't call ll_fill_super by name because it lives in a module that
3024    must be loaded after this one. */
3025 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
3026                                                   struct vfsmount *mnt))
3027 {
3028         client_fill_super = cfs;
3029 }
3030 EXPORT_SYMBOL(lustre_register_client_fill_super);
3031
3032 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
3033 {
3034         kill_super_cb = cfs;
3035 }
3036 EXPORT_SYMBOL(lustre_register_kill_super_cb);
3037
3038 /***************** FS registration ******************/
3039 #ifdef HAVE_FSTYPE_MOUNT
3040 struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
3041                                 const char *devname, void *data)
3042 {
3043         struct lustre_mount_data2 lmd2 = { data, NULL };
3044
3045         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
3046 }
3047 #else
3048 int lustre_get_sb(struct file_system_type *fs_type, int flags,
3049                   const char *devname, void * data, struct vfsmount *mnt)
3050 {
3051         struct lustre_mount_data2 lmd2 = { data, mnt };
3052
3053         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
3054 }
3055 #endif
3056
3057 void lustre_kill_super(struct super_block *sb)
3058 {
3059         struct lustre_sb_info *lsi = s2lsi(sb);
3060
3061         if (kill_super_cb && lsi && !IS_SERVER(lsi))
3062                 (*kill_super_cb)(sb);
3063
3064         kill_anon_super(sb);
3065 }
3066
3067 /** Register the "lustre" fs type
3068  */
3069 struct file_system_type lustre_fs_type = {
3070         .owner        = THIS_MODULE,
3071         .name         = "lustre",
3072 #ifdef HAVE_FSTYPE_MOUNT
3073         .mount        = lustre_mount,
3074 #else
3075         .get_sb       = lustre_get_sb,
3076 #endif
3077         .kill_sb      = lustre_kill_super,
3078         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
3079                         FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
3080 };
3081
3082 int lustre_register_fs(void)
3083 {
3084         return register_filesystem(&lustre_fs_type);
3085 }
3086
3087 int lustre_unregister_fs(void)
3088 {
3089         return unregister_filesystem(&lustre_fs_type);
3090 }
3091
3092 EXPORT_SYMBOL(server_mti_print);