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