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