Whamcloud - gitweb
LU-6210 obd: Change positional struct initializers to C99
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_mount.c
33  *
34  * Client mount routines
35  *
36  * Author: Nathan Rutman <nathan@clusterfs.com>
37  */
38
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
42 #define PRINT_CMD CDEBUG
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre/lustre_user.h>
47 #include <linux/version.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <lustre_param.h>
51
52 static int (*client_fill_super)(struct super_block *sb,
53                                 struct vfsmount *mnt);
54
55 static void (*kill_super_cb)(struct super_block *sb);
56
57 /**************** config llog ********************/
58
59 /** Get a config log from the MGS and process it.
60  * This func is called for both clients and servers.
61  * Continue to process new statements appended to the logs
62  * (whenever the config lock is revoked) until lustre_end_log
63  * is called.
64  * @param sb The superblock is used by the MGC to write to the local copy of
65  *   the config log
66  * @param logname The name of the llog to replicate from the MGS
67  * @param cfg Since the same mgc may be used to follow multiple config logs
68  *   (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
69  *   this log, and is added to the mgc's list of logs to follow.
70  */
71 int lustre_process_log(struct super_block *sb, char *logname,
72                      struct config_llog_instance *cfg)
73 {
74         struct lustre_cfg *lcfg;
75         struct lustre_cfg_bufs *bufs;
76         struct lustre_sb_info *lsi = s2lsi(sb);
77         struct obd_device *mgc = lsi->lsi_mgc;
78         int rc;
79         ENTRY;
80
81         LASSERT(mgc);
82         LASSERT(cfg);
83
84         OBD_ALLOC_PTR(bufs);
85         if (bufs == NULL)
86                 RETURN(-ENOMEM);
87
88         /* mgc_process_config */
89         lustre_cfg_bufs_reset(bufs, mgc->obd_name);
90         lustre_cfg_bufs_set_string(bufs, 1, logname);
91         lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
92         lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
93         lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
94         if (lcfg == NULL)
95                 GOTO(out, rc = -ENOMEM);
96         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
97         lustre_cfg_free(lcfg);
98 out:
99         OBD_FREE_PTR(bufs);
100
101         if (rc == -EINVAL)
102                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
103                                    "failed from the MGS (%d).  Make sure this "
104                                    "client and the MGS are running compatible "
105                                    "versions of Lustre.\n",
106                                    mgc->obd_name, logname, rc);
107         else if (rc != 0)
108                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
109                                    "failed (%d). This may be the result of "
110                                    "communication errors between this node and "
111                                    "the MGS, a bad configuration, or other "
112                                    "errors. See the syslog for more "
113                                    "information.\n", mgc->obd_name, logname,
114                                    rc);
115
116         /* class_obd_list(); */
117         RETURN(rc);
118 }
119 EXPORT_SYMBOL(lustre_process_log);
120
121 /* Stop watching this config log for updates */
122 int lustre_end_log(struct super_block *sb, char *logname,
123                        struct config_llog_instance *cfg)
124 {
125         struct lustre_cfg *lcfg;
126         struct lustre_cfg_bufs bufs;
127         struct lustre_sb_info *lsi = s2lsi(sb);
128         struct obd_device *mgc = lsi->lsi_mgc;
129         int rc;
130         ENTRY;
131
132         if (!mgc)
133                 RETURN(-ENOENT);
134
135         /* mgc_process_config */
136         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
137         lustre_cfg_bufs_set_string(&bufs, 1, logname);
138         if (cfg)
139                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
140         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
141         if (lcfg == NULL)
142                 RETURN(-ENOMEM);
143         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
144         lustre_cfg_free(lcfg);
145         RETURN(rc);
146 }
147 EXPORT_SYMBOL(lustre_end_log);
148
149 /**************** obd start *******************/
150
151 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
152  * lctl (and do for echo cli/srv.
153  */
154 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
155             char *s1, char *s2, char *s3, char *s4)
156 {
157         struct lustre_cfg_bufs bufs;
158         struct lustre_cfg    * lcfg = NULL;
159         int rc;
160
161         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
162                cmd, s1, s2, s3, s4);
163
164         lustre_cfg_bufs_reset(&bufs, cfgname);
165         if (s1)
166                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
167         if (s2)
168                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
169         if (s3)
170                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
171         if (s4)
172                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
173
174         lcfg = lustre_cfg_new(cmd, &bufs);
175         if (lcfg == NULL)
176                 return -ENOMEM;
177         lcfg->lcfg_nid = nid;
178         rc = class_process_config(lcfg);
179         lustre_cfg_free(lcfg);
180         return(rc);
181 }
182
183 /** Call class_attach and class_setup.  These methods in turn call
184  * obd type-specific methods.
185  */
186 int lustre_start_simple(char *obdname, char *type, char *uuid,
187                         char *s1, char *s2, char *s3, char *s4)
188 {
189         int rc;
190         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
191
192         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
193         if (rc) {
194                 CERROR("%s attach error %d\n", obdname, rc);
195                 return rc;
196         }
197         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
198         if (rc) {
199                 CERROR("%s setup error %d\n", obdname, rc);
200                 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
201         }
202         return rc;
203 }
204
205 static DEFINE_MUTEX(mgc_start_lock);
206
207 /** Set up a mgc obd to process startup logs
208  *
209  * \param sb [in] super block of the mgc obd
210  *
211  * \retval 0 success, otherwise error code
212  */
213 int lustre_start_mgc(struct super_block *sb)
214 {
215         struct obd_connect_data *data = NULL;
216         struct lustre_sb_info *lsi = s2lsi(sb);
217         struct obd_device *obd;
218         struct obd_export *exp;
219         struct obd_uuid *uuid;
220         class_uuid_t uuidc;
221         lnet_nid_t nid;
222         char nidstr[LNET_NIDSTR_SIZE];
223         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
224         char *ptr;
225         int rc = 0, i = 0, j;
226         size_t len;
227         ENTRY;
228
229         LASSERT(lsi->lsi_lmd);
230
231         /* Find the first non-lo MGS nid for our MGC name */
232         if (IS_SERVER(lsi)) {
233                 /* mount -o mgsnode=nid */
234                 ptr = lsi->lsi_lmd->lmd_mgs;
235                 if (lsi->lsi_lmd->lmd_mgs &&
236                     (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
237                         i++;
238                 } else if (IS_MGS(lsi)) {
239                         lnet_process_id_t id;
240                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
241                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
242                                         continue;
243                                 nid = id.nid;
244                                 i++;
245                                 break;
246                         }
247                 }
248         } else { /* client */
249                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
250                 ptr = lsi->lsi_lmd->lmd_dev;
251                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
252                         i++;
253         }
254         if (i == 0) {
255                 CERROR("No valid MGS nids found.\n");
256                 RETURN(-EINVAL);
257         }
258
259         mutex_lock(&mgc_start_lock);
260
261         libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
262         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
263         OBD_ALLOC(mgcname, len);
264         OBD_ALLOC(niduuid, len + 2);
265         if (mgcname == NULL || niduuid == NULL)
266                 GOTO(out_free, rc = -ENOMEM);
267         snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
268
269         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
270
271         OBD_ALLOC_PTR(data);
272         if (data == NULL)
273                 GOTO(out_free, rc = -ENOMEM);
274
275         obd = class_name2obd(mgcname);
276         if (obd && !obd->obd_stopping) {
277                 int recov_bk;
278
279                 rc = obd_set_info_async(NULL, obd->obd_self_export,
280                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
281                                         strlen(mgssec), mgssec, NULL);
282                 if (rc)
283                         GOTO(out_free, rc);
284
285                 /* Re-using an existing MGC */
286                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
287
288                 /* IR compatibility check, only for clients */
289                 if (lmd_is_client(lsi->lsi_lmd)) {
290                         int has_ir;
291                         int vallen = sizeof(*data);
292                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
293
294                         rc = obd_get_info(NULL, obd->obd_self_export,
295                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
296                                           &vallen, data);
297                         LASSERT(rc == 0);
298                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
299                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
300                                 /* LMD_FLG_NOIR is for test purpose only */
301                                 LCONSOLE_WARN(
302                                     "Trying to mount a client with IR setting "
303                                     "not compatible with current mgc. "
304                                     "Force to use current mgc setting that is "
305                                     "IR %s.\n",
306                                     has_ir ? "enabled" : "disabled");
307                                 if (has_ir)
308                                         *flags &= ~LMD_FLG_NOIR;
309                                 else
310                                         *flags |= LMD_FLG_NOIR;
311                         }
312                 }
313
314                 recov_bk = 0;
315                 /* If we are restarting the MGS, don't try to keep the MGC's
316                    old connection, or registration will fail. */
317                 if (IS_MGS(lsi)) {
318                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
319                         recov_bk = 1;
320                 }
321
322                 /* Try all connections, but only once (again).
323                    We don't want to block another target from starting
324                    (using its local copy of the log), but we do want to connect
325                    if at all possible. */
326                 recov_bk++;
327                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
328                 rc = obd_set_info_async(NULL, obd->obd_self_export,
329                                         sizeof(KEY_INIT_RECOV_BACKUP),
330                                         KEY_INIT_RECOV_BACKUP,
331                                         sizeof(recov_bk), &recov_bk, NULL);
332                 GOTO(out, rc = 0);
333         }
334
335         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
336
337         /* Add the primary nids for the MGS */
338         i = 0;
339         snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
340         if (IS_SERVER(lsi)) {
341                 ptr = lsi->lsi_lmd->lmd_mgs;
342                 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
343                 if (IS_MGS(lsi)) {
344                         /* Use local nids (including LO) */
345                         lnet_process_id_t id;
346                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
347                                 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
348                                              niduuid, NULL, NULL, NULL);
349                         }
350                 } else {
351                         /* Use mgsnode= nids */
352                         /* mount -o mgsnode=nid */
353                         if (lsi->lsi_lmd->lmd_mgs) {
354                                 ptr = lsi->lsi_lmd->lmd_mgs;
355                         } else if (class_find_param(ptr, PARAM_MGSNODE,
356                                                     &ptr) != 0) {
357                                 CERROR("No MGS nids given.\n");
358                                 GOTO(out_free, rc = -EINVAL);
359                         }
360                         /*
361                          * LU-3829.
362                          * Here we only take the first mgsnid as its primary
363                          * serving mgs node, the rest mgsnid will be taken as
364                          * failover mgs node, otherwise they would be takens
365                          * as multiple nids of a single mgs node.
366                          */
367                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
368                                 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
369                                              niduuid, NULL, NULL, NULL);
370                                 if (rc == 0) {
371                                         i = 1;
372                                         break;
373                                 }
374                         }
375                 }
376         } else { /* client */
377                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
378                 ptr = lsi->lsi_lmd->lmd_dev;
379                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
380                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
381                                      niduuid, NULL, NULL, NULL);
382                         if (rc == 0)
383                                 ++i;
384                         /* Stop at the first failover nid */
385                         if (*ptr == ':')
386                                 break;
387                 }
388         }
389         if (i == 0) {
390                 CERROR("No valid MGS nids found.\n");
391                 GOTO(out_free, rc = -EINVAL);
392         }
393         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
394
395         /* Random uuid for MGC allows easier reconnects */
396         OBD_ALLOC_PTR(uuid);
397         if (uuid == NULL)
398                 GOTO(out_free, rc = -ENOMEM);
399
400         ll_generate_random_uuid(uuidc);
401         class_uuid_unparse(uuidc, uuid);
402
403         /* Start the MGC */
404         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
405                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
406                                  niduuid, NULL, NULL);
407         OBD_FREE_PTR(uuid);
408         if (rc)
409                 GOTO(out_free, rc);
410
411         /* Add any failover MGS nids */
412         i = 1;
413         while (ptr && ((*ptr == ':' ||
414                class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
415                 /* New failover node */
416                 sprintf(niduuid, "%s_%x", mgcname, i);
417                 j = 0;
418                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
419                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
420                                      niduuid, NULL, NULL, NULL);
421                         if (rc == 0)
422                                 ++j;
423                         if (*ptr == ':')
424                                 break;
425                 }
426                 if (j > 0) {
427                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
428                                      niduuid, NULL, NULL, NULL);
429                         if (rc == 0)
430                                 ++i;
431                 } else {
432                         /* at ":/fsname" */
433                         break;
434                 }
435         }
436         lsi->lsi_lmd->lmd_mgs_failnodes = i;
437
438         obd = class_name2obd(mgcname);
439         if (!obd) {
440                 CERROR("Can't find mgcobd %s\n", mgcname);
441                 GOTO(out_free, rc = -ENOTCONN);
442         }
443
444         rc = obd_set_info_async(NULL, obd->obd_self_export,
445                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
446                                 strlen(mgssec), mgssec, NULL);
447         if (rc)
448                 GOTO(out_free, rc);
449
450         /* Keep a refcount of servers/clients who started with "mount",
451            so we know when we can get rid of the mgc. */
452         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
453
454         /* We connect to the MGS at setup, and don't disconnect until cleanup */
455         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
456                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
457                                   OBD_CONNECT_LVB_TYPE | OBD_CONNECT_BULK_MBITS;
458
459 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
460         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
461 #endif
462
463         if (lmd_is_client(lsi->lsi_lmd) &&
464             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
465                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
466         data->ocd_version = LUSTRE_VERSION_CODE;
467         rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
468         if (rc) {
469                 CERROR("connect failed %d\n", rc);
470                 GOTO(out, rc);
471         }
472
473         obd->u.cli.cl_mgc_mgsexp = exp;
474
475 out:
476         /* Keep the mgc info in the sb. Note that many lsi's can point
477            to the same mgc.*/
478         lsi->lsi_mgc = obd;
479 out_free:
480         mutex_unlock(&mgc_start_lock);
481
482         if (data)
483                 OBD_FREE_PTR(data);
484         if (mgcname)
485                 OBD_FREE(mgcname, len);
486         if (niduuid)
487                 OBD_FREE(niduuid, len + 2);
488         RETURN(rc);
489 }
490
491 static int lustre_stop_mgc(struct super_block *sb)
492 {
493         struct lustre_sb_info *lsi = s2lsi(sb);
494         struct obd_device *obd;
495         char *niduuid = NULL, *ptr = NULL;
496         int i, rc = 0, len = 0;
497         ENTRY;
498
499         if (!lsi)
500                 RETURN(-ENOENT);
501         obd = lsi->lsi_mgc;
502         if (!obd)
503                 RETURN(-ENOENT);
504         lsi->lsi_mgc = NULL;
505
506         mutex_lock(&mgc_start_lock);
507         LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
508         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
509                 /* This is not fatal, every client that stops
510                    will call in here. */
511                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
512                        atomic_read(&obd->u.cli.cl_mgc_refcount));
513                 GOTO(out, rc = -EBUSY);
514         }
515
516         /* The MGC has no recoverable data in any case.
517          * force shotdown set in umount_begin */
518         obd->obd_no_recov = 1;
519
520         if (obd->u.cli.cl_mgc_mgsexp) {
521                 /* An error is not fatal, if we are unable to send the
522                    disconnect mgs ping evictor cleans up the export */
523                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
524                 if (rc)
525                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
526         }
527
528         /* Save the obdname for cleaning the nid uuids, which are
529            obdname_XX */
530         len = strlen(obd->obd_name) + 6;
531         OBD_ALLOC(niduuid, len);
532         if (niduuid) {
533                 strcpy(niduuid, obd->obd_name);
534                 ptr = niduuid + strlen(niduuid);
535         }
536
537         rc = class_manual_cleanup(obd);
538         if (rc)
539                 GOTO(out, rc);
540
541         /* Clean the nid uuids */
542         if (!niduuid)
543                 GOTO(out, rc = -ENOMEM);
544
545         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
546                 sprintf(ptr, "_%x", i);
547                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
548                              niduuid, NULL, NULL, NULL);
549                 if (rc)
550                         CERROR("del MDC UUID %s failed: rc = %d\n",
551                                niduuid, rc);
552         }
553 out:
554         if (niduuid)
555                 OBD_FREE(niduuid, len);
556
557         /* class_import_put will get rid of the additional connections */
558         mutex_unlock(&mgc_start_lock);
559         RETURN(rc);
560 }
561
562 /***************** lustre superblock **************/
563
564 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
565 {
566         struct lustre_sb_info *lsi;
567         ENTRY;
568
569         OBD_ALLOC_PTR(lsi);
570         if (!lsi)
571                 RETURN(NULL);
572         OBD_ALLOC_PTR(lsi->lsi_lmd);
573         if (!lsi->lsi_lmd) {
574                 OBD_FREE_PTR(lsi);
575                 RETURN(NULL);
576         }
577
578         lsi->lsi_lmd->lmd_exclude_count = 0;
579         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
580         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
581         s2lsi_nocast(sb) = lsi;
582         /* we take 1 extra ref for our setup */
583         atomic_set(&lsi->lsi_mounts, 1);
584
585         /* Default umount style */
586         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
587         INIT_LIST_HEAD(&lsi->lsi_lwp_list);
588         spin_lock_init(&lsi->lsi_lwp_lock);
589
590         RETURN(lsi);
591 }
592
593 static int lustre_free_lsi(struct super_block *sb)
594 {
595         struct lustre_sb_info *lsi = s2lsi(sb);
596         ENTRY;
597
598         LASSERT(lsi != NULL);
599         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
600
601         /* someone didn't call server_put_mount. */
602         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
603
604         if (lsi->lsi_lmd != NULL) {
605                 if (lsi->lsi_lmd->lmd_dev != NULL)
606                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
607                                 strlen(lsi->lsi_lmd->lmd_dev) + 1);
608                 if (lsi->lsi_lmd->lmd_profile != NULL)
609                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
610                                 strlen(lsi->lsi_lmd->lmd_profile) + 1);
611                 if (lsi->lsi_lmd->lmd_fileset != NULL)
612                         OBD_FREE(lsi->lsi_lmd->lmd_fileset,
613                                 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
614                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
615                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
616                                 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
617                 if (lsi->lsi_lmd->lmd_opts != NULL)
618                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
619                                 strlen(lsi->lsi_lmd->lmd_opts) + 1);
620                 if (lsi->lsi_lmd->lmd_exclude_count)
621                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
622                                 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
623                                 lsi->lsi_lmd->lmd_exclude_count);
624                 if (lsi->lsi_lmd->lmd_mgs != NULL)
625                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
626                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
627                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
628                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
629                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
630                 if (lsi->lsi_lmd->lmd_params != NULL)
631                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
632                 if (lsi->lsi_lmd->lmd_nidnet != NULL)
633                         OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
634                                 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
635
636                 OBD_FREE_PTR(lsi->lsi_lmd);
637         }
638
639         LASSERT(lsi->lsi_llsbi == NULL);
640         OBD_FREE_PTR(lsi);
641         s2lsi_nocast(sb) = NULL;
642
643         RETURN(0);
644 }
645
646 /* The lsi has one reference for every server that is using the disk -
647    e.g. MDT, MGS, and potentially MGC */
648 int lustre_put_lsi(struct super_block *sb)
649 {
650         struct lustre_sb_info *lsi = s2lsi(sb);
651         ENTRY;
652
653         LASSERT(lsi != NULL);
654
655         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
656         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
657                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
658                         lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
659                         lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
660                         lsi->lsi_dt_dev = NULL;
661                         obd_disconnect(lsi->lsi_osd_exp);
662                         /* wait till OSD is gone */
663                         obd_zombie_barrier();
664                 }
665                 lustre_free_lsi(sb);
666                 RETURN(1);
667         }
668         RETURN(0);
669 }
670
671 /*** SERVER NAME ***
672  * <FSNAME><SEPARATOR><TYPE><INDEX>
673  * FSNAME is between 1 and 8 characters (inclusive).
674  *      Excluded characters are '/' and ':'
675  * SEPARATOR is either ':' or '-'
676  * TYPE: "OST", "MDT", etc.
677  * INDEX: Hex representation of the index
678  */
679
680 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
681  * @param [in] svname server name including type and index
682  * @param [out] fsname Buffer to copy filesystem name prefix into.
683  *  Must have at least 'strlen(fsname) + 1' chars.
684  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
685  * rc < 0  on error
686  */
687 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
688 {
689         const char *dash;
690
691         dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
692         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
693                 ;
694         if (dash == svname)
695                 return -EINVAL;
696
697         if (fsname != NULL) {
698                 strncpy(fsname, svname, dash - svname);
699                 fsname[dash - svname] = '\0';
700         }
701
702         if (endptr != NULL)
703                 *endptr = dash;
704
705         return 0;
706 }
707 EXPORT_SYMBOL(server_name2fsname);
708
709 /**
710  * Get service name (svname) from string
711  * rc < 0 on error
712  * if endptr isn't NULL it is set to end of fsname *
713  */
714 int server_name2svname(const char *label, char *svname, const char **endptr,
715                        size_t svsize)
716 {
717         int rc;
718         const char *dash;
719
720         /* We use server_name2fsname() just for parsing */
721         rc = server_name2fsname(label, NULL, &dash);
722         if (rc != 0)
723                 return rc;
724
725         if (endptr != NULL)
726                 *endptr = dash;
727
728         if (strlcpy(svname, dash + 1, svsize) >= svsize)
729                 return -E2BIG;
730
731         return 0;
732 }
733 EXPORT_SYMBOL(server_name2svname);
734
735 /**
736  * check server name is OST.
737  **/
738 int server_name_is_ost(const char *svname)
739 {
740         const char *dash;
741         int rc;
742
743         /* We use server_name2fsname() just for parsing */
744         rc = server_name2fsname(svname, NULL, &dash);
745         if (rc != 0)
746                 return rc;
747
748         dash++;
749
750         if (strncmp(dash, "OST", 3) == 0)
751                 return 1;
752         return 0;
753 }
754 EXPORT_SYMBOL(server_name_is_ost);
755
756 /**
757  * Get the index from the target name MDTXXXX/OSTXXXX
758  * rc = server type, or rc < 0  on error
759  **/
760 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
761 {
762         const char *dash = tgtname;
763         unsigned long index;
764         int rc;
765
766         if (strncmp(dash, "MDT", 3) == 0)
767                 rc = LDD_F_SV_TYPE_MDT;
768         else if (strncmp(dash, "OST", 3) == 0)
769                 rc = LDD_F_SV_TYPE_OST;
770         else
771                 return -EINVAL;
772
773         dash += 3;
774
775         if (strncmp(dash, "all", 3) == 0) {
776                 if (endptr != NULL)
777                         *endptr = dash + 3;
778                 return rc | LDD_F_SV_ALL;
779         }
780
781         index = simple_strtoul(dash, (char **)endptr, 16);
782         if (idx != NULL)
783                 *idx = index;
784         return rc;
785 }
786 EXPORT_SYMBOL(target_name2index);
787
788 /* Get the index from the obd name.
789    rc = server type, or
790    rc < 0  on error
791    if endptr isn't NULL it is set to end of name */
792 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
793 {
794         const char *dash;
795         int rc;
796
797         /* We use server_name2fsname() just for parsing */
798         rc = server_name2fsname(svname, NULL, &dash);
799         if (rc != 0)
800                 return rc;
801
802         dash++;
803         rc = target_name2index(dash, idx, endptr);
804         if (rc < 0)
805                 return rc;
806
807         /* Account for -mdc after index that is possible when specifying mdt */
808         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
809                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
810                 *endptr += sizeof(LUSTRE_MDC_NAME);
811
812         return rc;
813 }
814 EXPORT_SYMBOL(server_name2index);
815
816 /*************** mount common betweeen server and client ***************/
817
818 /* Common umount */
819 int lustre_common_put_super(struct super_block *sb)
820 {
821         int rc;
822         ENTRY;
823
824         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
825
826         /* Drop a ref to the MGC */
827         rc = lustre_stop_mgc(sb);
828         if (rc && (rc != -ENOENT)) {
829                 if (rc != -EBUSY) {
830                         CERROR("Can't stop MGC: %d\n", rc);
831                         RETURN(rc);
832                 }
833                 /* BUSY just means that there's some other obd that
834                    needs the mgc.  Let him clean it up. */
835                 CDEBUG(D_MOUNT, "MGC still in use\n");
836         }
837         /* Drop a ref to the mounted disk */
838         lustre_put_lsi(sb);
839
840         RETURN(rc);
841 }
842 EXPORT_SYMBOL(lustre_common_put_super);
843
844 static void lmd_print(struct lustre_mount_data *lmd)
845 {
846         int i;
847
848         PRINT_CMD(D_MOUNT, "  mount data:\n");
849         if (lmd_is_client(lmd))
850                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
851         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
852         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
853
854         if (lmd->lmd_opts)
855                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
856
857         if (lmd->lmd_recovery_time_soft)
858                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
859                           lmd->lmd_recovery_time_soft);
860
861         if (lmd->lmd_recovery_time_hard)
862                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
863                           lmd->lmd_recovery_time_hard);
864
865         for (i = 0; i < lmd->lmd_exclude_count; i++) {
866                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
867                           lmd->lmd_exclude[i]);
868         }
869 }
870
871 /* Is this server on the exclusion list */
872 int lustre_check_exclusion(struct super_block *sb, char *svname)
873 {
874         struct lustre_sb_info *lsi = s2lsi(sb);
875         struct lustre_mount_data *lmd = lsi->lsi_lmd;
876         __u32 index;
877         int i, rc;
878         ENTRY;
879
880         rc = server_name2index(svname, &index, NULL);
881         if (rc != LDD_F_SV_TYPE_OST)
882                 /* Only exclude OSTs */
883                 RETURN(0);
884
885         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
886                index, lmd->lmd_exclude_count, lmd->lmd_dev);
887
888         for(i = 0; i < lmd->lmd_exclude_count; i++) {
889                 if (index == lmd->lmd_exclude[i]) {
890                         CWARN("Excluding %s (on exclusion list)\n", svname);
891                         RETURN(1);
892                 }
893         }
894         RETURN(0);
895 }
896
897 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
898 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
899 {
900         const char *s1 = ptr, *s2;
901         __u32 *exclude_list;
902         __u32 index = 0;
903         int rc = 0, devmax;
904         ENTRY;
905
906         /* The shortest an ost name can be is 8 chars: -OST0000.
907            We don't actually know the fsname at this time, so in fact
908            a user could specify any fsname. */
909         devmax = strlen(ptr) / 8 + 1;
910
911         /* temp storage until we figure out how many we have */
912         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
913         if (!exclude_list)
914                 RETURN(-ENOMEM);
915
916         /* we enter this fn pointing at the '=' */
917         while (*s1 && *s1 != ' ' && *s1 != ',') {
918                 s1++;
919                 rc = server_name2index(s1, &index, &s2);
920                 if (rc < 0) {
921                         CERROR("Can't parse server name '%s': rc = %d\n",
922                                s1, rc);
923                         break;
924                 }
925                 if (rc == LDD_F_SV_TYPE_OST)
926                         exclude_list[lmd->lmd_exclude_count++] = index;
927                 else
928                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
929                                (uint)(s2-s1), s1, rc);
930                 s1 = s2;
931                 /* now we are pointing at ':' (next exclude)
932                    or ',' (end of excludes) */
933                 if (lmd->lmd_exclude_count >= devmax)
934                         break;
935         }
936         if (rc >= 0) /* non-err */
937                 rc = 0;
938
939         if (lmd->lmd_exclude_count) {
940                 /* permanent, freed in lustre_free_lsi */
941                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
942                           lmd->lmd_exclude_count);
943                 if (lmd->lmd_exclude) {
944                         memcpy(lmd->lmd_exclude, exclude_list,
945                                sizeof(index) * lmd->lmd_exclude_count);
946                 } else {
947                         rc = -ENOMEM;
948                         lmd->lmd_exclude_count = 0;
949                 }
950         }
951         OBD_FREE(exclude_list, sizeof(index) * devmax);
952         RETURN(rc);
953 }
954
955 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
956 {
957         char   *tail;
958         int     length;
959
960         if (lmd->lmd_mgssec != NULL) {
961                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
962                 lmd->lmd_mgssec = NULL;
963         }
964
965         tail = strchr(ptr, ',');
966         if (tail == NULL)
967                 length = strlen(ptr);
968         else
969                 length = tail - ptr;
970
971         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
972         if (lmd->lmd_mgssec == NULL)
973                 return -ENOMEM;
974
975         memcpy(lmd->lmd_mgssec, ptr, length);
976         lmd->lmd_mgssec[length] = '\0';
977         return 0;
978 }
979
980 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
981 {
982         char   *tail;
983         int     length;
984
985         if (lmd->lmd_nidnet != NULL) {
986                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
987                 lmd->lmd_nidnet = NULL;
988         }
989
990         tail = strchr(ptr, ',');
991         if (tail == NULL)
992                 length = strlen(ptr);
993         else
994                 length = tail - ptr;
995
996         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
997         if (lmd->lmd_nidnet == NULL)
998                 return -ENOMEM;
999
1000         memcpy(lmd->lmd_nidnet, ptr, length);
1001         lmd->lmd_nidnet[length] = '\0';
1002         return 0;
1003 }
1004
1005 static int lmd_parse_string(char **handle, char *ptr)
1006 {
1007         char   *tail;
1008         int     length;
1009
1010         if ((handle == NULL) || (ptr == NULL))
1011                 return -EINVAL;
1012
1013         if (*handle != NULL) {
1014                 OBD_FREE(*handle, strlen(*handle) + 1);
1015                 *handle = NULL;
1016         }
1017
1018         tail = strchr(ptr, ',');
1019         if (tail == NULL)
1020                 length = strlen(ptr);
1021         else
1022                 length = tail - ptr;
1023
1024         OBD_ALLOC(*handle, length + 1);
1025         if (*handle == NULL)
1026                 return -ENOMEM;
1027
1028         memcpy(*handle, ptr, length);
1029         (*handle)[length] = '\0';
1030
1031         return 0;
1032 }
1033
1034 /* Collect multiple values for mgsnid specifiers */
1035 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1036 {
1037         lnet_nid_t nid;
1038         char *tail = *ptr;
1039         char *mgsnid;
1040         int   length;
1041         int   oldlen = 0;
1042
1043         /* Find end of nidlist */
1044         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1045         length = tail - *ptr;
1046         if (length == 0) {
1047                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1048                 return -EINVAL;
1049         }
1050
1051         if (lmd->lmd_mgs != NULL)
1052                 oldlen = strlen(lmd->lmd_mgs) + 1;
1053
1054         OBD_ALLOC(mgsnid, oldlen + length + 1);
1055         if (mgsnid == NULL)
1056                 return -ENOMEM;
1057
1058         if (lmd->lmd_mgs != NULL) {
1059                 /* Multiple mgsnid= are taken to mean failover locations */
1060                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1061                 mgsnid[oldlen - 1] = ':';
1062                 OBD_FREE(lmd->lmd_mgs, oldlen);
1063         }
1064         memcpy(mgsnid + oldlen, *ptr, length);
1065         mgsnid[oldlen + length] = '\0';
1066         lmd->lmd_mgs = mgsnid;
1067         *ptr = tail;
1068
1069         return 0;
1070 }
1071
1072 /**
1073  * Find the first delimiter (comma or colon) from the specified \a buf and
1074  * make \a *endh point to the string starting with the delimiter. The commas
1075  * in expression list [...] will be skipped.
1076  *
1077  * \param[in] buf       a delimiter-separated string
1078  * \param[in] endh      a pointer to a pointer that will point to the string
1079  *                      starting with the delimiter
1080  *
1081  * \retval 0            if delimiter is found
1082  * \retval 1            if delimiter is not found
1083  */
1084 static int lmd_find_delimiter(char *buf, char **endh)
1085 {
1086         char *c = buf;
1087         int   skip = 0;
1088
1089         if (buf == NULL)
1090                 return 1;
1091
1092         while (*c != '\0') {
1093                 if (*c == '[')
1094                         skip++;
1095                 else if (*c == ']')
1096                         skip--;
1097
1098                 if ((*c == ',' || *c == ':') && skip == 0) {
1099                         if (endh != NULL)
1100                                 *endh = c;
1101                         return 0;
1102                 }
1103
1104                 c++;
1105         }
1106
1107         return 1;
1108 }
1109
1110 /**
1111  * Find the first valid string delimited by comma or colon from the specified
1112  * \a buf and parse it to see whether it's a valid nid list. If yes, \a *endh
1113  * will point to the next string starting with the delimiter.
1114  *
1115  * \param[in] buf       a delimiter-separated string
1116  * \param[in] endh      a pointer to a pointer that will point to the string
1117  *                      starting with the delimiter
1118  *
1119  * \retval 0            if the string is a valid nid list
1120  * \retval 1            if the string is not a valid nid list
1121  */
1122 static int lmd_parse_nidlist(char *buf, char **endh)
1123 {
1124         struct list_head nidlist;
1125         char            *endp = buf;
1126         char             tmp;
1127         int              rc = 0;
1128
1129         if (buf == NULL)
1130                 return 1;
1131         while (*buf == ',' || *buf == ':')
1132                 buf++;
1133         if (*buf == ' ' || *buf == '/' || *buf == '\0')
1134                 return 1;
1135
1136         if (lmd_find_delimiter(buf, &endp) != 0)
1137                 endp = buf + strlen(buf);
1138
1139         tmp = *endp;
1140         *endp = '\0';
1141
1142         INIT_LIST_HEAD(&nidlist);
1143         if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1144                 rc = 1;
1145         cfs_free_nidlist(&nidlist);
1146
1147         *endp = tmp;
1148         if (rc != 0)
1149                 return rc;
1150         if (endh != NULL)
1151                 *endh = endp;
1152         return 0;
1153 }
1154
1155 /** Parse mount line options
1156  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1157  * dev is passed as device=uml1:/lustre by mount.lustre
1158  */
1159 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1160 {
1161         char *s1, *s2, *s3, *devname = NULL;
1162         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1163         int rc = 0;
1164         ENTRY;
1165
1166         LASSERT(lmd);
1167         if (!options) {
1168                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1169                                    "/sbin/mount.lustre is installed.\n");
1170                 RETURN(-EINVAL);
1171         }
1172
1173         /* Options should be a string - try to detect old lmd data */
1174         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1175                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1176                                    "/sbin/mount.lustre.  Please install "
1177                                    "version %s\n", LUSTRE_VERSION_STRING);
1178                 RETURN(-EINVAL);
1179         }
1180         lmd->lmd_magic = LMD_MAGIC;
1181
1182         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1183         if (lmd->lmd_params == NULL)
1184                 RETURN(-ENOMEM);
1185         lmd->lmd_params[0] = '\0';
1186
1187         /* Set default flags here */
1188
1189         s1 = options;
1190         while (*s1) {
1191                 int clear = 0;
1192                 int time_min = OBD_RECOVERY_TIME_MIN;
1193
1194                 /* Skip whitespace and extra commas */
1195                 while (*s1 == ' ' || *s1 == ',')
1196                         s1++;
1197                 s3 = s1;
1198
1199                 /* Client options are parsed in ll_options: eg. flock,
1200                    user_xattr, acl */
1201
1202                 /* Parse non-ldiskfs options here. Rather than modifying
1203                    ldiskfs, we just zero these out here */
1204                 if (strncmp(s1, "abort_recov", 11) == 0) {
1205                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1206                         clear++;
1207                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1208                         lmd->lmd_recovery_time_soft =
1209                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1210                                       time_min);
1211                         clear++;
1212                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1213                         lmd->lmd_recovery_time_hard =
1214                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1215                                       time_min);
1216                         clear++;
1217                 } else if (strncmp(s1, "noir", 4) == 0) {
1218                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1219                         clear++;
1220                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1221                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1222                         clear++;
1223                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1224                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1225                         clear++;
1226                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1227                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1228                         clear++;
1229                 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1230                         lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1231                         clear++;
1232                 } else if (strncmp(s1, PARAM_MGSNODE,
1233                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1234                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1235                         /* Assume the next mount opt is the first
1236                            invalid nid we get to. */
1237                         rc = lmd_parse_mgs(lmd, &s2);
1238                         if (rc)
1239                                 goto invalid;
1240                         s3 = s2;
1241                         clear++;
1242                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1243                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1244                         clear++;
1245                 } else if (strncmp(s1, "update", 6) == 0) {
1246                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1247                         clear++;
1248                 } else if (strncmp(s1, "virgin", 6) == 0) {
1249                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1250                         clear++;
1251                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1252                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1253                         clear++;
1254                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1255                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1256                         if (rc)
1257                                 goto invalid;
1258                         clear++;
1259                         /* ost exclusion list */
1260                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1261                         rc = lmd_make_exclusion(lmd, s1 + 7);
1262                         if (rc)
1263                                 goto invalid;
1264                         clear++;
1265                 } else if (strncmp(s1, "mgs", 3) == 0) {
1266                         /* We are an MGS */
1267                         lmd->lmd_flags |= LMD_FLG_MGS;
1268                         clear++;
1269                 } else if (strncmp(s1, "svname=", 7) == 0) {
1270                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1271                         if (rc)
1272                                 goto invalid;
1273                         clear++;
1274                 } else if (strncmp(s1, "param=", 6) == 0) {
1275                         size_t length, params_length;
1276                         char  *tail = s1;
1277                         if (lmd_find_delimiter(s1 + 6, &tail) != 0)
1278                                 length = strlen(s1);
1279                         else {
1280                                 char *param_str = tail + 1;
1281                                 int   supplementary = 1;
1282                                 while (lmd_parse_nidlist(param_str,
1283                                                          &param_str) == 0) {
1284                                         supplementary = 0;
1285                                 }
1286                                 length = param_str - s1 - supplementary;
1287                         }
1288                         length -= 6;
1289                         params_length = strlen(lmd->lmd_params);
1290                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1291                                 RETURN(-E2BIG);
1292                         strncat(lmd->lmd_params, s1 + 6, length);
1293                         lmd->lmd_params[params_length + length] = '\0';
1294                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1295                         s3 = s1 + 6 + length;
1296                         clear++;
1297                 } else if (strncmp(s1, "osd=", 4) == 0) {
1298                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1299                         if (rc)
1300                                 goto invalid;
1301                         clear++;
1302                 }
1303                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1304                    end of the options. */
1305                 else if (strncmp(s1, "device=", 7) == 0) {
1306                         devname = s1 + 7;
1307                         /* terminate options right before device.  device
1308                            must be the last one. */
1309                         *s1 = '\0';
1310                         break;
1311                 } else if (strncmp(s1, "network=", 8) == 0) {
1312                         rc = lmd_parse_network(lmd, s1 + 8);
1313                         if (rc)
1314                                 goto invalid;
1315                         clear++;
1316                 }
1317
1318                 /* Find next opt */
1319                 s2 = strchr(s3, ',');
1320                 if (s2 == NULL) {
1321                         if (clear)
1322                                 *s1 = '\0';
1323                         break;
1324                 }
1325                 s2++;
1326                 if (clear)
1327                         memmove(s1, s2, strlen(s2) + 1);
1328                 else
1329                         s1 = s2;
1330         }
1331
1332         if (!devname) {
1333                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1334                                    "(need mount option 'device=...')\n");
1335                 goto invalid;
1336         }
1337
1338         s1 = strstr(devname, ":/");
1339         if (s1) {
1340                 ++s1;
1341                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1342                 /* Remove leading /s from fsname */
1343                 while (*++s1 == '/')
1344                         ;
1345                 s2 = s1;
1346                 while (*s2 != '/' && *s2 != '\0')
1347                         s2++;
1348                 /* Freed in lustre_free_lsi */
1349                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1350                 if (!lmd->lmd_profile)
1351                         RETURN(-ENOMEM);
1352
1353                 strncat(lmd->lmd_profile, s1, s2 - s1);
1354                 strncat(lmd->lmd_profile, "-client", 7);
1355
1356                 s1 = s2;
1357                 s2 = s1 + strlen(s1) - 1;
1358                 /* Remove padding /s from fileset */
1359                 while (*s2 == '/')
1360                         s2--;
1361                 if (s2 > s1) {
1362                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1363                         if (lmd->lmd_fileset == NULL) {
1364                                 OBD_FREE(lmd->lmd_profile,
1365                                          strlen(lmd->lmd_profile) + 1);
1366                                 RETURN(-ENOMEM);
1367                         }
1368                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1369                 }
1370         } else {
1371                 /* server mount */
1372                 if (lmd->lmd_nidnet != NULL) {
1373                         /* 'network=' mount option forbidden for server */
1374                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1375                         lmd->lmd_nidnet = NULL;
1376                         rc = -EINVAL;
1377                         CERROR("%s: option 'network=' not allowed for Lustre "
1378                                "servers: rc = %d\n", devname, rc);
1379                         RETURN(rc);
1380                 }
1381         }
1382
1383         /* Freed in lustre_free_lsi */
1384         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1385         if (!lmd->lmd_dev)
1386                 RETURN(-ENOMEM);
1387         strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1388
1389         /* Save mount options */
1390         s1 = options + strlen(options) - 1;
1391         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1392                 *s1-- = 0;
1393         if (*options != 0) {
1394                 /* Freed in lustre_free_lsi */
1395                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1396                 if (!lmd->lmd_opts)
1397                         RETURN(-ENOMEM);
1398                 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1399         }
1400
1401         lmd_print(lmd);
1402         lmd->lmd_magic = LMD_MAGIC;
1403
1404         RETURN(rc);
1405
1406 invalid:
1407         CERROR("Bad mount options %s\n", options);
1408         RETURN(-EINVAL);
1409 }
1410
1411 struct lustre_mount_data2 {
1412         void *lmd2_data;
1413         struct vfsmount *lmd2_mnt;
1414 };
1415
1416 /** This is the entry point for the mount call into Lustre.
1417  * This is called when a server or client is mounted,
1418  * and this is where we start setting things up.
1419  * @param data Mount options (e.g. -o flock,abort_recov)
1420  */
1421 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1422 {
1423         struct lustre_mount_data *lmd;
1424         struct lustre_mount_data2 *lmd2 = data;
1425         struct lustre_sb_info *lsi;
1426         int rc;
1427         ENTRY;
1428
1429         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1430
1431         lsi = lustre_init_lsi(sb);
1432         if (!lsi)
1433                 RETURN(-ENOMEM);
1434         lmd = lsi->lsi_lmd;
1435
1436         /*
1437          * Disable lockdep during mount, because mount locking patterns are
1438          * `special'.
1439          */
1440         lockdep_off();
1441
1442         /*
1443          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1444          */
1445         obd_zombie_barrier();
1446
1447         /* Figure out the lmd from the mount options */
1448         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1449                 lustre_put_lsi(sb);
1450                 GOTO(out, rc = -EINVAL);
1451         }
1452
1453         if (lmd_is_client(lmd)) {
1454                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1455                 if (client_fill_super == NULL)
1456                         request_module("lustre");
1457                 if (client_fill_super == NULL) {
1458                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1459                                            "client mount! Is the 'lustre' "
1460                                            "module loaded?\n");
1461                         lustre_put_lsi(sb);
1462                         rc = -ENODEV;
1463                 } else {
1464                         rc = lustre_start_mgc(sb);
1465                         if (rc) {
1466                                 lustre_common_put_super(sb);
1467                                 GOTO(out, rc);
1468                         }
1469                         /* Connect and start */
1470                         /* (should always be ll_fill_super) */
1471                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1472                         /* c_f_s will call lustre_common_put_super on failure */
1473                 }
1474         } else {
1475 #ifdef HAVE_SERVER_SUPPORT
1476                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1477                 rc = server_fill_super(sb);
1478                 /* s_f_s calls lustre_start_mgc after the mount because we need
1479                    the MGS nids which are stored on disk.  Plus, we may
1480                    need to start the MGS first. */
1481                 /* s_f_s will call server_put_super on failure */
1482 #else
1483                 CERROR("This is client-side-only module, "
1484                        "cannot handle server mount.\n");
1485                 rc = -EINVAL;
1486 #endif
1487         }
1488
1489         /* If error happens in fill_super() call, @lsi will be killed there.
1490          * This is why we do not put it here. */
1491         GOTO(out, rc);
1492 out:
1493         if (rc) {
1494                 CERROR("Unable to mount %s (%d)\n",
1495                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1496         } else {
1497                 CDEBUG(D_SUPER, "Mount %s complete\n",
1498                        lmd->lmd_dev);
1499         }
1500         lockdep_on();
1501         return rc;
1502 }
1503
1504
1505 /* We can't call ll_fill_super by name because it lives in a module that
1506    must be loaded after this one. */
1507 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1508                                                   struct vfsmount *mnt))
1509 {
1510         client_fill_super = cfs;
1511 }
1512 EXPORT_SYMBOL(lustre_register_client_fill_super);
1513
1514 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1515 {
1516         kill_super_cb = cfs;
1517 }
1518 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1519
1520 /***************** FS registration ******************/
1521 #ifdef HAVE_FSTYPE_MOUNT
1522 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1523                                    const char *devname, void *data)
1524 {
1525         struct lustre_mount_data2 lmd2 = {
1526                 .lmd2_data = data,
1527         };
1528
1529         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1530 }
1531 #else
1532 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1533                          const char *devname, void *data, struct vfsmount *mnt)
1534 {
1535         struct lustre_mount_data2 lmd2 = {
1536                 .lmd2_data = data,
1537                 .lmd2_mnt = mnt,
1538         };
1539
1540         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1541 }
1542 #endif
1543
1544 static void lustre_kill_super(struct super_block *sb)
1545 {
1546         struct lustre_sb_info *lsi = s2lsi(sb);
1547
1548         if (kill_super_cb && lsi && !IS_SERVER(lsi))
1549                 (*kill_super_cb)(sb);
1550
1551         kill_anon_super(sb);
1552 }
1553
1554 /** Register the "lustre" fs type
1555  */
1556 static struct file_system_type lustre_fs_type = {
1557         .owner        = THIS_MODULE,
1558         .name         = "lustre",
1559 #ifdef HAVE_FSTYPE_MOUNT
1560         .mount        = lustre_mount,
1561 #else
1562         .get_sb       = lustre_get_sb,
1563 #endif
1564         .kill_sb      = lustre_kill_super,
1565         .fs_flags     = FS_REQUIRES_DEV | FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1566 };
1567 MODULE_ALIAS_FS("lustre");
1568
1569 int lustre_register_fs(void)
1570 {
1571         return register_filesystem(&lustre_fs_type);
1572 }
1573
1574 int lustre_unregister_fs(void)
1575 {
1576         return unregister_filesystem(&lustre_fs_type);
1577 }