Whamcloud - gitweb
LU-1303 mds: integration lod/osp into the stack
[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, Whamcloud, Inc.
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 CFS_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         cfs_mutex_lock(&lustre_mount_info_lock);
109
110         if (server_find_mount(name)) {
111                 cfs_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         cfs_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         cfs_mutex_lock(&lustre_mount_info_lock);
136         lmi = server_find_mount(name);
137         if (!lmi) {
138                 cfs_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         cfs_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         cfs_mutex_lock(&lustre_mount_info_lock);
163         lmi = server_find_mount(name);
164         cfs_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         cfs_mutex_lock(&lustre_mount_info_lock);
191         lmi = server_find_mount(name);
192         cfs_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         cfs_mutex_lock(&lustre_mount_info_lock);
210         lmi = server_find_mount(name);
211         cfs_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         LASSERT(mnt);
393
394         /* It is impossible to have more than 1 MGS per node, since
395            MGC wouldn't know which to connect to */
396         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
397         if (lmi) {
398                 lsi = s2lsi(lmi->lmi_sb);
399                 LCONSOLE_ERROR_MSG(0x15d, "The MGS service was already started"
400                                    " from server\n");
401                 RETURN(-EALREADY);
402         }
403
404         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
405
406         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb, mnt);
407
408         if (!rc) {
409                 rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
410                                          LUSTRE_MGS_OBDNAME, 0, 0,
411                                          lsi->lsi_osd_obdname, 0);
412                 /* Do NOT call server_deregister_mount() here. This leads to
413                  * inability cleanup cleanly and free lsi and other stuff when
414                  * mgs calls server_put_mount() in error handling case. -umka */
415         }
416
417         if (rc)
418                 LCONSOLE_ERROR_MSG(0x15e, "Failed to start MGS '%s' (%d). "
419                                    "Is the 'mgs' module loaded?\n",
420                                    LUSTRE_MGS_OBDNAME, rc);
421         RETURN(rc);
422 }
423
424 static int server_stop_mgs(struct super_block *sb)
425 {
426         struct obd_device *obd;
427         int rc;
428         ENTRY;
429
430         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
431
432         /* There better be only one MGS */
433         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
434         if (!obd) {
435                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
436                 RETURN(-EALREADY);
437         }
438
439         /* The MGS should always stop when we say so */
440         obd->obd_force = 1;
441         rc = class_manual_cleanup(obd);
442         RETURN(rc);
443 }
444
445 CFS_DEFINE_MUTEX(mgc_start_lock);
446
447 /** Set up a mgc obd to process startup logs
448  *
449  * \param sb [in] super block of the mgc obd
450  *
451  * \retval 0 success, otherwise error code
452  */
453 static int lustre_start_mgc(struct super_block *sb)
454 {
455         struct obd_connect_data *data = NULL;
456         struct lustre_sb_info *lsi = s2lsi(sb);
457         struct obd_device *obd;
458         struct obd_export *exp;
459         struct obd_uuid *uuid;
460         class_uuid_t uuidc;
461         lnet_nid_t nid;
462         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
463         char *ptr;
464         int recov_bk;
465         int rc = 0, i = 0, j, len;
466         ENTRY;
467
468         LASSERT(lsi->lsi_lmd);
469
470         /* Find the first non-lo MGS nid for our MGC name */
471         if (IS_SERVER(lsi)) {
472                 /* mount -o mgsnode=nid */
473                 ptr = lsi->lsi_lmd->lmd_mgs;
474                 if (lsi->lsi_lmd->lmd_mgs &&
475                     (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
476                         i++;
477                 } else if (IS_MGS(lsi)) {
478                         lnet_process_id_t id;
479                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
480                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
481                                         continue;
482                                 nid = id.nid;
483                                 i++;
484                                 break;
485                         }
486                 }
487         } else { /* client */
488                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
489                 ptr = lsi->lsi_lmd->lmd_dev;
490                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
491                         i++;
492         }
493         if (i == 0) {
494                 CERROR("No valid MGS nids found.\n");
495                 RETURN(-EINVAL);
496         }
497
498         cfs_mutex_lock(&mgc_start_lock);
499
500         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(libcfs_nid2str(nid)) + 1;
501         OBD_ALLOC(mgcname, len);
502         OBD_ALLOC(niduuid, len + 2);
503         if (!mgcname || !niduuid)
504                 GOTO(out_free, rc = -ENOMEM);
505         sprintf(mgcname, "%s%s", LUSTRE_MGC_OBDNAME, libcfs_nid2str(nid));
506
507         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
508
509         OBD_ALLOC_PTR(data);
510         if (data == NULL)
511                 GOTO(out_free, rc = -ENOMEM);
512
513         obd = class_name2obd(mgcname);
514         if (obd && !obd->obd_stopping) {
515                 rc = obd_set_info_async(NULL, obd->obd_self_export,
516                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
517                                         strlen(mgssec), mgssec, NULL);
518                 if (rc)
519                         GOTO(out_free, rc);
520
521                 /* Re-using an existing MGC */
522                 cfs_atomic_inc(&obd->u.cli.cl_mgc_refcount);
523
524                 /* IR compatibility check, only for clients */
525                 if (lmd_is_client(lsi->lsi_lmd)) {
526                         int has_ir;
527                         int vallen = sizeof(*data);
528                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
529
530                         rc = obd_get_info(NULL, obd->obd_self_export,
531                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
532                                           &vallen, data, NULL);
533                         LASSERT(rc == 0);
534                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
535                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
536                                 /* LMD_FLG_NOIR is for test purpose only */
537                                 LCONSOLE_WARN(
538                                     "Trying to mount a client with IR setting "
539                                     "not compatible with current mgc. "
540                                     "Force to use current mgc setting that is "
541                                     "IR %s.\n",
542                                     has_ir ? "enabled" : "disabled");
543                                 if (has_ir)
544                                         *flags &= ~LMD_FLG_NOIR;
545                                 else
546                                         *flags |= LMD_FLG_NOIR;
547                         }
548                 }
549
550                 recov_bk = 0;
551                 /* If we are restarting the MGS, don't try to keep the MGC's
552                    old connection, or registration will fail. */
553                 if (IS_MGS(lsi)) {
554                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
555                         recov_bk = 1;
556                 }
557
558                 /* Try all connections, but only once (again).
559                    We don't want to block another target from starting
560                    (using its local copy of the log), but we do want to connect
561                    if at all possible. */
562                 recov_bk++;
563                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
564                 rc = obd_set_info_async(NULL, obd->obd_self_export,
565                                         sizeof(KEY_INIT_RECOV_BACKUP),
566                                         KEY_INIT_RECOV_BACKUP,
567                                         sizeof(recov_bk), &recov_bk, NULL);
568                 GOTO(out, rc = 0);
569         }
570
571         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
572
573         /* Add the primary nids for the MGS */
574         i = 0;
575         sprintf(niduuid, "%s_%x", mgcname, i);
576         if (IS_SERVER(lsi)) {
577                 ptr = lsi->lsi_lmd->lmd_mgs;
578                 if (IS_MGS(lsi)) {
579                         /* Use local nids (including LO) */
580                         lnet_process_id_t id;
581                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
582                                 rc = do_lcfg(mgcname, id.nid,
583                                              LCFG_ADD_UUID, niduuid, 0,0,0);
584                         }
585                 } else {
586                         /* Use mgsnode= nids */
587                         /* mount -o mgsnode=nid */
588                         if (lsi->lsi_lmd->lmd_mgs) {
589                                 ptr = lsi->lsi_lmd->lmd_mgs;
590                         } else if (class_find_param(ptr, PARAM_MGSNODE,
591                                                     &ptr) != 0) {
592                                 CERROR("No MGS nids given.\n");
593                                 GOTO(out_free, rc = -EINVAL);
594                         }
595                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
596                                 rc = do_lcfg(mgcname, nid,
597                                              LCFG_ADD_UUID, niduuid, 0,0,0);
598                                 i++;
599                         }
600                 }
601         } else { /* client */
602                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
603                 ptr = lsi->lsi_lmd->lmd_dev;
604                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
605                         rc = do_lcfg(mgcname, nid,
606                                      LCFG_ADD_UUID, niduuid, 0,0,0);
607                         i++;
608                         /* Stop at the first failover nid */
609                         if (*ptr == ':')
610                                 break;
611                 }
612         }
613         if (i == 0) {
614                 CERROR("No valid MGS nids found.\n");
615                 GOTO(out_free, rc = -EINVAL);
616         }
617         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
618
619         /* Random uuid for MGC allows easier reconnects */
620         OBD_ALLOC_PTR(uuid);
621         ll_generate_random_uuid(uuidc);
622         class_uuid_unparse(uuidc, uuid);
623
624         /* Start the MGC */
625         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
626                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
627                                  niduuid, 0, 0);
628         OBD_FREE_PTR(uuid);
629         if (rc)
630                 GOTO(out_free, rc);
631
632         /* Add any failover MGS nids */
633         i = 1;
634         while (ptr && ((*ptr == ':' ||
635                class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
636                 /* New failover node */
637                 sprintf(niduuid, "%s_%x", mgcname, i);
638                 j = 0;
639                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
640                         j++;
641                         rc = do_lcfg(mgcname, nid,
642                                      LCFG_ADD_UUID, niduuid, 0,0,0);
643                         if (*ptr == ':')
644                                 break;
645                 }
646                 if (j > 0) {
647                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
648                                      niduuid, 0, 0, 0);
649                         i++;
650                 } else {
651                         /* at ":/fsname" */
652                         break;
653                 }
654         }
655         lsi->lsi_lmd->lmd_mgs_failnodes = i;
656
657         obd = class_name2obd(mgcname);
658         if (!obd) {
659                 CERROR("Can't find mgcobd %s\n", mgcname);
660                 GOTO(out_free, rc = -ENOTCONN);
661         }
662
663         rc = obd_set_info_async(NULL, obd->obd_self_export,
664                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
665                                 strlen(mgssec), mgssec, NULL);
666         if (rc)
667                 GOTO(out_free, rc);
668
669         /* Keep a refcount of servers/clients who started with "mount",
670            so we know when we can get rid of the mgc. */
671         cfs_atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
672
673         /* Try all connections, but only once. */
674         recov_bk = 1;
675         rc = obd_set_info_async(NULL, obd->obd_self_export,
676                                 sizeof(KEY_INIT_RECOV_BACKUP),
677                                 KEY_INIT_RECOV_BACKUP,
678                                 sizeof(recov_bk), &recov_bk, NULL);
679         if (rc)
680                 /* nonfatal */
681                 CWARN("can't set %s %d\n", KEY_INIT_RECOV_BACKUP, rc);
682
683         /* We connect to the MGS at setup, and don't disconnect until cleanup */
684         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
685                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV;
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         cfs_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         cfs_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         cfs_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 CFS_DEFINE_MUTEX(server_start_lock);
827
828 /* Stop MDS/OSS if nobody is using them */
829 static int server_stop_servers(int lsiflags)
830 {
831         struct obd_device *obd = NULL;
832         struct obd_type *type = NULL;
833         int rc = 0;
834         ENTRY;
835
836         cfs_mutex_lock(&server_start_lock);
837
838         /* Either an MDT or an OST or neither  */
839         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
840         if ((lsiflags & LDD_F_SV_TYPE_MDT) &&
841             (obd = class_name2obd(LUSTRE_MDS_OBDNAME))) {
842                 /*FIXME pre-rename, should eventually be LUSTRE_MDT_NAME*/
843                 type = class_search_type(LUSTRE_MDS_NAME);
844         }
845         /* if this was an OST, and there are no more OST's, clean up the OSS */
846         if ((lsiflags & LDD_F_SV_TYPE_OST) &&
847             (obd = class_name2obd(LUSTRE_OSS_OBDNAME))) {
848                 type = class_search_type(LUSTRE_OST_NAME);
849         }
850
851         if (obd && (!type || !type->typ_refcnt)) {
852                 int err;
853                 obd->obd_force = 1;
854                 /* obd_fail doesn't mean much on a server obd */
855                 err = class_manual_cleanup(obd);
856                 if (!rc)
857                         rc = err;
858         }
859
860         cfs_mutex_unlock(&server_start_lock);
861
862         RETURN(rc);
863 }
864
865 int server_mti_print(char *title, struct mgs_target_info *mti)
866 {
867         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
868         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
869         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
870         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
871         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
872                   mti->mti_config_ver, mti->mti_flags);
873         return(0);
874 }
875
876 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
877  * @param [in] svname server name including type and index
878  * @param [out] fsname Buffer to copy filesystem name prefix into.
879  *  Must have at least 'strlen(fsname) + 1' chars.
880  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
881  * rc < 0  on error
882  */
883 static int server_name2fsname(char *svname, char *fsname, char **endptr)
884 {
885         char *p;
886
887         p = strstr(svname, "-OST");
888         if (p == NULL)
889                 p = strstr(svname, "-MDT");
890         if (p == NULL)
891                 return -1;
892
893         if (fsname) {
894                 strncpy(fsname, svname, p - svname);
895                 fsname[p - svname] = '\0';
896         }
897
898         if (endptr != NULL)
899                 *endptr = p;
900
901         return 0;
902 }
903
904 /**
905  * Get service name (svname) from string
906  * rc < 0 on error
907  * if endptr isn't NULL it is set to end of fsname *
908  */
909 int server_name2svname(char *label, char *svname, char **endptr)
910 {
911         int rc;
912         char *dash;
913
914         /* We use server_name2fsname() just for parsing */
915         rc = server_name2fsname(label, NULL, &dash);
916         if (rc != 0)
917                 return rc;
918
919         if (*dash != '-')
920                 return -1;
921
922         strncpy(svname, dash + 1, MTI_NAME_MAXLEN);
923
924         return 0;
925 }
926 EXPORT_SYMBOL(server_name2svname);
927
928
929 /* Get the index from the obd name.
930    rc = server type, or
931    rc < 0  on error
932    if endptr isn't NULL it is set to end of name */
933 int server_name2index(char *svname, __u32 *idx, char **endptr)
934 {
935         unsigned long index;
936         int rc;
937         char *dash;
938
939         /* We use server_name2fsname() just for parsing */
940         rc = server_name2fsname(svname, NULL, &dash);
941         if (rc != 0)
942                 return rc;
943
944         if (*dash != '-')
945                 return -EINVAL;
946
947         dash++;
948
949         if (strncmp(dash, "MDT", 3) == 0)
950                 rc = LDD_F_SV_TYPE_MDT;
951         else if (strncmp(dash, "OST", 3) == 0)
952                 rc = LDD_F_SV_TYPE_OST;
953         else
954                 return -EINVAL;
955
956         dash += 3;
957
958         if (strcmp(dash, "all") == 0)
959                 return rc | LDD_F_SV_ALL;
960
961         index = simple_strtoul(dash, endptr, 16);
962         *idx = index;
963
964         return rc;
965 }
966 EXPORT_SYMBOL(server_name2index);
967
968 /* Generate data for registration */
969 static int server_lsi2mti(struct lustre_sb_info *lsi,
970                           struct mgs_target_info *mti)
971 {
972         lnet_process_id_t id;
973         int rc, i = 0;
974         ENTRY;
975
976         if (!IS_SERVER(lsi))
977                 RETURN(-EINVAL);
978
979         strncpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname));
980
981         mti->mti_nid_count = 0;
982         while (LNetGetId(i++, &id) != -ENOENT) {
983                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
984                         continue;
985
986                 /* server use --servicenode param, only allow specified
987                  * nids be registered */
988                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) != 0 &&
989                     class_match_nid(lsi->lsi_lmd->lmd_params,
990                                     PARAM_FAILNODE, id.nid) < 1)
991                         continue;
992
993                 /* match specified network */
994                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
995                                      PARAM_NETWORK, LNET_NIDNET(id.nid)))
996                         continue;
997
998                 mti->mti_nids[mti->mti_nid_count] = id.nid;
999                 mti->mti_nid_count++;
1000                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1001                         CWARN("Only using first %d nids for %s\n",
1002                               mti->mti_nid_count, mti->mti_svname);
1003                         break;
1004                 }
1005         }
1006
1007         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1008         mti->mti_config_ver = 0;
1009
1010         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1011         if (rc != 0)
1012                 return rc;
1013
1014         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1015         if (rc < 0)
1016                 return rc;
1017         /* Orion requires index to be set */
1018         LASSERT(!(rc & LDD_F_NEED_INDEX));
1019         /* keep only LDD flags */
1020         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1021         mti->mti_flags |= LDD_F_UPDATE;
1022         strncpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1023                 sizeof(mti->mti_params));
1024         return 0;
1025 }
1026
1027 /* Register an old or new target with the MGS. If needed MGS will construct
1028    startup logs and assign index */
1029 static int server_register_target(struct lustre_sb_info *lsi)
1030 {
1031         struct obd_device *mgc = lsi->lsi_mgc;
1032         struct mgs_target_info *mti = NULL;
1033         bool writeconf;
1034         int rc;
1035         ENTRY;
1036
1037         LASSERT(mgc);
1038
1039         if (!IS_SERVER(lsi))
1040                 RETURN(-EINVAL);
1041
1042         OBD_ALLOC_PTR(mti);
1043         if (!mti)
1044                 RETURN(-ENOMEM);
1045
1046         rc = server_lsi2mti(lsi, mti);
1047         if (rc)
1048                 GOTO(out, rc);
1049
1050         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1051                mti->mti_svname, mti->mti_fsname,
1052                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1053                mti->mti_flags);
1054
1055         /* if write_conf is true, the registration must succeed */
1056         writeconf = !!(lsi->lsi_flags & (LDD_F_NEED_INDEX | LDD_F_UPDATE));
1057         mti->mti_flags |= LDD_F_OPC_REG;
1058
1059         /* Register the target */
1060         /* FIXME use mgc_process_config instead */
1061         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1062                                 sizeof(KEY_REGISTER_TARGET), KEY_REGISTER_TARGET,
1063                                 sizeof(*mti), mti, NULL);
1064         if (rc) {
1065                 if (mti->mti_flags & LDD_F_ERROR) {
1066                         LCONSOLE_ERROR_MSG(0x160,
1067                                 "The MGS is refusing to allow this "
1068                                 "server (%s) to start. Please see messages"
1069                                 " on the MGS node.\n", lsi->lsi_svname);
1070                 } else if (writeconf) {
1071                         LCONSOLE_ERROR_MSG(0x15f,
1072                                 "Communication to the MGS return error %d. "
1073                                 "Is the MGS running?\n", rc);
1074                 } else {
1075                         CERROR("Cannot talk to the MGS: %d, not fatal\n", rc);
1076                         /* reset the error code for non-fatal error. */
1077                         rc = 0;
1078                 }
1079                 GOTO(out, rc);
1080         }
1081
1082 out:
1083         if (mti)
1084                 OBD_FREE_PTR(mti);
1085         RETURN(rc);
1086 }
1087
1088 /**
1089  * Notify the MGS that this target is ready.
1090  * Used by IR - if the MGS receives this message, it will notify clients.
1091  */
1092 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1093 {
1094         struct lustre_sb_info *lsi = s2lsi(sb);
1095         struct obd_device *mgc = lsi->lsi_mgc;
1096         struct mgs_target_info *mti = NULL;
1097         int rc;
1098         ENTRY;
1099
1100         LASSERT(mgc);
1101
1102         if (!(IS_SERVER(lsi)))
1103                 RETURN(-EINVAL);
1104
1105         OBD_ALLOC_PTR(mti);
1106         if (!mti)
1107                 RETURN(-ENOMEM);
1108         rc = server_lsi2mti(lsi, mti);
1109         if (rc)
1110                 GOTO(out, rc);
1111
1112         mti->mti_instance = obd->u.obt.obt_instance;
1113         mti->mti_flags |= LDD_F_OPC_READY;
1114
1115         /* FIXME use mgc_process_config instead */
1116         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1117                                 sizeof(KEY_REGISTER_TARGET),
1118                                 KEY_REGISTER_TARGET,
1119                                 sizeof(*mti), mti, NULL);
1120
1121         /* Imperative recovery: if the mgs informs us to use IR? */
1122         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1123             (mti->mti_flags & LDD_F_IR_CAPABLE))
1124                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1125
1126 out:
1127         if (mti)
1128                 OBD_FREE_PTR(mti);
1129         RETURN(rc);
1130
1131 }
1132
1133 /** Start server targets: MDTs and OSTs
1134  */
1135 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
1136 {
1137         struct obd_device *obd;
1138         struct lustre_sb_info *lsi = s2lsi(sb);
1139         struct config_llog_instance cfg;
1140         struct lu_env env;
1141         struct lu_device *dev;
1142         int rc;
1143         ENTRY;
1144
1145         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1146
1147 #if 0
1148         /* If we're an MDT, make sure the global MDS is running */
1149         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_MDT) {
1150                 /* make sure the MDS is started */
1151                 cfs_mutex_lock(&server_start_lock);
1152                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1153                 if (!obd) {
1154                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1155                     /* FIXME pre-rename, should eventually be LUSTRE_MDS_NAME */
1156                                                  LUSTRE_MDT_NAME,
1157                                                  LUSTRE_MDS_OBDNAME"_uuid",
1158                                                  0, 0);
1159                         if (rc) {
1160                                 cfs_mutex_unlock(&server_start_lock);
1161                                 CERROR("failed to start MDS: %d\n", rc);
1162                                 RETURN(rc);
1163                         }
1164                 }
1165                 cfs_mutex_unlock(&server_start_lock);
1166         }
1167 #endif
1168
1169         /* If we're an OST, make sure the global OSS is running */
1170         if (IS_OST(lsi)) {
1171                 /* make sure OSS is started */
1172                 cfs_mutex_lock(&server_start_lock);
1173                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1174                 if (!obd) {
1175                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1176                                                  LUSTRE_OSS_NAME,
1177                                                  LUSTRE_OSS_OBDNAME"_uuid",
1178                                                  0, 0, 0, 0);
1179                         if (rc) {
1180                                 cfs_mutex_unlock(&server_start_lock);
1181                                 CERROR("failed to start OSS: %d\n", rc);
1182                                 RETURN(rc);
1183                         }
1184                 }
1185                 cfs_mutex_unlock(&server_start_lock);
1186         }
1187
1188         /* Set the mgc fs to our server disk.  This allows the MGC to
1189          * read and write configs locally, in case it can't talk to the MGS. */
1190         if (lsi->lsi_srv_mnt) {
1191                 rc = server_mgc_set_fs(lsi->lsi_mgc, sb);
1192                 if (rc)
1193                         RETURN(rc);
1194         }
1195
1196         /* Register with MGS */
1197         rc = server_register_target(lsi);
1198         if (rc)
1199                 GOTO(out_mgc, rc);
1200
1201         /* Let the target look up the mount using the target's name
1202            (we can't pass the sb or mnt through class_process_config.) */
1203         rc = server_register_mount(lsi->lsi_svname, sb, mnt);
1204         if (rc)
1205                 GOTO(out_mgc, rc);
1206
1207         /* Start targets using the llog named for the target */
1208         memset(&cfg, 0, sizeof(cfg));
1209         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1210         if (rc) {
1211                 CERROR("failed to start server %s: %d\n",
1212                        lsi->lsi_svname, rc);
1213                 /* Do NOT call server_deregister_mount() here. This makes it
1214                  * impossible to find mount later in cleanup time and leaves
1215                  * @lsi and othder stuff leaked. -umka */
1216                 GOTO(out_mgc, rc);
1217         }
1218
1219 out_mgc:
1220         /* Release the mgc fs for others to use */
1221         if (lsi->lsi_srv_mnt)
1222                 server_mgc_clear_fs(lsi->lsi_mgc);
1223
1224         if (!rc) {
1225                 obd = class_name2obd(lsi->lsi_svname);
1226                 if (!obd) {
1227                         CERROR("no server named %s was started\n",
1228                                lsi->lsi_svname);
1229                         RETURN(-ENXIO);
1230                 }
1231
1232                 server_notify_target(sb, obd);
1233
1234                 /* calculate recovery timeout, do it after lustre_process_log */
1235                 server_calc_timeout(lsi, obd);
1236
1237                 /* log has been fully processed */
1238                 obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1239
1240                 /* log has been fully processed, let clients connect */
1241                 dev = obd->obd_lu_dev;
1242                 if (dev && dev->ld_ops->ldo_prepare) {
1243                         rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1244                         if (rc == 0) {
1245                                 struct lu_context  session_ctx;
1246
1247                                 lu_context_init(&session_ctx, LCT_SESSION);
1248                                 session_ctx.lc_thread = NULL;
1249                                 lu_context_enter(&session_ctx);
1250                                 env.le_ses = &session_ctx;
1251
1252                                 dev->ld_ops->ldo_prepare(&env, NULL, dev);
1253
1254                                 lu_env_fini(&env);
1255                                 lu_context_exit(&session_ctx);
1256                                 lu_context_fini(&session_ctx);
1257                         }
1258                 }
1259
1260                 /* abort recovery only on the complete stack:
1261                  * many devices can be involved */
1262                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1263                     (OBP(obd, iocontrol))) {
1264                         obd_iocontrol(OBD_IOC_ABORT_RECOVERY,
1265                                       obd->obd_self_export, 0, NULL, NULL);
1266                 }
1267         }
1268
1269         RETURN(rc);
1270 }
1271
1272 /***************** lustre superblock **************/
1273
1274 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
1275 {
1276         struct lustre_sb_info *lsi;
1277         ENTRY;
1278
1279         OBD_ALLOC_PTR(lsi);
1280         if (!lsi)
1281                 RETURN(NULL);
1282         OBD_ALLOC_PTR(lsi->lsi_lmd);
1283         if (!lsi->lsi_lmd) {
1284                 OBD_FREE_PTR(lsi);
1285                 RETURN(NULL);
1286         }
1287
1288         lsi->lsi_lmd->lmd_exclude_count = 0;
1289         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
1290         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
1291         s2lsi_nocast(sb) = lsi;
1292         /* we take 1 extra ref for our setup */
1293         cfs_atomic_set(&lsi->lsi_mounts, 1);
1294
1295         /* Default umount style */
1296         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
1297
1298         RETURN(lsi);
1299 }
1300
1301 static int lustre_free_lsi(struct super_block *sb)
1302 {
1303         struct lustre_sb_info *lsi = s2lsi(sb);
1304         ENTRY;
1305
1306         LASSERT(lsi != NULL);
1307         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
1308
1309         /* someone didn't call server_put_mount. */
1310         LASSERT(cfs_atomic_read(&lsi->lsi_mounts) == 0);
1311
1312         if (lsi->lsi_lmd != NULL) {
1313                 if (lsi->lsi_lmd->lmd_dev != NULL)
1314                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
1315                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1316                 if (lsi->lsi_lmd->lmd_profile != NULL)
1317                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
1318                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
1319                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
1320                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
1321                                  strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
1322                 if (lsi->lsi_lmd->lmd_opts != NULL)
1323                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
1324                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1325                 if (lsi->lsi_lmd->lmd_exclude_count)
1326                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
1327                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
1328                                  lsi->lsi_lmd->lmd_exclude_count);
1329                 if (lsi->lsi_lmd->lmd_mgs != NULL)
1330                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
1331                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
1332                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
1333                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
1334                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
1335                 if (lsi->lsi_lmd->lmd_params != NULL)
1336                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
1337
1338                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1339         }
1340
1341         LASSERT(lsi->lsi_llsbi == NULL);
1342         OBD_FREE(lsi, sizeof(*lsi));
1343         s2lsi_nocast(sb) = NULL;
1344
1345         RETURN(0);
1346 }
1347
1348 /* The lsi has one reference for every server that is using the disk -
1349    e.g. MDT, MGS, and potentially MGC */
1350 static int lustre_put_lsi(struct super_block *sb)
1351 {
1352         struct lustre_sb_info *lsi = s2lsi(sb);
1353         ENTRY;
1354
1355         LASSERT(lsi != NULL);
1356
1357         CDEBUG(D_MOUNT, "put %p %d\n", sb, cfs_atomic_read(&lsi->lsi_mounts));
1358         if (cfs_atomic_dec_and_test(&lsi->lsi_mounts)) {
1359                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
1360                         obd_disconnect(lsi->lsi_osd_exp);
1361                         /* wait till OSD is gone */
1362                         obd_zombie_barrier();
1363                 }
1364                 lustre_free_lsi(sb);
1365                 RETURN(1);
1366         }
1367         RETURN(0);
1368 }
1369
1370 static int lsi_prepare(struct lustre_sb_info *lsi)
1371 {
1372         __u32 index;
1373         int rc;
1374         ENTRY;
1375
1376         LASSERT(lsi);
1377         LASSERT(lsi->lsi_lmd);
1378
1379         /* The server name is given as a mount line option */
1380         if (lsi->lsi_lmd->lmd_profile == NULL) {
1381                 LCONSOLE_ERROR("Can't determine server name\n");
1382                 RETURN(-EINVAL);
1383         }
1384
1385         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname))
1386                 RETURN(-ENAMETOOLONG);
1387
1388         strcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile);
1389
1390         /* Determine osd type */
1391         if (lsi->lsi_lmd->lmd_osd_type != NULL) {
1392                 if (strlen(lsi->lsi_lmd->lmd_osd_type) >=
1393                            sizeof(lsi->lsi_osd_type))
1394                         RETURN(-ENAMETOOLONG);
1395
1396                 strcpy(lsi->lsi_osd_type, lsi->lsi_lmd->lmd_osd_type);
1397         } else {
1398                 strcpy(lsi->lsi_osd_type, LUSTRE_OSD_LDISKFS_NAME);
1399         }
1400
1401         /* XXX: a temp. solution for components using fsfilt
1402          *      to be removed in one of the subsequent patches */
1403         if (!strcmp(lsi->lsi_lmd->lmd_osd_type, "osd-ldiskfs")) {
1404                 strcpy(lsi->lsi_fstype, "ldiskfs");
1405         } else {
1406                 strcpy(lsi->lsi_fstype, lsi->lsi_lmd->lmd_osd_type);
1407         }
1408
1409         /* Determine server type */
1410         rc = server_name2index(lsi->lsi_svname, &index, NULL);
1411         if (rc < 0) {
1412                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) {
1413                         /* Assume we're a bare MGS */
1414                         rc = 0;
1415                         lsi->lsi_lmd->lmd_flags |= LMD_FLG_NOSVC;
1416                 } else {
1417                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1418                                        lsi->lsi_svname);
1419                         RETURN(rc);
1420                 }
1421         }
1422         lsi->lsi_flags |= rc;
1423
1424         /* Add mount line flags that used to be in ldd:
1425          * writeconf, mgs, iam, anything else?
1426          */
1427         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_WRITECONF) ?
1428                 LDD_F_WRITECONF : 0;
1429         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_VIRGIN) ?
1430                 LDD_F_VIRGIN : 0;
1431         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) ?
1432                 LDD_F_SV_TYPE_MGS : 0;
1433         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_IAM) ?
1434                 LDD_F_IAM_DIR : 0;
1435         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) ?
1436                 LDD_F_NO_PRIMNODE : 0;
1437
1438         RETURN(0);
1439 }
1440
1441 /*************** server mount ******************/
1442
1443 /** Start the shutdown of servers at umount.
1444  */
1445 static void server_put_super(struct super_block *sb)
1446 {
1447         struct lustre_sb_info *lsi = s2lsi(sb);
1448         struct obd_device     *obd;
1449         char *tmpname, *extraname = NULL;
1450         int tmpname_sz;
1451         int lsiflags = lsi->lsi_flags;
1452         ENTRY;
1453
1454         LASSERT(IS_SERVER(lsi));
1455
1456         tmpname_sz = strlen(lsi->lsi_svname) + 1;
1457         OBD_ALLOC(tmpname, tmpname_sz);
1458         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1459         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1460         if (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC))
1461                 snprintf(tmpname, tmpname_sz, "MGS");
1462
1463         /* Stop the target */
1464         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1465             (IS_MDT(lsi) || IS_OST(lsi))) {
1466                 struct lustre_profile *lprof = NULL;
1467
1468                 /* tell the mgc to drop the config log */
1469                 lustre_end_log(sb, lsi->lsi_svname, NULL);
1470
1471                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1472                    If there are any setup/cleanup errors, save the lov
1473                    name for safety cleanup later. */
1474                 lprof = class_get_profile(lsi->lsi_svname);
1475                 if (lprof && lprof->lp_dt) {
1476                         OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1477                         strcpy(extraname, lprof->lp_dt);
1478                 }
1479
1480                 obd = class_name2obd(lsi->lsi_svname);
1481                 if (obd) {
1482                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1483                         if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
1484                                 obd->obd_fail = 1;
1485                         /* We can't seem to give an error return code
1486                          * to .put_super, so we better make sure we clean up! */
1487                         obd->obd_force = 1;
1488                         class_manual_cleanup(obd);
1489                 } else {
1490                         CERROR("no obd %s\n", lsi->lsi_svname);
1491                         server_deregister_mount(lsi->lsi_svname);
1492                 }
1493         }
1494
1495         /* If they wanted the mgs to stop separately from the mdt, they
1496            should have put it on a different device. */
1497         if (IS_MGS(lsi)) {
1498                 /* if MDS start with --nomgs, don't stop MGS then */
1499                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
1500                         server_stop_mgs(sb);
1501         }
1502
1503         /* Clean the mgc and sb */
1504         lustre_common_put_super(sb);
1505
1506         /* wait till all in-progress cleanups are done
1507          * specifically we're interested in ofd cleanup
1508          * as it pins OSS */
1509         obd_zombie_barrier();
1510
1511         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1512            until the target is really gone so that our type refcount check
1513            is right. */
1514         server_stop_servers(lsiflags);
1515
1516         /* In case of startup or cleanup err, stop related obds */
1517         if (extraname) {
1518                 obd = class_name2obd(extraname);
1519                 if (obd) {
1520                         CWARN("Cleaning orphaned obd %s\n", extraname);
1521                         obd->obd_force = 1;
1522                         class_manual_cleanup(obd);
1523                 }
1524                 OBD_FREE(extraname, strlen(extraname) + 1);
1525         }
1526
1527         LCONSOLE_WARN("server umount %s complete\n", tmpname);
1528         OBD_FREE(tmpname, tmpname_sz);
1529         EXIT;
1530 }
1531
1532 /** Called only for 'umount -f'
1533  */
1534 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1535 static void server_umount_begin(struct vfsmount *vfsmnt, int flags)
1536 {
1537         struct super_block *sb = vfsmnt->mnt_sb;
1538 #else
1539 static void server_umount_begin(struct super_block *sb)
1540 {
1541 #endif
1542         struct lustre_sb_info *lsi = s2lsi(sb);
1543         ENTRY;
1544
1545 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1546         if (!(flags & MNT_FORCE)) {
1547                 EXIT;
1548                 return;
1549         }
1550 #endif
1551
1552         CDEBUG(D_MOUNT, "umount -f\n");
1553         /* umount = failover
1554            umount -f = force
1555            no third way to do non-force, non-failover */
1556         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1557         EXIT;
1558 }
1559
1560 static int server_statfs (struct dentry *dentry, cfs_kstatfs_t *buf)
1561 {
1562         struct super_block *sb = dentry->d_sb;
1563         struct lustre_sb_info *lsi = s2lsi(sb);
1564         struct obd_statfs statfs;
1565         int rc;
1566         ENTRY;
1567
1568         if (lsi->lsi_dt_dev) {
1569                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1570                 if (rc == 0) {
1571                         statfs_unpack(buf, &statfs);
1572                         buf->f_type = sb->s_magic;
1573                         RETURN(0);
1574                 }
1575         }
1576
1577         /* just return 0 */
1578         buf->f_type = sb->s_magic;
1579         buf->f_bsize = sb->s_blocksize;
1580         buf->f_blocks = 1;
1581         buf->f_bfree = 0;
1582         buf->f_bavail = 0;
1583         buf->f_files = 1;
1584         buf->f_ffree = 0;
1585         buf->f_namelen = NAME_MAX;
1586         RETURN(0);
1587 }
1588
1589 /** The operations we support directly on the superblock:
1590  * mount, umount, and df.
1591  */
1592 static struct super_operations server_ops =
1593 {
1594         .put_super      = server_put_super,
1595         .umount_begin   = server_umount_begin, /* umount -f */
1596         .statfs         = server_statfs,
1597 };
1598
1599 #define log2(n) cfs_ffz(~(n))
1600 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1601
1602 static int server_fill_super_common(struct super_block *sb)
1603 {
1604         struct inode *root = 0;
1605         ENTRY;
1606
1607         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1608
1609         sb->s_blocksize = 4096;
1610         sb->s_blocksize_bits = log2(sb->s_blocksize);
1611         sb->s_magic = LUSTRE_SUPER_MAGIC;
1612         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
1613         sb->s_flags |= MS_RDONLY;
1614         sb->s_op = &server_ops;
1615
1616         root = new_inode(sb);
1617         if (!root) {
1618                 CERROR("Can't make root inode\n");
1619                 RETURN(-EIO);
1620         }
1621
1622         /* returns -EIO for every operation */
1623         /* make_bad_inode(root); -- badness - can't umount */
1624         /* apparently we need to be a directory for the mount to finish */
1625         root->i_mode = S_IFDIR;
1626
1627         sb->s_root = d_alloc_root(root);
1628         if (!sb->s_root) {
1629                 CERROR("Can't make root dentry\n");
1630                 iput(root);
1631                 RETURN(-EIO);
1632         }
1633
1634         RETURN(0);
1635 }
1636
1637 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
1638 {
1639         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1640         struct obd_device        *obd;
1641         struct dt_device_param    p;
1642         char                      flagstr[16];
1643         int                       rc;
1644         ENTRY;
1645
1646         CDEBUG(D_MOUNT,
1647                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
1648                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
1649
1650         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
1651         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
1652         strcat(lsi->lsi_osd_uuid, "_UUID");
1653         sprintf(flagstr, "%lu:%lu", mflags, (unsigned long) lmd->lmd_flags);
1654
1655         obd = class_name2obd(lsi->lsi_osd_obdname);
1656         if (obd == NULL) {
1657                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
1658                                 lsi->lsi_osd_type,
1659                                 lsi->lsi_osd_uuid, lmd->lmd_dev,
1660                                 flagstr, lsi->lsi_lmd->lmd_opts,
1661                                 lsi->lsi_svname);
1662                 if (rc)
1663                         GOTO(out, rc);
1664                 obd = class_name2obd(lsi->lsi_osd_obdname);
1665                 LASSERT(obd);
1666         }
1667
1668         rc = obd_connect(NULL, &lsi->lsi_osd_exp, obd, &obd->obd_uuid, NULL, NULL);
1669         if (rc) {
1670                 obd->obd_force = 1;
1671                 class_manual_cleanup(obd);
1672                 lsi->lsi_dt_dev = NULL;
1673         }
1674
1675         /* XXX: to keep support old components relying on lsi_srv_mnt
1676          *      we get this info from OSD just started */
1677         LASSERT(obd->obd_lu_dev);
1678         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
1679         LASSERT(lsi->lsi_dt_dev);
1680
1681         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
1682
1683         lsi->lsi_srv_mnt = p.ddp_mnt;
1684
1685 out:
1686         RETURN(rc);
1687 }
1688
1689 /** Fill in the superblock info for a Lustre server.
1690  * Mount the device with the correct options.
1691  * Read the on-disk config file.
1692  * Start the services.
1693  */
1694 static int server_fill_super(struct super_block *sb)
1695 {
1696         struct lustre_sb_info *lsi = s2lsi(sb);
1697         int rc;
1698         ENTRY;
1699
1700         rc = lsi_prepare(lsi);
1701         if (rc)
1702                 RETURN(rc);
1703
1704         /* Start low level OSD */
1705         rc = osd_start(lsi, sb->s_flags);
1706         if (rc) {
1707                 CERROR("Unable to start osd on %s: %d\n",
1708                        lsi->lsi_lmd->lmd_dev, rc);
1709                 lustre_put_lsi(sb);
1710                 RETURN(rc);
1711         }
1712
1713         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
1714                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
1715
1716         if (class_name2obd(lsi->lsi_svname)) {
1717                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
1718                                    "running. Double-mount may have compromised"
1719                                    " the disk journal.\n",
1720                                    lsi->lsi_svname);
1721                 lustre_put_lsi(sb);
1722                 RETURN(-EALREADY);
1723         }
1724
1725         /* Start MGS before MGC */
1726         if (IS_MGS(lsi) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)){
1727                 rc = server_start_mgs(sb);
1728                 if (rc)
1729                         GOTO(out_mnt, rc);
1730         }
1731
1732         /* Start MGC before servers */
1733         rc = lustre_start_mgc(sb);
1734         if (rc)
1735                 GOTO(out_mnt, rc);
1736
1737         /* Set up all obd devices for service */
1738         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1739                         (IS_OST(lsi) || IS_MDT(lsi))) {
1740                 rc = server_start_targets(sb, lsi->lsi_srv_mnt);
1741                 if (rc < 0) {
1742                         CERROR("Unable to start targets: %d\n", rc);
1743                         GOTO(out_mnt, rc);
1744                 }
1745         /* FIXME overmount client here,
1746            or can we just start a client log and client_fill_super on this sb?
1747            We need to make sure server_put_super gets called too - ll_put_super
1748            calls lustre_common_put_super; check there for LSI_SERVER flag,
1749            call s_p_s if so.
1750            Probably should start client from new thread so we can return.
1751            Client will not finish until all servers are connected.
1752            Note - MGS-only server does NOT get a client, since there is no
1753            lustre fs associated - the MGS is for all lustre fs's */
1754         }
1755
1756         rc = server_fill_super_common(sb);
1757         if (rc)
1758                 GOTO(out_mnt, rc);
1759
1760         RETURN(0);
1761 out_mnt:
1762         /* We jump here in case of failure while starting targets or MGS.
1763          * In this case we can't just put @mnt and have to do real cleanup
1764          * with stoping targets, etc. */
1765         server_put_super(sb);
1766         return rc;
1767 }
1768
1769 /*
1770  * Calculate timeout value for a target.
1771  */
1772 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
1773 {
1774         struct lustre_mount_data *lmd;
1775         int soft = 0;
1776         int hard = 0;
1777         int factor = 0;
1778         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
1779         int min = OBD_RECOVERY_TIME_MIN;
1780
1781         LASSERT(IS_SERVER(lsi));
1782
1783         lmd = lsi->lsi_lmd;
1784         if (lmd) {
1785                 soft   = lmd->lmd_recovery_time_soft;
1786                 hard   = lmd->lmd_recovery_time_hard;
1787                 has_ir = has_ir && !(lmd->lmd_flags & LMD_FLG_NOIR);
1788                 obd->obd_no_ir = !has_ir;
1789         }
1790
1791         if (soft == 0)
1792                 soft = OBD_RECOVERY_TIME_SOFT;
1793         if (hard == 0)
1794                 hard = OBD_RECOVERY_TIME_HARD;
1795
1796         /* target may have ir_factor configured. */
1797         factor = OBD_IR_FACTOR_DEFAULT;
1798         if (obd->obd_recovery_ir_factor)
1799                 factor = obd->obd_recovery_ir_factor;
1800
1801         if (has_ir) {
1802                 int new_soft = soft;
1803                 int new_hard = hard;
1804
1805                 /* adjust timeout value by imperative recovery */
1806
1807                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
1808                 new_hard = (hard * factor) / OBD_IR_FACTOR_MAX;
1809
1810                 /* make sure the timeout is not too short */
1811                 new_soft = max(min, new_soft);
1812                 new_hard = max(new_soft, new_hard);
1813
1814                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery "
1815                               "window shrunk from %d-%d down to %d-%d\n",
1816                               obd->obd_name, soft, hard, new_soft, new_hard);
1817
1818                 soft = new_soft;
1819                 hard = new_hard;
1820         }
1821
1822         /* we're done */
1823         obd->obd_recovery_timeout   = max(obd->obd_recovery_timeout, soft);
1824         obd->obd_recovery_time_hard = hard;
1825         obd->obd_recovery_ir_factor = factor;
1826 }
1827 EXPORT_SYMBOL(server_calc_timeout);
1828
1829 /*************** mount common betweeen server and client ***************/
1830
1831 /* Common umount */
1832 int lustre_common_put_super(struct super_block *sb)
1833 {
1834         int rc;
1835         ENTRY;
1836
1837         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
1838
1839         /* Drop a ref to the MGC */
1840         rc = lustre_stop_mgc(sb);
1841         if (rc && (rc != -ENOENT)) {
1842                 if (rc != -EBUSY) {
1843                         CERROR("Can't stop MGC: %d\n", rc);
1844                         RETURN(rc);
1845                 }
1846                 /* BUSY just means that there's some other obd that
1847                    needs the mgc.  Let him clean it up. */
1848                 CDEBUG(D_MOUNT, "MGC still in use\n");
1849         }
1850         /* Drop a ref to the mounted disk */
1851         lustre_put_lsi(sb);
1852         lu_types_stop();
1853         RETURN(rc);
1854 }
1855 EXPORT_SYMBOL(lustre_common_put_super);
1856
1857 static void lmd_print(struct lustre_mount_data *lmd)
1858 {
1859         int i;
1860
1861         PRINT_CMD(PRINT_MASK, "  mount data:\n");
1862         if (lmd_is_client(lmd))
1863                 PRINT_CMD(PRINT_MASK, "profile: %s\n", lmd->lmd_profile);
1864         PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
1865         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
1866
1867         if (lmd->lmd_opts)
1868                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
1869
1870         if (lmd->lmd_recovery_time_soft)
1871                 PRINT_CMD(PRINT_MASK, "recovery time soft: %d\n",
1872                           lmd->lmd_recovery_time_soft);
1873
1874         if (lmd->lmd_recovery_time_hard)
1875                 PRINT_CMD(PRINT_MASK, "recovery time hard: %d\n",
1876                           lmd->lmd_recovery_time_hard);
1877
1878         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1879                 PRINT_CMD(PRINT_MASK, "exclude %d:  OST%04x\n", i,
1880                           lmd->lmd_exclude[i]);
1881         }
1882 }
1883
1884 /* Is this server on the exclusion list */
1885 int lustre_check_exclusion(struct super_block *sb, char *svname)
1886 {
1887         struct lustre_sb_info *lsi = s2lsi(sb);
1888         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1889         __u32 index;
1890         int i, rc;
1891         ENTRY;
1892
1893         rc = server_name2index(svname, &index, NULL);
1894         if (rc != LDD_F_SV_TYPE_OST)
1895                 /* Only exclude OSTs */
1896                 RETURN(0);
1897
1898         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1899                index, lmd->lmd_exclude_count, lmd->lmd_dev);
1900
1901         for(i = 0; i < lmd->lmd_exclude_count; i++) {
1902                 if (index == lmd->lmd_exclude[i]) {
1903                         CWARN("Excluding %s (on exclusion list)\n", svname);
1904                         RETURN(1);
1905                 }
1906         }
1907         RETURN(0);
1908 }
1909
1910 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1911 static int lmd_make_exclusion(struct lustre_mount_data *lmd, char *ptr)
1912 {
1913         char *s1 = ptr, *s2;
1914         __u32 index, *exclude_list;
1915         int rc = 0, devmax;
1916         ENTRY;
1917
1918         /* The shortest an ost name can be is 8 chars: -OST0000.
1919            We don't actually know the fsname at this time, so in fact
1920            a user could specify any fsname. */
1921         devmax = strlen(ptr) / 8 + 1;
1922
1923         /* temp storage until we figure out how many we have */
1924         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
1925         if (!exclude_list)
1926                 RETURN(-ENOMEM);
1927
1928         /* we enter this fn pointing at the '=' */
1929         while (*s1 && *s1 != ' ' && *s1 != ',') {
1930                 s1++;
1931                 rc = server_name2index(s1, &index, &s2);
1932                 if (rc < 0) {
1933                         CERROR("Can't parse server name '%s'\n", s1);
1934                         break;
1935                 }
1936                 if (rc == LDD_F_SV_TYPE_OST)
1937                         exclude_list[lmd->lmd_exclude_count++] = index;
1938                 else
1939                         CDEBUG(D_MOUNT, "ignoring exclude %.7s\n", s1);
1940                 s1 = s2;
1941                 /* now we are pointing at ':' (next exclude)
1942                    or ',' (end of excludes) */
1943                 if (lmd->lmd_exclude_count >= devmax)
1944                         break;
1945         }
1946         if (rc >= 0) /* non-err */
1947                 rc = 0;
1948
1949         if (lmd->lmd_exclude_count) {
1950                 /* permanent, freed in lustre_free_lsi */
1951                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
1952                           lmd->lmd_exclude_count);
1953                 if (lmd->lmd_exclude) {
1954                         memcpy(lmd->lmd_exclude, exclude_list,
1955                                sizeof(index) * lmd->lmd_exclude_count);
1956                 } else {
1957                         rc = -ENOMEM;
1958                         lmd->lmd_exclude_count = 0;
1959                 }
1960         }
1961         OBD_FREE(exclude_list, sizeof(index) * devmax);
1962         RETURN(rc);
1963 }
1964
1965 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1966 {
1967         char   *tail;
1968         int     length;
1969
1970         if (lmd->lmd_mgssec != NULL) {
1971                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1972                 lmd->lmd_mgssec = NULL;
1973         }
1974
1975         tail = strchr(ptr, ',');
1976         if (tail == NULL)
1977                 length = strlen(ptr);
1978         else
1979                 length = tail - ptr;
1980
1981         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1982         if (lmd->lmd_mgssec == NULL)
1983                 return -ENOMEM;
1984
1985         memcpy(lmd->lmd_mgssec, ptr, length);
1986         lmd->lmd_mgssec[length] = '\0';
1987         return 0;
1988 }
1989
1990 static int lmd_parse_string(char **handle, char *ptr)
1991 {
1992         char   *tail;
1993         int     length;
1994
1995         if ((handle == NULL) || (ptr == NULL))
1996                 return -EINVAL;
1997
1998         if (*handle != NULL) {
1999                 OBD_FREE(*handle, strlen(*handle) + 1);
2000                 *handle = NULL;
2001         }
2002
2003         tail = strchr(ptr, ',');
2004         if (tail == NULL)
2005                 length = strlen(ptr);
2006         else
2007                 length = tail - ptr;
2008
2009         OBD_ALLOC(*handle, length + 1);
2010         if (*handle == NULL)
2011                 return -ENOMEM;
2012
2013         memcpy(*handle, ptr, length);
2014         (*handle)[length] = '\0';
2015
2016         return 0;
2017 }
2018
2019 /* Collect multiple values for mgsnid specifiers */
2020 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
2021 {
2022         lnet_nid_t nid;
2023         char *tail = *ptr;
2024         char *mgsnid;
2025         int   length;
2026         int   oldlen = 0;
2027
2028         /* Find end of nidlist */
2029         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
2030         length = tail - *ptr;
2031         if (length == 0) {
2032                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
2033                 return -EINVAL;
2034         }
2035
2036         if (lmd->lmd_mgs != NULL)
2037                 oldlen = strlen(lmd->lmd_mgs) + 1;
2038
2039         OBD_ALLOC(mgsnid, oldlen + length + 1);
2040         if (mgsnid == NULL)
2041                 return -ENOMEM;
2042
2043         if (lmd->lmd_mgs != NULL) {
2044                 /* Multiple mgsnid= are taken to mean failover locations */
2045                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
2046                 mgsnid[oldlen - 1] = ':';
2047                 OBD_FREE(lmd->lmd_mgs, oldlen);
2048         }
2049         memcpy(mgsnid + oldlen, *ptr, length);
2050         mgsnid[oldlen + length] = '\0';
2051         lmd->lmd_mgs = mgsnid;
2052         *ptr = tail;
2053
2054         return 0;
2055 }
2056
2057 /** Parse mount line options
2058  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
2059  * dev is passed as device=uml1:/lustre by mount.lustre
2060  */
2061 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
2062 {
2063         char *s1, *s2, *devname = NULL;
2064         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
2065         int rc = 0;
2066         ENTRY;
2067
2068         LASSERT(lmd);
2069         if (!options) {
2070                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
2071                                    "/sbin/mount.lustre is installed.\n");
2072                 RETURN(-EINVAL);
2073         }
2074
2075         /* Options should be a string - try to detect old lmd data */
2076         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
2077                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
2078                                    "/sbin/mount.lustre.  Please install "
2079                                    "version %s\n", LUSTRE_VERSION_STRING);
2080                 RETURN(-EINVAL);
2081         }
2082         lmd->lmd_magic = LMD_MAGIC;
2083
2084         OBD_ALLOC(lmd->lmd_params, 4096);
2085         if (lmd->lmd_params == NULL)
2086                 RETURN(-ENOMEM);
2087         lmd->lmd_params[0] = '\0';
2088
2089         /* Set default flags here */
2090
2091         s1 = options;
2092         while (*s1) {
2093                 int clear = 0;
2094                 int time_min = OBD_RECOVERY_TIME_MIN;
2095
2096                 /* Skip whitespace and extra commas */
2097                 while (*s1 == ' ' || *s1 == ',')
2098                         s1++;
2099
2100                 /* Client options are parsed in ll_options: eg. flock,
2101                    user_xattr, acl */
2102
2103                 /* Parse non-ldiskfs options here. Rather than modifying
2104                    ldiskfs, we just zero these out here */
2105                 if (strncmp(s1, "abort_recov", 11) == 0) {
2106                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
2107                         clear++;
2108                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
2109                         lmd->lmd_recovery_time_soft = max_t(int,
2110                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2111                         clear++;
2112                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
2113                         lmd->lmd_recovery_time_hard = max_t(int,
2114                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
2115                         clear++;
2116                 } else if (strncmp(s1, "noir", 4) == 0) {
2117                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
2118                         clear++;
2119                 } else if (strncmp(s1, "nosvc", 5) == 0) {
2120                         lmd->lmd_flags |= LMD_FLG_NOSVC;
2121                         clear++;
2122                 } else if (strncmp(s1, "nomgs", 5) == 0) {
2123                         lmd->lmd_flags |= LMD_FLG_NOMGS;
2124                         clear++;
2125                 } else if (strncmp(s1, "noscrub", 7) == 0) {
2126                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
2127                         clear++;
2128                 } else if (strncmp(s1, PARAM_MGSNODE,
2129                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
2130                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
2131                         /* Assume the next mount opt is the first
2132                            invalid nid we get to. */
2133                         rc = lmd_parse_mgs(lmd, &s2);
2134                         if (rc)
2135                                 goto invalid;
2136                         clear++;
2137                 } else if (strncmp(s1, "writeconf", 9) == 0) {
2138                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
2139                         clear++;
2140                 } else if (strncmp(s1, "virgin", 6) == 0) {
2141                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
2142                         clear++;
2143                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
2144                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
2145                         clear++;
2146                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
2147                         rc = lmd_parse_mgssec(lmd, s1 + 7);
2148                         if (rc)
2149                                 goto invalid;
2150                         clear++;
2151                 /* ost exclusion list */
2152                 } else if (strncmp(s1, "exclude=", 8) == 0) {
2153                         rc = lmd_make_exclusion(lmd, s1 + 7);
2154                         if (rc)
2155                                 goto invalid;
2156                         clear++;
2157                 } else if (strncmp(s1, "mgs", 3) == 0) {
2158                         /* We are an MGS */
2159                         lmd->lmd_flags |= LMD_FLG_MGS;
2160                         clear++;
2161                 } else if (strncmp(s1, "svname=", 7) == 0) {
2162                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
2163                         if (rc)
2164                                 goto invalid;
2165                         clear++;
2166                 } else if (strncmp(s1, "param=", 6) == 0) {
2167                         int length;
2168                         char *tail = strchr(s1 + 6, ',');
2169                         if (tail == NULL)
2170                                 length = strlen(s1);
2171                         else
2172                                 length = tail - s1;
2173                         length -= 6;
2174                         strncat(lmd->lmd_params, s1 + 6, length);
2175                         strcat(lmd->lmd_params, " ");
2176                         clear++;
2177                 } else if (strncmp(s1, "osd=", 4) == 0) {
2178                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
2179                         if (rc)
2180                                 goto invalid;
2181                         clear++;
2182                 }
2183                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
2184                    end of the options. */
2185                 else if (strncmp(s1, "device=", 7) == 0) {
2186                         devname = s1 + 7;
2187                         /* terminate options right before device.  device
2188                            must be the last one. */
2189                         *s1 = '\0';
2190                         break;
2191                 }
2192
2193                 /* Find next opt */
2194                 s2 = strchr(s1, ',');
2195                 if (s2 == NULL) {
2196                         if (clear)
2197                                 *s1 = '\0';
2198                         break;
2199                 }
2200                 s2++;
2201                 if (clear)
2202                         memmove(s1, s2, strlen(s2) + 1);
2203                 else
2204                         s1 = s2;
2205         }
2206
2207         if (!devname) {
2208                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
2209                                    "(need mount option 'device=...')\n");
2210                 goto invalid;
2211         }
2212
2213         s1 = strstr(devname, ":/");
2214         if (s1) {
2215                 ++s1;
2216                 lmd->lmd_flags |= LMD_FLG_CLIENT;
2217                 /* Remove leading /s from fsname */
2218                 while (*++s1 == '/') ;
2219                 /* Freed in lustre_free_lsi */
2220                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
2221                 if (!lmd->lmd_profile)
2222                         RETURN(-ENOMEM);
2223                 sprintf(lmd->lmd_profile, "%s-client", s1);
2224         }
2225
2226         /* Freed in lustre_free_lsi */
2227         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
2228         if (!lmd->lmd_dev)
2229                 RETURN(-ENOMEM);
2230         strcpy(lmd->lmd_dev, devname);
2231
2232         /* Save mount options */
2233         s1 = options + strlen(options) - 1;
2234         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
2235                 *s1-- = 0;
2236         if (*options != 0) {
2237                 /* Freed in lustre_free_lsi */
2238                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
2239                 if (!lmd->lmd_opts)
2240                         RETURN(-ENOMEM);
2241                 strcpy(lmd->lmd_opts, options);
2242         }
2243
2244         lmd_print(lmd);
2245         lmd->lmd_magic = LMD_MAGIC;
2246
2247         RETURN(rc);
2248
2249 invalid:
2250         CERROR("Bad mount options %s\n", options);
2251         RETURN(-EINVAL);
2252 }
2253
2254 struct lustre_mount_data2 {
2255         void *lmd2_data;
2256         struct vfsmount *lmd2_mnt;
2257 };
2258
2259 /** This is the entry point for the mount call into Lustre.
2260  * This is called when a server or client is mounted,
2261  * and this is where we start setting things up.
2262  * @param data Mount options (e.g. -o flock,abort_recov)
2263  */
2264 int lustre_fill_super(struct super_block *sb, void *data, int silent)
2265 {
2266         struct lustre_mount_data *lmd;
2267         struct lustre_mount_data2 *lmd2 = data;
2268         struct lustre_sb_info *lsi;
2269         int rc;
2270         ENTRY;
2271
2272         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2273
2274         lsi = lustre_init_lsi(sb);
2275         if (!lsi)
2276                 RETURN(-ENOMEM);
2277         lmd = lsi->lsi_lmd;
2278
2279         /*
2280          * Disable lockdep during mount, because mount locking patterns are
2281          * `special'.
2282          */
2283         cfs_lockdep_off();
2284
2285         /*
2286          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
2287          */
2288         obd_zombie_barrier();
2289
2290         /* Figure out the lmd from the mount options */
2291         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
2292                 lustre_put_lsi(sb);
2293                 GOTO(out, rc = -EINVAL);
2294         }
2295
2296         if (lmd_is_client(lmd)) {
2297                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
2298                 if (!client_fill_super) {
2299                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
2300                                            "client mount! Is the 'lustre' "
2301                                            "module loaded?\n");
2302                         lustre_put_lsi(sb);
2303                         rc = -ENODEV;
2304                 } else {
2305                         rc = lustre_start_mgc(sb);
2306                         if (rc) {
2307                                 lustre_put_lsi(sb);
2308                                 GOTO(out, rc);
2309                         }
2310                         /* Connect and start */
2311                         /* (should always be ll_fill_super) */
2312                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
2313                         /* c_f_s will call lustre_common_put_super on failure */
2314                 }
2315         } else {
2316                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2317                 rc = server_fill_super(sb);
2318                 /* s_f_s calls lustre_start_mgc after the mount because we need
2319                    the MGS nids which are stored on disk.  Plus, we may
2320                    need to start the MGS first. */
2321                 /* s_f_s will call server_put_super on failure */
2322         }
2323
2324         /* If error happens in fill_super() call, @lsi will be killed there.
2325          * This is why we do not put it here. */
2326         GOTO(out, rc);
2327 out:
2328         if (rc) {
2329                 CERROR("Unable to mount %s (%d)\n",
2330                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
2331         } else {
2332                 CDEBUG(D_SUPER, "Mount %s complete\n",
2333                        lmd->lmd_dev);
2334         }
2335         cfs_lockdep_on();
2336         return rc;
2337 }
2338
2339
2340 /* We can't call ll_fill_super by name because it lives in a module that
2341    must be loaded after this one. */
2342 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
2343                                                   struct vfsmount *mnt))
2344 {
2345         client_fill_super = cfs;
2346 }
2347 EXPORT_SYMBOL(lustre_register_client_fill_super);
2348
2349 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
2350 {
2351         kill_super_cb = cfs;
2352 }
2353 EXPORT_SYMBOL(lustre_register_kill_super_cb);
2354
2355 /***************** FS registration ******************/
2356 #ifdef HAVE_FSTYPE_MOUNT
2357 struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
2358                                 const char *devname, void *data)
2359 {
2360         struct lustre_mount_data2 lmd2 = { data, NULL };
2361
2362         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
2363 }
2364 #else
2365 int lustre_get_sb(struct file_system_type *fs_type, int flags,
2366                   const char *devname, void * data, struct vfsmount *mnt)
2367 {
2368         struct lustre_mount_data2 lmd2 = { data, mnt };
2369
2370         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
2371 }
2372 #endif
2373
2374 void lustre_kill_super(struct super_block *sb)
2375 {
2376         struct lustre_sb_info *lsi = s2lsi(sb);
2377
2378         if (kill_super_cb && lsi && !IS_SERVER(lsi))
2379                 (*kill_super_cb)(sb);
2380
2381         kill_anon_super(sb);
2382 }
2383
2384 /** Register the "lustre" fs type
2385  */
2386 struct file_system_type lustre_fs_type = {
2387         .owner        = THIS_MODULE,
2388         .name         = "lustre",
2389 #ifdef HAVE_FSTYPE_MOUNT
2390         .mount        = lustre_mount,
2391 #else
2392         .get_sb       = lustre_get_sb,
2393 #endif
2394         .kill_sb      = lustre_kill_super,
2395         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
2396 #ifdef FS_HAS_FIEMAP
2397                         FS_HAS_FIEMAP |
2398 #endif
2399                         LL_RENAME_DOES_D_MOVE,
2400 };
2401
2402 int lustre_register_fs(void)
2403 {
2404         return register_filesystem(&lustre_fs_type);
2405 }
2406
2407 int lustre_unregister_fs(void)
2408 {
2409         return unregister_filesystem(&lustre_fs_type);
2410 }
2411
2412 EXPORT_SYMBOL(server_mti_print);