Whamcloud - gitweb
a84169263c02b23ee36774c17920411a4f39b6c9
[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         if (strlcpy(svname, dash + 1, svsize) >= svsize)
852                 return -E2BIG;
853
854         return 0;
855 }
856 EXPORT_SYMBOL(server_name2svname);
857 #endif /* HAVE_SERVER_SUPPORT */
858
859 /**
860  * check server name is OST.
861  **/
862 int server_name_is_ost(const char *svname)
863 {
864         const char *dash;
865         int rc;
866
867         /* We use server_name2fsname() just for parsing */
868         rc = server_name2fsname(svname, NULL, &dash);
869         if (rc != 0)
870                 return rc;
871
872         dash++;
873
874         if (strncmp(dash, "OST", 3) == 0)
875                 return 1;
876         return 0;
877 }
878 EXPORT_SYMBOL(server_name_is_ost);
879
880 /**
881  * Get the index from the target name MDTXXXX/OSTXXXX
882  * rc = server type, or rc < 0  on error
883  **/
884 int target_name2index(const char *tgtname, u32 *idx, const char **endptr)
885 {
886         const char *dash = tgtname;
887         int type, len, rc;
888         u16 index;
889
890         if (strncmp(dash, "MDT", 3) == 0)
891                 type = LDD_F_SV_TYPE_MDT;
892         else if (strncmp(dash, "OST", 3) == 0)
893                 type = LDD_F_SV_TYPE_OST;
894         else
895                 return -EINVAL;
896
897         dash += 3;
898
899         if (strncmp(dash, "all", 3) == 0) {
900                 if (endptr != NULL)
901                         *endptr = dash + 3;
902                 return type | LDD_F_SV_ALL;
903         }
904
905         len = strspn(dash, "0123456789ABCDEFabcdef");
906         if (len > 4)
907                 return -ERANGE;
908
909         if (strlen(dash) != len) {
910                 char num[5];
911
912                 num[4] = '\0';
913                 memcpy(num, dash, sizeof(num) - 1);
914                 rc = kstrtou16(num, 16, &index);
915                 if (rc < 0)
916                         return rc;
917         } else {
918                 rc = kstrtou16(dash, 16, &index);
919                 if (rc < 0)
920                         return rc;
921         }
922
923         if (idx)
924                 *idx = index;
925
926         if (endptr)
927                 *endptr = dash  + len;
928
929         return type;
930 }
931 EXPORT_SYMBOL(target_name2index);
932
933 /*
934  * Get the index from the OBD name.
935  * rc = server type, or
936  * rc < 0  on error
937  * if endptr isn't NULL it is set to end of name
938  */
939 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
940 {
941         const char *dash;
942         int rc;
943
944         /* We use server_name2fsname() just for parsing */
945         rc = server_name2fsname(svname, NULL, &dash);
946         if (rc != 0)
947                 return rc;
948
949         dash++;
950         rc = target_name2index(dash, idx, endptr);
951         if (rc < 0)
952                 return rc;
953
954         /* Account for -mdc after index that is possible when specifying mdt */
955         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
956                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
957                 *endptr += sizeof(LUSTRE_MDC_NAME);
958
959         return rc;
960 }
961 EXPORT_SYMBOL(server_name2index);
962
963 /*************** mount common betweeen server and client ***************/
964
965 /* Common umount */
966 int lustre_common_put_super(struct super_block *sb)
967 {
968         int rc;
969
970         ENTRY;
971
972         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
973
974         /* Drop a ref to the MGC */
975         rc = lustre_stop_mgc(sb);
976         if (rc && (rc != -ENOENT)) {
977                 if (rc != -EBUSY) {
978                         CERROR("Can't stop MGC: %d\n", rc);
979                         RETURN(rc);
980                 }
981                 /*
982                  * BUSY just means that there's some other OBD that
983                  * needs the mgc.  Let him clean it up.
984                  */
985                 CDEBUG(D_MOUNT, "MGC still in use\n");
986         }
987         /* Drop a ref to the mounted disk */
988         lustre_put_lsi(sb);
989
990         RETURN(rc);
991 }
992 EXPORT_SYMBOL(lustre_common_put_super);
993
994 static void lmd_print(struct lustre_mount_data *lmd)
995 {
996         int i;
997
998         PRINT_CMD(D_MOUNT, "  mount data:\n");
999         if (lmd_is_client(lmd))
1000                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
1001         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
1002
1003         if (lmd->lmd_opts)
1004                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
1005
1006         if (lmd->lmd_recovery_time_soft)
1007                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
1008                           lmd->lmd_recovery_time_soft);
1009
1010         if (lmd->lmd_recovery_time_hard)
1011                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
1012                           lmd->lmd_recovery_time_hard);
1013
1014         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1015                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
1016                           lmd->lmd_exclude[i]);
1017         }
1018 }
1019
1020 /* Is this server on the exclusion list */
1021 int lustre_check_exclusion(struct super_block *sb, char *svname)
1022 {
1023         struct lustre_sb_info *lsi = s2lsi(sb);
1024         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1025         __u32 index;
1026         int i, rc;
1027
1028         ENTRY;
1029
1030         rc = server_name2index(svname, &index, NULL);
1031         if (rc != LDD_F_SV_TYPE_OST)
1032                 /* Only exclude OSTs */
1033                 RETURN(0);
1034
1035         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1036                index, lmd->lmd_exclude_count, lmd->lmd_dev);
1037
1038         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1039                 if (index == lmd->lmd_exclude[i]) {
1040                         CWARN("Excluding %s (on exclusion list)\n", svname);
1041                         RETURN(1);
1042                 }
1043         }
1044         RETURN(0);
1045 }
1046
1047 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1048 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1049 {
1050         const char *s1 = ptr, *s2;
1051         __u32 *exclude_list;
1052         __u32 index = 0;
1053         int rc = 0, devmax;
1054
1055         ENTRY;
1056
1057         /*
1058          * The shortest an ost name can be is 8 chars: -OST0000.
1059          * We don't actually know the fsname at this time, so in fact
1060          * a user could specify any fsname.
1061          */
1062         devmax = strlen(ptr) / 8 + 1;
1063
1064         /* temp storage until we figure out how many we have */
1065         OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
1066         if (!exclude_list)
1067                 RETURN(-ENOMEM);
1068
1069         /* we enter this fn pointing at the '=' */
1070         while (*s1 && *s1 != ' ' && *s1 != ',') {
1071                 s1++;
1072                 rc = server_name2index(s1, &index, &s2);
1073                 if (rc < 0) {
1074                         CERROR("Can't parse server name '%s': rc = %d\n",
1075                                s1, rc);
1076                         break;
1077                 }
1078                 if (rc == LDD_F_SV_TYPE_OST)
1079                         exclude_list[lmd->lmd_exclude_count++] = index;
1080                 else
1081                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1082                                (uint)(s2-s1), s1, rc);
1083                 s1 = s2;
1084                 /*
1085                  * now we are pointing at ':' (next exclude)
1086                  * or ',' (end of excludes)
1087                  */
1088                 if (lmd->lmd_exclude_count >= devmax)
1089                         break;
1090         }
1091         if (rc >= 0) /* non-err */
1092                 rc = 0;
1093
1094         if (lmd->lmd_exclude_count) {
1095                 /* permanent, freed in lustre_free_lsi */
1096                 OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
1097                                     lmd->lmd_exclude_count);
1098                 if (lmd->lmd_exclude) {
1099                         memcpy(lmd->lmd_exclude, exclude_list,
1100                                sizeof(index) * lmd->lmd_exclude_count);
1101                 } else {
1102                         rc = -ENOMEM;
1103                         lmd->lmd_exclude_count = 0;
1104                 }
1105         }
1106         OBD_FREE_PTR_ARRAY(exclude_list, devmax);
1107         RETURN(rc);
1108 }
1109
1110 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1111 {
1112         int length = strlen(ptr);
1113
1114         if (lmd->lmd_mgssec != NULL) {
1115                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1116                 lmd->lmd_mgssec = NULL;
1117         }
1118
1119         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1120         if (lmd->lmd_mgssec == NULL)
1121                 return -ENOMEM;
1122
1123         memcpy(lmd->lmd_mgssec, ptr, length);
1124         lmd->lmd_mgssec[length] = '\0';
1125         return 0;
1126 }
1127
1128 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1129 {
1130         int length = strlen(ptr);
1131
1132         if (lmd->lmd_nidnet != NULL) {
1133                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1134                 lmd->lmd_nidnet = NULL;
1135         }
1136
1137         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1138         if (lmd->lmd_nidnet == NULL)
1139                 return -ENOMEM;
1140
1141         memcpy(lmd->lmd_nidnet, ptr, length);
1142         lmd->lmd_nidnet[length] = '\0';
1143         return 0;
1144 }
1145
1146 static int lmd_parse_string(char **handle, char *ptr)
1147 {
1148         if (!handle || !ptr)
1149                 return -EINVAL;
1150
1151         OBD_FREE(*handle, strlen(*handle) + 1);
1152         *handle = NULL;
1153
1154         *handle = kstrdup(ptr, GFP_NOFS);
1155         if (!*handle)
1156                 return -ENOMEM;
1157
1158         OBD_ALLOC_POST(*handle, strlen(ptr) + 1, "kmalloced");
1159
1160         return 0;
1161 }
1162
1163 /* Collect multiple values for mgsnid specifiers */
1164 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char *ptr, char **tail)
1165 {
1166         int length = strlen(ptr);
1167         struct lnet_nid nid;
1168         char *next = *tail;
1169         char *mgsnid;
1170         int oldlen = 0;
1171
1172         /* Find end of NID-list */
1173         while (class_parse_nid_quiet(*tail, &nid, tail) == 0)
1174                 ; /* do nothing */
1175
1176         if (next && next != *tail)
1177                 length += *tail - next + 1;
1178         if (length == 0) {
1179                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", ptr);
1180                 return -EINVAL;
1181         }
1182
1183         if (lmd->lmd_mgs != NULL)
1184                 oldlen = strlen(lmd->lmd_mgs) + 1;
1185
1186         OBD_ALLOC(mgsnid, oldlen + length + 1);
1187         if (mgsnid == NULL)
1188                 return -ENOMEM;
1189
1190         if (lmd->lmd_mgs != NULL) {
1191                 /* Multiple mgsnid= are taken to mean failover locations */
1192                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1193                 mgsnid[oldlen - 1] = ':';
1194                 OBD_FREE(lmd->lmd_mgs, oldlen);
1195         }
1196
1197         if (next && next != *tail)
1198                 snprintf(mgsnid + oldlen, length + 1, "%s,%.*s", ptr,
1199                          (int)(*tail - next + 1), next);
1200         else
1201                 snprintf(mgsnid + oldlen, length + 1, "%s", ptr);
1202         lmd->lmd_mgs = mgsnid;
1203
1204         return 0;
1205 }
1206
1207 enum lmd_mnt_flags {
1208         LMD_OPT_RECOVERY_TIME_SOFT      = LMD_FLG_NUM_FLAGS + 1,
1209         LMD_OPT_RECOVERY_TIME_HARD,
1210         LMD_OPT_MGSNODE,
1211         LMD_OPT_MGSSEC,
1212         LMD_OPT_EXCLUDE,
1213         LMD_OPT_SVNAME,
1214         LMD_OPT_PARAM,
1215         LMD_OPT_OSD,
1216         LMD_OPT_NETWORK,
1217         LMD_OPT_DEVICE,
1218         LMD_NUM_MOUNT_OPT
1219 };
1220
1221 static const match_table_t lmd_flags_table = {
1222         {LMD_FLG_SKIP_LFSCK,            "skip_lfsck"},
1223         {LMD_FLG_ABORT_RECOV,           "abort_recov"},
1224         {LMD_FLG_ABORT_RECOV,           "abort_recovery"},
1225         {LMD_FLG_NOSVC,                 "nosvc"},
1226         {LMD_FLG_MGS,                   "mgs"},
1227         {LMD_FLG_NOMGS,                 "nomgs"},
1228         {LMD_FLG_WRITECONF,             "writeconf"},
1229         {LMD_FLG_NOIR,                  "noir"},
1230         {LMD_FLG_NOSCRUB,               "noscrub"},
1231         {LMD_FLG_NO_PRIMNODE,           "noprimnode"},
1232         {LMD_FLG_VIRGIN,                "virgin"},
1233         {LMD_FLG_UPDATE,                "update"},
1234         {LMD_FLG_DEV_RDONLY,            "rdonly_dev"},
1235         {LMD_FLG_NO_CREATE,             "no_create"},
1236         {LMD_FLG_NO_CREATE,             "no_precreate"},
1237         {LMD_FLG_LOCAL_RECOV,           "localrecov"},
1238         {LMD_FLG_ABORT_RECOV_MDT,       "abort_recov_mdt"},
1239         {LMD_FLG_ABORT_RECOV_MDT,       "abort_recovery_mdt"},
1240         {LMD_FLG_NO_LOCAL_LOGS,         "nolocallogs"},
1241
1242         {LMD_OPT_RECOVERY_TIME_SOFT,    "recovery_time_soft=%u"},
1243         {LMD_OPT_RECOVERY_TIME_HARD,    "recovery_time_hard=%u"},
1244         {LMD_OPT_MGSNODE,               "mgsnode=%s"},
1245         {LMD_OPT_MGSSEC,                "mgssec=%s"},
1246         {LMD_OPT_EXCLUDE,               "exclude=%s"},
1247         {LMD_OPT_SVNAME,                "svname=%s"},
1248         {LMD_OPT_PARAM,                 "param=%s"},
1249         {LMD_OPT_OSD,                   "osd=%s"},
1250         {LMD_OPT_NETWORK,               "network=%s"},
1251         {LMD_OPT_DEVICE,                "device=%s"}, /* should be last */
1252         {LMD_NUM_MOUNT_OPT,             NULL}
1253 };
1254
1255 /**
1256  * Find the first delimiter; comma; from the specified \a buf and
1257  * make \a *endh point to the string starting with the delimiter.
1258  * The character ':' is also a delimiter for Lustre but not match_table
1259  * so the string is not split on it. Making it safe to ignore.
1260  *
1261  * @buf         a delimiter-separated string
1262  * @endh        a pointer to a pointer that will point to the string
1263  *              starting with the delimiter
1264  *
1265  * Returns:     true if delimiter is found, false if delimiter is not found
1266  */
1267 static bool lmd_find_delimiter(char *buf, char **endh)
1268 {
1269         substring_t args[LMD_NUM_MOUNT_OPT];
1270         char *end, *tmp;
1271         size_t len;
1272         int token;
1273
1274         if (!buf)
1275                 return false;
1276
1277         /* No more options so we are done */
1278         end = strchr(buf, ',');
1279         if (!end)
1280                 return false;
1281
1282         len = end - buf;
1283         tmp = kstrndup(buf, len, GFP_KERNEL);
1284         if (!tmp)
1285                 return false;
1286
1287         args[0].to = NULL;
1288         args[0].from = NULL;
1289         token = match_token(tmp, lmd_flags_table, args);
1290         kfree(tmp);
1291         if (token != LMD_NUM_MOUNT_OPT)
1292                 return false;
1293
1294         if (endh)
1295                 *endh = end;
1296
1297         return true;
1298 }
1299
1300 /**
1301  * Make sure the string in \a buf is of a valid formt.
1302  *
1303  * @buf         a delimiter-separated string
1304  *
1305  * Returns:     true if string valid, false if string contains errors
1306  */
1307 static bool lmd_validate_param(char *buf)
1308 {
1309         char *c = buf;
1310         size_t pos;
1311
1312         if (!buf)
1313                 return false;
1314 try_again:
1315         pos = strcspn(c, "[]");
1316         if (!pos)
1317                 return true;
1318
1319         c += pos;
1320         /* Not a valid mount string */
1321         if (*c == ']') {
1322                 CWARN("invalid mount string format\n");
1323                 return false;
1324         }
1325
1326         if (*c == '[') {
1327                 char *right = strchr(c, ']'), *tmp;
1328
1329                 /* invalid mount string */
1330                 if (!right) {
1331                         CWARN("invalid mount string format\n");
1332                         return false;
1333                 }
1334                 c++;
1335
1336                 /* Test for [ .. [ .. ] */
1337                 tmp = strchr(c, '[');
1338                 if (tmp && tmp < right) {
1339                         CWARN("invalid mount string format\n");
1340                         return false;
1341                 }
1342
1343                 /* Test for [ .. @ .. ] which means brackets
1344                  * span more than one NID string.
1345                  */
1346                 tmp = strchr(c, '@');
1347                 if (tmp && tmp < right) {
1348                         CWARN("invalid mount string format\n");
1349                         return false;
1350                 }
1351
1352                 c = right++;
1353                 goto try_again;
1354         }
1355
1356         return true;
1357 }
1358
1359 /**
1360  * Find the first valid string delimited by comma or colon from the specified
1361  * @buf and parse it to see whether it's a valid nid list. If yes, @*endh
1362  * will point to the next string starting with the delimiter.
1363  *
1364  * @buf:        a delimiter-separated string
1365  *
1366  * Returns:     false   if the string is a valid nid list
1367  *              true    if the string is not a valid nid list
1368  */
1369 static bool lmd_parse_nidlist(char *buf)
1370 {
1371         LIST_HEAD(nidlist);
1372         bool invalid;
1373         char *end;
1374
1375         if (!buf)
1376                 return true;
1377
1378         end = strchr(buf, '=');
1379         if (end)
1380                 buf = end + 1;
1381
1382         while ((end = strchr(buf, '@')) != NULL) {
1383                 size_t pos = strcspn(end, ":,");
1384                 char c;
1385
1386                 end += pos;
1387                 c = end[0];
1388                 end[0] = '\0';
1389                 /* FIXME !!! Add IPv6 support to cfs_parse_nidlist */
1390                 if (strchr(buf, ':')) {
1391                         struct lnet_nid nid;
1392
1393                         if (libcfs_strnid(&nid, buf) < 0) {
1394                                 invalid = true;
1395                                 goto failed;
1396                         }
1397                 } else {
1398                         if (cfs_parse_nidlist(buf, &nidlist) < 0) {
1399                                 invalid = true;
1400                                 goto failed;
1401                         } else {
1402                                 cfs_free_nidlist(&nidlist);
1403                         }
1404                 }
1405                 end[0] = c;
1406                 end++;
1407                 buf = end;
1408         }
1409         invalid = false;
1410 failed:
1411         return invalid;
1412 }
1413
1414 /**
1415  * Parse mount line options
1416  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1417  * dev is passed as device=uml1:/lustre by mount.lustre_tgt
1418  */
1419 int lmd_parse(char *options, struct lustre_mount_data *lmd)
1420 {
1421         char *s1, *s2, *opts, *orig_opts, *devname = NULL;
1422         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1423         int rc = 0;
1424
1425         ENTRY;
1426         LASSERT(lmd);
1427         if (!options) {
1428                 LCONSOLE_ERROR_MSG(0x162,
1429                                    "Missing mount data: check /sbin/mount.lustre_tgt is installed.\n");
1430                 RETURN(-EINVAL);
1431         }
1432
1433         /* Options should be a string - try to detect old lmd data */
1434         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1435                 LCONSOLE_ERROR_MSG(0x163,
1436                                    "Using an old version of /sbin/mount.lustre. Please install version %s\n",
1437                                    LUSTRE_VERSION_STRING);
1438                 RETURN(-EINVAL);
1439         }
1440         lmd->lmd_magic = LMD_MAGIC;
1441
1442         /* Don't stomp on lmd_opts */
1443         opts = kstrdup(options, GFP_KERNEL);
1444         if (!opts)
1445                 RETURN(-ENOMEM);
1446         orig_opts = opts;
1447         s1 = opts;
1448
1449         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1450         if (!lmd->lmd_params)
1451                 GOTO(invalid, rc = -ENOMEM);
1452         lmd->lmd_params[0] = '\0';
1453
1454         /* Set default flags here */
1455         while ((s1 = strsep(&opts, ",")) != NULL) {
1456                 int time_min = OBD_RECOVERY_TIME_MIN, tmp;
1457                 substring_t args[LMD_NUM_MOUNT_OPT];
1458                 int token;
1459
1460                 if (!*s1)
1461                         continue;
1462                 /*
1463                  * Initialize args struct so we know whether arg was
1464                  * found; some options take optional arguments.
1465                  */
1466                 args[0].to = NULL;
1467                 args[0].from = NULL;
1468                 token = match_token(s1, lmd_flags_table, args);
1469                 if (token == LMD_NUM_MOUNT_OPT) {
1470                         if (match_wildcard("iam", s1) ||
1471                             match_wildcard("hsm", s1))
1472                                 continue;
1473
1474                         /* Normally we would error but client and
1475                          * server mounting is intertwine. So pass
1476                          * off unknown args to ll_options instead.
1477                          */
1478                         continue;
1479                 } else {
1480                         /* We found a known server option. Filter out
1481                          * the result out of the options string. The
1482                          * reset will be stored in lmd_opts.
1483                          */
1484                         char *tmp = strstr(options, s1);
1485
1486                         if (strcmp(tmp, s1) != 0) {
1487                                 s2 = tmp + strlen(s1) + 1;
1488                                 memmove(tmp, s2, strlen(s2) + 1);
1489                         } else {
1490                                 *tmp = 0;
1491                         }
1492                 }
1493
1494                 /*
1495                  * Client options are parsed in ll_options: eg. flock,
1496                  * user_xattr, acl
1497                  */
1498
1499                 /*
1500                  * Parse non-ldiskfs options here. Rather than modifying
1501                  * ldiskfs, we just zero these out here
1502                  */
1503                 switch (token) {
1504                 case LMD_FLG_ABORT_RECOV_MDT:
1505                 case LMD_FLG_ABORT_RECOV:
1506                 case LMD_FLG_NO_CREATE:
1507                 case LMD_FLG_NOIR: /* test purpose only. */
1508                 case LMD_FLG_NOSVC:
1509                 case LMD_FLG_NOMGS:
1510                 case LMD_FLG_NOSCRUB:
1511                 case LMD_FLG_SKIP_LFSCK:
1512                 case LMD_FLG_DEV_RDONLY:
1513                 case LMD_FLG_WRITECONF:
1514                 case LMD_FLG_NO_LOCAL_LOGS:
1515                 case LMD_FLG_UPDATE:
1516                 case LMD_FLG_VIRGIN:
1517                 case LMD_FLG_NO_PRIMNODE:
1518                 case LMD_FLG_MGS: /* We are an MGS */
1519                 case LMD_FLG_LOCAL_RECOV:
1520                         set_bit(token, lmd->lmd_flags);
1521                         break;
1522                 case LMD_OPT_RECOVERY_TIME_SOFT:
1523                         rc = match_int(args, &tmp);
1524                         if (rc == 0)
1525                                 lmd->lmd_recovery_time_soft = max_t(int, tmp,
1526                                                                     time_min);
1527                         break;
1528                 case LMD_OPT_RECOVERY_TIME_HARD:
1529                         rc = match_int(args, &tmp);
1530                         if (rc == 0)
1531                                 lmd->lmd_recovery_time_hard = max_t(int, tmp,
1532                                                                     time_min);
1533                         break;
1534                 case LMD_OPT_MGSNODE:
1535                         /* Assume the next mount opt is the first
1536                          * invalid NID we get to.
1537                          */
1538                         rc = lmd_parse_mgs(lmd, args->from, &opts);
1539                         if (rc < 0)
1540                                 GOTO(invalid, rc);
1541
1542                         if (strcmp(options, opts) != 0) {
1543                                 s2 = strstr(options, opts);
1544                                 if (s2)
1545                                         options = s2;
1546                         }
1547                         break;
1548                 case LMD_OPT_MGSSEC:
1549                         rc = lmd_parse_mgssec(lmd, args->from);
1550                         break;
1551                 case LMD_OPT_EXCLUDE:
1552                         /* ost exclusion list */
1553                         rc = lmd_make_exclusion(lmd, args->from);
1554                         break;
1555                 case LMD_OPT_SVNAME:
1556                         rc = lmd_parse_string(&lmd->lmd_profile, args->from);
1557                         break;
1558                 case LMD_OPT_PARAM: {
1559                         size_t length = strlen(args->from), params_length;
1560                         char *tail = NULL, *entry;
1561
1562                         params_length = strlen(lmd->lmd_params);
1563                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN) {
1564                                 rc = -E2BIG;
1565                                 goto bad_string;
1566                         }
1567                         entry = lmd->lmd_params + params_length;
1568                         strncat(lmd->lmd_params, args->from, length);
1569
1570                         /* Find end of param string */
1571                         while (lmd_find_delimiter(opts, &tail)) {
1572                                 params_length = strlen(lmd->lmd_params);
1573                                 /* match_table splits by ',' so fill it in */
1574                                 lmd->lmd_params[params_length++] = ',';
1575
1576                                 length = tail - opts + 1;
1577                                 if (!length)
1578                                         break;
1579                                 if (params_length + length + 1 >=
1580                                     LMD_PARAMS_MAXLEN) {
1581                                         rc = -E2BIG;
1582                                         goto bad_string;
1583                                 }
1584
1585                                 strscpy(lmd->lmd_params + params_length,
1586                                         opts, length);
1587                                 opts = tail + 1;
1588                         }
1589
1590                         lmd->lmd_params[params_length + length] = '\0';
1591
1592                         if (!lmd_validate_param(entry)) {
1593                                 rc = -EINVAL;
1594                                 goto bad_string;
1595                         }
1596
1597                         /* param contains NIDs */
1598                         if (strchr(entry, '@') && lmd_parse_nidlist(entry)) {
1599                                 rc = -EINVAL;
1600                                 goto bad_string;
1601                         }
1602
1603                         /* remove params from opts string from options string */
1604                         if (strlen(args->from) != strlen(entry)) {
1605                                 char *tmp = entry + strlen(args->from) + 1;
1606
1607                                 s2 = strstr(options, tmp);
1608                                 if (s2) {
1609                                         size_t len = strlen(s2) - strlen(tmp);
1610
1611                                         memmove(s2, s2 + strlen(tmp) + 1, len);
1612                                 }
1613                         }
1614
1615                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1616                         if (tail)
1617                                 opts = tail + 1;
1618 bad_string:
1619                         break;
1620                 }
1621                 case LMD_OPT_OSD:
1622                         rc = lmd_parse_string(&lmd->lmd_osd_type, args->from);
1623                         break;
1624                 case LMD_OPT_DEVICE: {
1625                         size_t len = 0;
1626
1627                         /* match_table splits strings at ',' so we need to
1628                          * piece things back together.
1629                          */
1630                         if (opts) {
1631                                 len = strlen(opts) + 1;
1632
1633                                 /* Move to last part of device string */
1634                                 s2 = strchr(opts, '/');
1635                                 if (!s2)
1636                                         GOTO(invalid, rc = -EINVAL);
1637
1638                                 /* See if more options exist */
1639                                 s2 = strchr(s2, ',');
1640                                 if (s2)
1641                                         len = s2 - opts;
1642                         }
1643                         len += strlen(args->from) + 1;
1644
1645                         /* Freed in lustre_free_lsi */
1646                         OBD_ALLOC(lmd->lmd_dev, len);
1647                         if (!lmd->lmd_dev)
1648                                 GOTO(invalid, rc = -ENOMEM);
1649
1650                         if (opts)
1651                                 snprintf(lmd->lmd_dev, len, "%s,%s",
1652                                          args->from, opts);
1653                         else
1654                                 strscpy(lmd->lmd_dev, args->from, len);
1655
1656                         devname = lmd->lmd_dev;
1657
1658                         /* remove the split string 'opts' from options */
1659                         if (opts) {
1660                                 s1 = strstr(options, opts);
1661                                 if (s1) {
1662                                         /* opts start after args->from so
1663                                          * reduce len.
1664                                          */
1665                                         len -= strlen(args->from) + 2;
1666                                         s2 = s1 + len;
1667                                         memmove(options, s2, strlen(s2) + 1);
1668                                         opts += len;
1669                                 }
1670                         }
1671                         break;
1672                 }
1673                 case LMD_OPT_NETWORK:
1674                         rc = lmd_parse_network(lmd, args->from);
1675                         /* check if LNet dynamic peer discovery is activated */
1676                         if (LNetGetPeerDiscoveryStatus()) {
1677                                 CERROR("LNet Dynamic Peer Discovery is enabled on this node. 'network' mount option cannot be taken into account.\n");
1678                                 rc = -EINVAL;
1679                         }
1680                         break;
1681                 }
1682         }
1683         if (rc < 0)
1684                 GOTO(invalid, rc);
1685
1686         if (!devname) {
1687                 LCONSOLE_ERROR_MSG(0x164,
1688                                    "Can't find device name (need mount option 'device=...')\n");
1689                 GOTO(invalid, rc = -ENODEV);
1690         }
1691
1692         s1 = strstr(devname, ":/");
1693         if (s1) {
1694                 ++s1;
1695                 set_bit(LMD_FLG_CLIENT, lmd->lmd_flags);
1696                 /* Remove leading /s from fsname */
1697                 while (*++s1 == '/')
1698                         ;
1699                 s2 = s1;
1700                 while (*s2 != '/' && *s2 != '\0')
1701                         s2++;
1702                 /* Freed in lustre_free_lsi */
1703                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1704                 if (!lmd->lmd_profile)
1705                         GOTO(invalid, rc = -ENOMEM);
1706
1707                 strncat(lmd->lmd_profile, s1, s2 - s1);
1708                 strncat(lmd->lmd_profile, "-client", 7);
1709
1710                 s1 = s2;
1711                 s2 = s1 + strlen(s1) - 1;
1712                 /* Remove padding /s from fileset */
1713                 while (*s2 == '/')
1714                         s2--;
1715                 if (s2 > s1) {
1716                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1717                         if (!lmd->lmd_fileset)
1718                                 GOTO(invalid, rc = -ENOMEM);
1719                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1720                 }
1721         } else {
1722                 /* server mount */
1723                 if (lmd->lmd_nidnet != NULL) {
1724                         /* 'network=' mount option forbidden for server */
1725                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1726                         lmd->lmd_nidnet = NULL;
1727                         rc = -EINVAL;
1728                         CERROR("%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1729                                devname, rc);
1730                         GOTO(invalid, rc);
1731                 }
1732         }
1733
1734         /* Save mount options */
1735         s1 = options + strlen(options) - 1;
1736         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1737                 *s1-- = 0;
1738         while (*options && (*options == ',' || *options == ' '))
1739                 options++;
1740         if (*options != 0) {
1741                 /* Freed in lustre_free_lsi */
1742                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1743                 if (!lmd->lmd_opts)
1744                         GOTO(invalid, rc = -ENOMEM);
1745                 strncpy(lmd->lmd_opts, options, strlen(options));
1746         }
1747
1748         lmd_print(lmd);
1749 invalid:
1750         if (rc < 0)
1751                 CERROR("Bad mount options %s\n", options);
1752         kfree(orig_opts);
1753
1754         RETURN(rc);
1755 }
1756 EXPORT_SYMBOL(lmd_parse);