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