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