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