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