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