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