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