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