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