Whamcloud - gitweb
LU-1904 idl: add checks for OBD_CONNECT flags
[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_nid(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_nid(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_nid(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_nid(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_nid_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 static 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
572 /***************** lustre superblock **************/
573
574 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
575 {
576         struct lustre_sb_info *lsi;
577
578         ENTRY;
579
580         OBD_ALLOC_PTR(lsi);
581         if (!lsi)
582                 RETURN(NULL);
583         OBD_ALLOC_PTR(lsi->lsi_lmd);
584         if (!lsi->lsi_lmd) {
585                 OBD_FREE_PTR(lsi);
586                 RETURN(NULL);
587         }
588
589         s2lsi_nocast(sb) = lsi;
590         /* we take 1 extra ref for our setup */
591         atomic_set(&lsi->lsi_mounts, 1);
592
593         /* Default umount style */
594         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
595         INIT_LIST_HEAD(&lsi->lsi_lwp_list);
596         mutex_init(&lsi->lsi_lwp_mutex);
597
598         RETURN(lsi);
599 }
600 EXPORT_SYMBOL(lustre_init_lsi);
601
602 static int lustre_free_lsi(struct super_block *sb)
603 {
604         struct lustre_sb_info *lsi = s2lsi(sb);
605
606         ENTRY;
607
608         LASSERT(lsi != NULL);
609         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
610
611         /* someone didn't call server_put_mount. */
612         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
613
614         llcrypt_sb_free(sb);
615         if (lsi->lsi_lmd != NULL) {
616                 if (lsi->lsi_lmd->lmd_dev != NULL)
617                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
618                                 strlen(lsi->lsi_lmd->lmd_dev) + 1);
619                 if (lsi->lsi_lmd->lmd_profile != NULL)
620                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
621                                 strlen(lsi->lsi_lmd->lmd_profile) + 1);
622                 if (lsi->lsi_lmd->lmd_fileset != NULL)
623                         OBD_FREE(lsi->lsi_lmd->lmd_fileset,
624                                 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
625                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
626                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
627                                 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
628                 if (lsi->lsi_lmd->lmd_opts != NULL)
629                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
630                                 strlen(lsi->lsi_lmd->lmd_opts) + 1);
631                 if (lsi->lsi_lmd->lmd_exclude_count)
632                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
633                                 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
634                                 lsi->lsi_lmd->lmd_exclude_count);
635                 if (lsi->lsi_lmd->lmd_mgs != NULL)
636                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
637                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
638                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
639                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
640                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
641                 if (lsi->lsi_lmd->lmd_params != NULL)
642                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
643                 if (lsi->lsi_lmd->lmd_nidnet != NULL)
644                         OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
645                                 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
646
647                 OBD_FREE_PTR(lsi->lsi_lmd);
648         }
649
650         LASSERT(lsi->lsi_llsbi == NULL);
651         OBD_FREE_PTR(lsi);
652         s2lsi_nocast(sb) = NULL;
653
654         RETURN(0);
655 }
656
657 /*
658  * The lsi has one reference for every server that is using the disk -
659  * e.g. MDT, MGS, and potentially MGC
660  */
661 int lustre_put_lsi(struct super_block *sb)
662 {
663         struct lustre_sb_info *lsi = s2lsi(sb);
664
665         ENTRY;
666
667         LASSERT(lsi != NULL);
668
669         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
670         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
671                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
672                         lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
673                         lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
674                         lsi->lsi_dt_dev = NULL;
675                         obd_disconnect(lsi->lsi_osd_exp);
676                         /* wait till OSD is gone */
677                         obd_zombie_barrier();
678                 }
679                 lustre_free_lsi(sb);
680                 RETURN(1);
681         }
682         RETURN(0);
683 }
684 EXPORT_SYMBOL(lustre_put_lsi);
685
686 /*
687  * The goal of this function is to extract the file system name
688  * from the OBD name. This can come in two flavors. One is
689  * fsname-MDTXXXX or fsname-XXXXXXX were X is a hexadecimal
690  * number. In both cases we should return fsname. If it is
691  * not a valid OBD name it is assumed to be the file system
692  * name itself.
693  */
694 void obdname2fsname(const char *tgt, char *fsname, size_t buflen)
695 {
696         const char *ptr;
697         const char *tmp;
698         size_t len = 0;
699
700         /*
701          * First we have to see if the @tgt has '-' at all. It is
702          * valid for the user to request something like
703          * lctl set_param -P llite.lustre*.xattr_cache=0
704          */
705         ptr = strrchr(tgt, '-');
706         if (!ptr) {
707                 /* No '-' means it could end in '*' */
708                 ptr = strchr(tgt, '*');
709                 if (!ptr) {
710                         /* No '*' either. Assume tgt = fsname */
711                         len = strlen(tgt);
712                         goto valid_obd_name;
713                 }
714                 len = ptr - tgt;
715                 goto valid_obd_name;
716         }
717
718         /* tgt format fsname-MDT0000-* */
719         if ((!strncmp(ptr, "-MDT", 4) ||
720              !strncmp(ptr, "-OST", 4)) &&
721              (isxdigit(ptr[4]) && isxdigit(ptr[5]) &&
722               isxdigit(ptr[6]) && isxdigit(ptr[7]))) {
723                 len = ptr - tgt;
724                 goto valid_obd_name;
725         }
726
727         /*
728          * tgt_format fsname-cli'dev'-'uuid' except for the llite case
729          * which are named fsname-'uuid'. Examples:
730          *
731          * lustre-clilov-ffff88104db5b800
732          * lustre-ffff88104db5b800  (for llite device)
733          *
734          * The length of the OBD uuid can vary on different platforms.
735          * This test if any invalid characters are in string. Allow
736          * wildcards with '*' character.
737          */
738         ptr++;
739         if (!strspn(ptr, "0123456789abcdefABCDEF*")) {
740                 len = 0;
741                 goto no_fsname;
742         }
743
744         /*
745          * Now that we validated the device name lets extract the
746          * file system name. Most of the names in this class will
747          * have '-cli' in its name which needs to be dropped. If
748          * it doesn't have '-cli' then its a llite device which
749          * ptr already points to the start of the uuid string.
750          */
751         tmp = strstr(tgt, "-cli");
752         if (tmp)
753                 ptr = tmp;
754         else
755                 ptr--;
756         len = ptr - tgt;
757 valid_obd_name:
758         len = min_t(size_t, len, LUSTRE_MAXFSNAME);
759         snprintf(fsname, buflen, "%.*s", (int)len, tgt);
760 no_fsname:
761         fsname[len] = '\0';
762 }
763 EXPORT_SYMBOL(obdname2fsname);
764
765 /**
766  * SERVER NAME ***
767  * <FSNAME><SEPARATOR><TYPE><INDEX>
768  * FSNAME is between 1 and 8 characters (inclusive).
769  *      Excluded characters are '/' and ':'
770  * SEPARATOR is either ':' or '-'
771  * TYPE: "OST", "MDT", etc.
772  * INDEX: Hex representation of the index
773  */
774
775 /**
776  * Get the fsname ("lustre") from the server name ("lustre-OST003F").
777  * @param [in] svname server name including type and index
778  * @param [out] fsname Buffer to copy filesystem name prefix into.
779  *  Must have at least 'strlen(fsname) + 1' chars.
780  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
781  * rc < 0  on error
782  */
783 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
784 {
785         const char *dash;
786
787         dash = svname + strnlen(svname, LUSTRE_MAXFSNAME);
788         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
789                 ;
790         if (dash == svname)
791                 return -EINVAL;
792
793         if (fsname != NULL) {
794                 strncpy(fsname, svname, dash - svname);
795                 fsname[dash - svname] = '\0';
796         }
797
798         if (endptr != NULL)
799                 *endptr = dash;
800
801         return 0;
802 }
803 EXPORT_SYMBOL(server_name2fsname);
804
805 #ifdef HAVE_SERVER_SUPPORT
806 /**
807  * Get service name (svname) from string
808  * rc < 0 on error
809  * if endptr isn't NULL it is set to end of fsname *
810  */
811 int server_name2svname(const char *label, char *svname, const char **endptr,
812                        size_t svsize)
813 {
814         int rc;
815         const char *dash;
816
817         /* We use server_name2fsname() just for parsing */
818         rc = server_name2fsname(label, NULL, &dash);
819         if (rc != 0)
820                 return rc;
821
822         if (endptr != NULL)
823                 *endptr = dash;
824
825         if (strlcpy(svname, dash + 1, svsize) >= svsize)
826                 return -E2BIG;
827
828         return 0;
829 }
830 EXPORT_SYMBOL(server_name2svname);
831 #endif /* HAVE_SERVER_SUPPORT */
832
833 /**
834  * check server name is OST.
835  **/
836 int server_name_is_ost(const char *svname)
837 {
838         const char *dash;
839         int rc;
840
841         /* We use server_name2fsname() just for parsing */
842         rc = server_name2fsname(svname, NULL, &dash);
843         if (rc != 0)
844                 return rc;
845
846         dash++;
847
848         if (strncmp(dash, "OST", 3) == 0)
849                 return 1;
850         return 0;
851 }
852 EXPORT_SYMBOL(server_name_is_ost);
853
854 /**
855  * Get the index from the target name MDTXXXX/OSTXXXX
856  * rc = server type, or rc < 0  on error
857  **/
858 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
859 {
860         const char *dash = tgtname;
861         unsigned long index;
862         int rc;
863
864         if (strncmp(dash, "MDT", 3) == 0)
865                 rc = LDD_F_SV_TYPE_MDT;
866         else if (strncmp(dash, "OST", 3) == 0)
867                 rc = LDD_F_SV_TYPE_OST;
868         else
869                 return -EINVAL;
870
871         dash += 3;
872
873         if (strncmp(dash, "all", 3) == 0) {
874                 if (endptr != NULL)
875                         *endptr = dash + 3;
876                 return rc | LDD_F_SV_ALL;
877         }
878
879         index = simple_strtoul(dash, (char **)endptr, 16);
880         if (idx != NULL)
881                 *idx = index;
882
883         if (index > 0xffff)
884                 return -ERANGE;
885
886         return rc;
887 }
888 EXPORT_SYMBOL(target_name2index);
889
890 /*
891  * Get the index from the OBD name.
892  * rc = server type, or
893  * rc < 0  on error
894  * if endptr isn't NULL it is set to end of name
895  */
896 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
897 {
898         const char *dash;
899         int rc;
900
901         /* We use server_name2fsname() just for parsing */
902         rc = server_name2fsname(svname, NULL, &dash);
903         if (rc != 0)
904                 return rc;
905
906         dash++;
907         rc = target_name2index(dash, idx, endptr);
908         if (rc < 0)
909                 return rc;
910
911         /* Account for -mdc after index that is possible when specifying mdt */
912         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
913                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
914                 *endptr += sizeof(LUSTRE_MDC_NAME);
915
916         return rc;
917 }
918 EXPORT_SYMBOL(server_name2index);
919
920 /*************** mount common betweeen server and client ***************/
921
922 /* Common umount */
923 int lustre_common_put_super(struct super_block *sb)
924 {
925         int rc;
926
927         ENTRY;
928
929         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
930
931         /* Drop a ref to the MGC */
932         rc = lustre_stop_mgc(sb);
933         if (rc && (rc != -ENOENT)) {
934                 if (rc != -EBUSY) {
935                         CERROR("Can't stop MGC: %d\n", rc);
936                         RETURN(rc);
937                 }
938                 /*
939                  * BUSY just means that there's some other OBD that
940                  * needs the mgc.  Let him clean it up.
941                  */
942                 CDEBUG(D_MOUNT, "MGC still in use\n");
943         }
944         /* Drop a ref to the mounted disk */
945         lustre_put_lsi(sb);
946
947         RETURN(rc);
948 }
949 EXPORT_SYMBOL(lustre_common_put_super);
950
951 static void lmd_print(struct lustre_mount_data *lmd)
952 {
953         int i;
954
955         PRINT_CMD(D_MOUNT, "  mount data:\n");
956         if (lmd_is_client(lmd))
957                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
958         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
959         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
960
961         if (lmd->lmd_opts)
962                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
963
964         if (lmd->lmd_recovery_time_soft)
965                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
966                           lmd->lmd_recovery_time_soft);
967
968         if (lmd->lmd_recovery_time_hard)
969                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
970                           lmd->lmd_recovery_time_hard);
971
972         for (i = 0; i < lmd->lmd_exclude_count; i++) {
973                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
974                           lmd->lmd_exclude[i]);
975         }
976 }
977
978 /* Is this server on the exclusion list */
979 int lustre_check_exclusion(struct super_block *sb, char *svname)
980 {
981         struct lustre_sb_info *lsi = s2lsi(sb);
982         struct lustre_mount_data *lmd = lsi->lsi_lmd;
983         __u32 index;
984         int i, rc;
985
986         ENTRY;
987
988         rc = server_name2index(svname, &index, NULL);
989         if (rc != LDD_F_SV_TYPE_OST)
990                 /* Only exclude OSTs */
991                 RETURN(0);
992
993         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
994                index, lmd->lmd_exclude_count, lmd->lmd_dev);
995
996         for (i = 0; i < lmd->lmd_exclude_count; i++) {
997                 if (index == lmd->lmd_exclude[i]) {
998                         CWARN("Excluding %s (on exclusion list)\n", svname);
999                         RETURN(1);
1000                 }
1001         }
1002         RETURN(0);
1003 }
1004
1005 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1006 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1007 {
1008         const char *s1 = ptr, *s2;
1009         __u32 *exclude_list;
1010         __u32 index = 0;
1011         int rc = 0, devmax;
1012
1013         ENTRY;
1014
1015         /*
1016          * The shortest an ost name can be is 8 chars: -OST0000.
1017          * We don't actually know the fsname at this time, so in fact
1018          * a user could specify any fsname.
1019          */
1020         devmax = strlen(ptr) / 8 + 1;
1021
1022         /* temp storage until we figure out how many we have */
1023         OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
1024         if (!exclude_list)
1025                 RETURN(-ENOMEM);
1026
1027         /* we enter this fn pointing at the '=' */
1028         while (*s1 && *s1 != ' ' && *s1 != ',') {
1029                 s1++;
1030                 rc = server_name2index(s1, &index, &s2);
1031                 if (rc < 0) {
1032                         CERROR("Can't parse server name '%s': rc = %d\n",
1033                                s1, rc);
1034                         break;
1035                 }
1036                 if (rc == LDD_F_SV_TYPE_OST)
1037                         exclude_list[lmd->lmd_exclude_count++] = index;
1038                 else
1039                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1040                                (uint)(s2-s1), s1, rc);
1041                 s1 = s2;
1042                 /*
1043                  * now we are pointing at ':' (next exclude)
1044                  * or ',' (end of excludes)
1045                  */
1046                 if (lmd->lmd_exclude_count >= devmax)
1047                         break;
1048         }
1049         if (rc >= 0) /* non-err */
1050                 rc = 0;
1051
1052         if (lmd->lmd_exclude_count) {
1053                 /* permanent, freed in lustre_free_lsi */
1054                 OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
1055                                     lmd->lmd_exclude_count);
1056                 if (lmd->lmd_exclude) {
1057                         memcpy(lmd->lmd_exclude, exclude_list,
1058                                sizeof(index) * lmd->lmd_exclude_count);
1059                 } else {
1060                         rc = -ENOMEM;
1061                         lmd->lmd_exclude_count = 0;
1062                 }
1063         }
1064         OBD_FREE_PTR_ARRAY(exclude_list, devmax);
1065         RETURN(rc);
1066 }
1067
1068 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1069 {
1070         char *tail;
1071         int length;
1072
1073         if (lmd->lmd_mgssec != NULL) {
1074                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1075                 lmd->lmd_mgssec = NULL;
1076         }
1077
1078         tail = strchr(ptr, ',');
1079         if (tail == NULL)
1080                 length = strlen(ptr);
1081         else
1082                 length = tail - ptr;
1083
1084         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1085         if (lmd->lmd_mgssec == NULL)
1086                 return -ENOMEM;
1087
1088         memcpy(lmd->lmd_mgssec, ptr, length);
1089         lmd->lmd_mgssec[length] = '\0';
1090         return 0;
1091 }
1092
1093 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1094 {
1095         char *tail;
1096         int length;
1097
1098         if (lmd->lmd_nidnet != NULL) {
1099                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1100                 lmd->lmd_nidnet = NULL;
1101         }
1102
1103         tail = strchr(ptr, ',');
1104         if (tail == NULL)
1105                 length = strlen(ptr);
1106         else
1107                 length = tail - ptr;
1108
1109         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1110         if (lmd->lmd_nidnet == NULL)
1111                 return -ENOMEM;
1112
1113         memcpy(lmd->lmd_nidnet, ptr, length);
1114         lmd->lmd_nidnet[length] = '\0';
1115         return 0;
1116 }
1117
1118 static int lmd_parse_string(char **handle, char *ptr)
1119 {
1120         char *tail;
1121         int length;
1122
1123         if ((handle == NULL) || (ptr == NULL))
1124                 return -EINVAL;
1125
1126         if (*handle != NULL) {
1127                 OBD_FREE(*handle, strlen(*handle) + 1);
1128                 *handle = NULL;
1129         }
1130
1131         tail = strchr(ptr, ',');
1132         if (tail == NULL)
1133                 length = strlen(ptr);
1134         else
1135                 length = tail - ptr;
1136
1137         OBD_ALLOC(*handle, length + 1);
1138         if (*handle == NULL)
1139                 return -ENOMEM;
1140
1141         memcpy(*handle, ptr, length);
1142         (*handle)[length] = '\0';
1143
1144         return 0;
1145 }
1146
1147 /* Collect multiple values for mgsnid specifiers */
1148 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1149 {
1150         lnet_nid_t nid;
1151         char *tail = *ptr;
1152         char *mgsnid;
1153         int length;
1154         int oldlen = 0;
1155
1156         /* Find end of NID-list */
1157         while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
1158                 ; /* do nothing */
1159
1160         length = tail - *ptr;
1161         if (length == 0) {
1162                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1163                 return -EINVAL;
1164         }
1165
1166         if (lmd->lmd_mgs != NULL)
1167                 oldlen = strlen(lmd->lmd_mgs) + 1;
1168
1169         OBD_ALLOC(mgsnid, oldlen + length + 1);
1170         if (mgsnid == NULL)
1171                 return -ENOMEM;
1172
1173         if (lmd->lmd_mgs != NULL) {
1174                 /* Multiple mgsnid= are taken to mean failover locations */
1175                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1176                 mgsnid[oldlen - 1] = ':';
1177                 OBD_FREE(lmd->lmd_mgs, oldlen);
1178         }
1179         memcpy(mgsnid + oldlen, *ptr, length);
1180         mgsnid[oldlen + length] = '\0';
1181         lmd->lmd_mgs = mgsnid;
1182         *ptr = tail;
1183
1184         return 0;
1185 }
1186
1187 /**
1188  * Find the first delimiter (comma or colon) from the specified \a buf and
1189  * make \a *endh point to the string starting with the delimiter. The commas
1190  * in expression list [...] will be skipped.
1191  *
1192  * @buf         a delimiter-separated string
1193  * @endh        a pointer to a pointer that will point to the string
1194  *              starting with the delimiter
1195  *
1196  * RETURNS      true if delimiter is found, false if delimiter is not found
1197  */
1198 static bool lmd_find_delimiter(char *buf, char **endh)
1199 {
1200         char *c = buf;
1201         size_t pos;
1202         bool found;
1203
1204         if (!buf)
1205                 return false;
1206 try_again:
1207         if (*c == ',' || *c == ':')
1208                 return true;
1209
1210         pos = strcspn(c, "[:,]");
1211         if (!pos)
1212                 return false;
1213
1214         /* Not a valid mount string */
1215         if (*c == ']') {
1216                 CWARN("invalid mount string format\n");
1217                 return false;
1218         }
1219
1220         c += pos;
1221         if (*c == '[') {
1222                 c = strchr(c, ']');
1223
1224                 /* invalid mount string */
1225                 if (!c) {
1226                         CWARN("invalid mount string format\n");
1227                         return false;
1228                 }
1229                 c++;
1230                 goto try_again;
1231         }
1232
1233         found = *c != '\0';
1234         if (found && endh)
1235                 *endh = c;
1236
1237         return found;
1238 }
1239
1240 /**
1241  * Find the first valid string delimited by comma or colon from the specified
1242  * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
1243  * will point to the next string starting with the delimiter.
1244  *
1245  * \param[in] buf       a delimiter-separated string
1246  * \param[in] endh      a pointer to a pointer that will point to the string
1247  *                      starting with the delimiter
1248  *
1249  * \retval 0            if the string is a valid NID list
1250  * \retval 1            if the string is not a valid NID list
1251  */
1252 static int lmd_parse_nidlist(char *buf, char **endh)
1253 {
1254         LIST_HEAD(nidlist);
1255         char *endp = buf;
1256         char tmp;
1257         int rc = 0;
1258
1259         if (buf == NULL)
1260                 return 1;
1261         while (*buf == ',' || *buf == ':')
1262                 buf++;
1263         if (*buf == ' ' || *buf == '/' || *buf == '\0')
1264                 return 1;
1265
1266         if (!lmd_find_delimiter(buf, &endp))
1267                 endp = buf + strlen(buf);
1268
1269         tmp = *endp;
1270         *endp = '\0';
1271
1272         if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1273                 rc = 1;
1274         cfs_free_nidlist(&nidlist);
1275
1276         *endp = tmp;
1277         if (rc != 0)
1278                 return rc;
1279         if (endh != NULL)
1280                 *endh = endp;
1281         return 0;
1282 }
1283
1284 /**
1285  * Parse mount line options
1286  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1287  * dev is passed as device=uml1:/lustre by mount.lustre_tgt
1288  */
1289 int lmd_parse(char *options, struct lustre_mount_data *lmd)
1290 {
1291         char *s1, *s2, *devname = NULL;
1292         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1293         int rc = 0;
1294
1295         ENTRY;
1296
1297         LASSERT(lmd);
1298         if (!options) {
1299                 LCONSOLE_ERROR_MSG(0x162,
1300                                    "Missing mount data: check /sbin/mount.lustre_tgt is installed.\n");
1301                 RETURN(-EINVAL);
1302         }
1303
1304         /* Options should be a string - try to detect old lmd data */
1305         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1306                 LCONSOLE_ERROR_MSG(0x163,
1307                                    "Using an old version of /sbin/mount.lustre. Please install version %s\n",
1308                                    LUSTRE_VERSION_STRING);
1309                 RETURN(-EINVAL);
1310         }
1311         lmd->lmd_magic = LMD_MAGIC;
1312
1313         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1314         if (lmd->lmd_params == NULL)
1315                 RETURN(-ENOMEM);
1316         lmd->lmd_params[0] = '\0';
1317
1318         /* Set default flags here */
1319
1320         s1 = options;
1321         while (*s1) {
1322                 int clear = 0;
1323                 int time_min = OBD_RECOVERY_TIME_MIN;
1324                 char *s3;
1325
1326                 /* Skip whitespace and extra commas */
1327                 while (*s1 == ' ' || *s1 == ',')
1328                         s1++;
1329                 s3 = s1;
1330
1331                 /*
1332                  * Client options are parsed in ll_options: eg. flock,
1333                  * user_xattr, acl
1334                  */
1335
1336                 /*
1337                  * Parse non-ldiskfs options here. Rather than modifying
1338                  * ldiskfs, we just zero these out here
1339                  */
1340                 if (!strncmp(s1, "abort_recov_mdt", 15) ||
1341                     !strncmp(s1, "abort_recovery_mdt", 18)) {
1342                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV_MDT;
1343                         clear++;
1344                 } else if (strncmp(s1, "abort_recov", 11) == 0) {
1345                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1346                         clear++;
1347                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1348                         lmd->lmd_recovery_time_soft =
1349                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1350                                       time_min);
1351                         clear++;
1352                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1353                         lmd->lmd_recovery_time_hard =
1354                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1355                                       time_min);
1356                         clear++;
1357                 } else if (strncmp(s1, "no_precreate", 12) == 0) {
1358                         lmd->lmd_flags |= LMD_FLG_NO_PRECREATE;
1359                         clear++;
1360                 } else if (strncmp(s1, "noir", 4) == 0) {
1361                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1362                         clear++;
1363                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1364                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1365                         clear++;
1366                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1367                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1368                         clear++;
1369                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1370                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1371                         clear++;
1372                 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1373                         lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1374                         clear++;
1375                 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1376                         lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1377                         clear++;
1378                 } else if (strncmp(s1, PARAM_MGSNODE,
1379                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1380                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1381                         /*
1382                          * Assume the next mount opt is the first
1383                          * invalid NID we get to.
1384                          */
1385                         rc = lmd_parse_mgs(lmd, &s2);
1386                         if (rc)
1387                                 goto invalid;
1388                         s3 = s2;
1389                         clear++;
1390                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1391                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1392                         clear++;
1393                 } else if (strncmp(s1, "nolocallogs", 11) == 0) {
1394                         lmd->lmd_flags |= LMD_FLG_NO_LOCAL_LOGS;
1395                         clear++;
1396                 } else if (strncmp(s1, "update", 6) == 0) {
1397                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1398                         clear++;
1399                 } else if (strncmp(s1, "virgin", 6) == 0) {
1400                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1401                         clear++;
1402                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1403                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1404                         clear++;
1405                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1406                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1407                         if (rc)
1408                                 goto invalid;
1409                         clear++;
1410                         /* ost exclusion list */
1411                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1412                         rc = lmd_make_exclusion(lmd, s1 + 7);
1413                         if (rc)
1414                                 goto invalid;
1415                         clear++;
1416                 } else if (strncmp(s1, "mgs", 3) == 0) {
1417                         /* We are an MGS */
1418                         lmd->lmd_flags |= LMD_FLG_MGS;
1419                         clear++;
1420                 } else if (strncmp(s1, "svname=", 7) == 0) {
1421                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1422                         if (rc)
1423                                 goto invalid;
1424                         clear++;
1425                 } else if (strncmp(s1, "param=", 6) == 0) {
1426                         size_t length, params_length;
1427                         char  *tail = s1;
1428
1429                         if (lmd_find_delimiter(s1 + 6, &tail)) {
1430                                 char *param_str = tail + 1;
1431                                 int   supplementary = 1;
1432
1433                                 while (lmd_parse_nidlist(param_str,
1434                                                          &param_str) == 0) {
1435                                         supplementary = 0;
1436                                 }
1437                                 length = param_str - s1 - supplementary;
1438                         } else {
1439                                 length = strlen(s1);
1440                         }
1441                         length -= 6;
1442                         params_length = strlen(lmd->lmd_params);
1443                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1444                                 RETURN(-E2BIG);
1445                         strncat(lmd->lmd_params, s1 + 6, length);
1446                         lmd->lmd_params[params_length + length] = '\0';
1447                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1448                         s3 = s1 + 6 + length;
1449                         clear++;
1450                 } else if (strncmp(s1, "localrecov", 10) == 0) {
1451                         lmd->lmd_flags |= LMD_FLG_LOCAL_RECOV;
1452                         clear++;
1453                 } else if (strncmp(s1, "osd=", 4) == 0) {
1454                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1455                         if (rc)
1456                                 goto invalid;
1457                         clear++;
1458                 }
1459                 /*
1460                  * Linux 2.4 doesn't pass the device, so we stuck it at
1461                  * the end of the options.
1462                  */
1463                 else if (strncmp(s1, "device=", 7) == 0) {
1464                         devname = s1 + 7;
1465                         /*
1466                          * terminate options right before device.  device
1467                          * must be the last one.
1468                          */
1469                         *s1 = '\0';
1470                         break;
1471                 } else if (strncmp(s1, "network=", 8) == 0) {
1472                         rc = lmd_parse_network(lmd, s1 + 8);
1473                         if (rc)
1474                                 goto invalid;
1475
1476                         /* check if LNet dynamic peer discovery is activated */
1477                         if (LNetGetPeerDiscoveryStatus()) {
1478                                 CERROR("LNet Dynamic Peer Discovery is enabled "
1479                                        "on this node. 'network' mount option "
1480                                        "cannot be taken into account.\n");
1481                                 goto invalid;
1482                         }
1483
1484                         clear++;
1485                 }
1486
1487                 /* Find next opt */
1488                 s2 = strchr(s3, ',');
1489                 if (s2 == NULL) {
1490                         if (clear)
1491                                 *s1 = '\0';
1492                         break;
1493                 }
1494                 s2++;
1495                 if (clear)
1496                         memmove(s1, s2, strlen(s2) + 1);
1497                 else
1498                         s1 = s2;
1499         }
1500
1501         if (!devname) {
1502                 LCONSOLE_ERROR_MSG(0x164,
1503                                    "Can't find device name (need mount option 'device=...')\n");
1504                 goto invalid;
1505         }
1506
1507         s1 = strstr(devname, ":/");
1508         if (s1) {
1509                 ++s1;
1510                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1511                 /* Remove leading /s from fsname */
1512                 while (*++s1 == '/')
1513                         ;
1514                 s2 = s1;
1515                 while (*s2 != '/' && *s2 != '\0')
1516                         s2++;
1517                 /* Freed in lustre_free_lsi */
1518                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1519                 if (!lmd->lmd_profile)
1520                         RETURN(-ENOMEM);
1521
1522                 strncat(lmd->lmd_profile, s1, s2 - s1);
1523                 strncat(lmd->lmd_profile, "-client", 7);
1524
1525                 s1 = s2;
1526                 s2 = s1 + strlen(s1) - 1;
1527                 /* Remove padding /s from fileset */
1528                 while (*s2 == '/')
1529                         s2--;
1530                 if (s2 > s1) {
1531                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1532                         if (lmd->lmd_fileset == NULL) {
1533                                 OBD_FREE(lmd->lmd_profile,
1534                                          strlen(lmd->lmd_profile) + 1);
1535                                 RETURN(-ENOMEM);
1536                         }
1537                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1538                 }
1539         } else {
1540                 /* server mount */
1541                 if (lmd->lmd_nidnet != NULL) {
1542                         /* 'network=' mount option forbidden for server */
1543                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1544                         lmd->lmd_nidnet = NULL;
1545                         rc = -EINVAL;
1546                         CERROR(
1547                                "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1548                                devname, rc);
1549                         RETURN(rc);
1550                 }
1551         }
1552
1553         /* Freed in lustre_free_lsi */
1554         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1555         if (!lmd->lmd_dev)
1556                 RETURN(-ENOMEM);
1557         strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1558
1559         /* Save mount options */
1560         s1 = options + strlen(options) - 1;
1561         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1562                 *s1-- = 0;
1563         while (*options && (*options == ',' || *options == ' '))
1564                 options++;
1565         if (*options != 0) {
1566                 /* Freed in lustre_free_lsi */
1567                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1568                 if (!lmd->lmd_opts)
1569                         RETURN(-ENOMEM);
1570                 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1571         }
1572
1573         lmd_print(lmd);
1574         lmd->lmd_magic = LMD_MAGIC;
1575
1576         RETURN(rc);
1577
1578 invalid:
1579         CERROR("Bad mount options %s\n", options);
1580         RETURN(-EINVAL);
1581 }
1582 EXPORT_SYMBOL(lmd_parse);