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