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