Whamcloud - gitweb
LU-8238 ldlm: rid of obsolete param of ldlm_resource_get()
[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 /**
805  * Get service name (svname) from string
806  * rc < 0 on error
807  * if endptr isn't NULL it is set to end of fsname *
808  */
809 int server_name2svname(const char *label, char *svname, const char **endptr,
810                        size_t svsize)
811 {
812         int rc;
813         const char *dash;
814
815         /* We use server_name2fsname() just for parsing */
816         rc = server_name2fsname(label, NULL, &dash);
817         if (rc != 0)
818                 return rc;
819
820         if (endptr != NULL)
821                 *endptr = dash;
822
823         if (strlcpy(svname, dash + 1, svsize) >= svsize)
824                 return -E2BIG;
825
826         return 0;
827 }
828 EXPORT_SYMBOL(server_name2svname);
829
830 /**
831  * check server name is OST.
832  **/
833 int server_name_is_ost(const char *svname)
834 {
835         const char *dash;
836         int rc;
837
838         /* We use server_name2fsname() just for parsing */
839         rc = server_name2fsname(svname, NULL, &dash);
840         if (rc != 0)
841                 return rc;
842
843         dash++;
844
845         if (strncmp(dash, "OST", 3) == 0)
846                 return 1;
847         return 0;
848 }
849 EXPORT_SYMBOL(server_name_is_ost);
850
851 /**
852  * Get the index from the target name MDTXXXX/OSTXXXX
853  * rc = server type, or rc < 0  on error
854  **/
855 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
856 {
857         const char *dash = tgtname;
858         unsigned long index;
859         int rc;
860
861         if (strncmp(dash, "MDT", 3) == 0)
862                 rc = LDD_F_SV_TYPE_MDT;
863         else if (strncmp(dash, "OST", 3) == 0)
864                 rc = LDD_F_SV_TYPE_OST;
865         else
866                 return -EINVAL;
867
868         dash += 3;
869
870         if (strncmp(dash, "all", 3) == 0) {
871                 if (endptr != NULL)
872                         *endptr = dash + 3;
873                 return rc | LDD_F_SV_ALL;
874         }
875
876         index = simple_strtoul(dash, (char **)endptr, 16);
877         if (idx != NULL)
878                 *idx = index;
879
880         if (index > 0xffff)
881                 return -ERANGE;
882
883         return rc;
884 }
885 EXPORT_SYMBOL(target_name2index);
886
887 /*
888  * Get the index from the OBD name.
889  * rc = server type, or
890  * rc < 0  on error
891  * if endptr isn't NULL it is set to end of name
892  */
893 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
894 {
895         const char *dash;
896         int rc;
897
898         /* We use server_name2fsname() just for parsing */
899         rc = server_name2fsname(svname, NULL, &dash);
900         if (rc != 0)
901                 return rc;
902
903         dash++;
904         rc = target_name2index(dash, idx, endptr);
905         if (rc < 0)
906                 return rc;
907
908         /* Account for -mdc after index that is possible when specifying mdt */
909         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
910                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
911                 *endptr += sizeof(LUSTRE_MDC_NAME);
912
913         return rc;
914 }
915 EXPORT_SYMBOL(server_name2index);
916
917 /*************** mount common betweeen server and client ***************/
918
919 /* Common umount */
920 int lustre_common_put_super(struct super_block *sb)
921 {
922         int rc;
923
924         ENTRY;
925
926         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
927
928         /* Drop a ref to the MGC */
929         rc = lustre_stop_mgc(sb);
930         if (rc && (rc != -ENOENT)) {
931                 if (rc != -EBUSY) {
932                         CERROR("Can't stop MGC: %d\n", rc);
933                         RETURN(rc);
934                 }
935                 /*
936                  * BUSY just means that there's some other OBD that
937                  * needs the mgc.  Let him clean it up.
938                  */
939                 CDEBUG(D_MOUNT, "MGC still in use\n");
940         }
941         /* Drop a ref to the mounted disk */
942         lustre_put_lsi(sb);
943
944         RETURN(rc);
945 }
946 EXPORT_SYMBOL(lustre_common_put_super);
947
948 static void lmd_print(struct lustre_mount_data *lmd)
949 {
950         int i;
951
952         PRINT_CMD(D_MOUNT, "  mount data:\n");
953         if (lmd_is_client(lmd))
954                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
955         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
956         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
957
958         if (lmd->lmd_opts)
959                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
960
961         if (lmd->lmd_recovery_time_soft)
962                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
963                           lmd->lmd_recovery_time_soft);
964
965         if (lmd->lmd_recovery_time_hard)
966                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
967                           lmd->lmd_recovery_time_hard);
968
969         for (i = 0; i < lmd->lmd_exclude_count; i++) {
970                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
971                           lmd->lmd_exclude[i]);
972         }
973 }
974
975 /* Is this server on the exclusion list */
976 int lustre_check_exclusion(struct super_block *sb, char *svname)
977 {
978         struct lustre_sb_info *lsi = s2lsi(sb);
979         struct lustre_mount_data *lmd = lsi->lsi_lmd;
980         __u32 index;
981         int i, rc;
982
983         ENTRY;
984
985         rc = server_name2index(svname, &index, NULL);
986         if (rc != LDD_F_SV_TYPE_OST)
987                 /* Only exclude OSTs */
988                 RETURN(0);
989
990         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
991                index, lmd->lmd_exclude_count, lmd->lmd_dev);
992
993         for (i = 0; i < lmd->lmd_exclude_count; i++) {
994                 if (index == lmd->lmd_exclude[i]) {
995                         CWARN("Excluding %s (on exclusion list)\n", svname);
996                         RETURN(1);
997                 }
998         }
999         RETURN(0);
1000 }
1001
1002 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1003 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1004 {
1005         const char *s1 = ptr, *s2;
1006         __u32 *exclude_list;
1007         __u32 index = 0;
1008         int rc = 0, devmax;
1009
1010         ENTRY;
1011
1012         /*
1013          * The shortest an ost name can be is 8 chars: -OST0000.
1014          * We don't actually know the fsname at this time, so in fact
1015          * a user could specify any fsname.
1016          */
1017         devmax = strlen(ptr) / 8 + 1;
1018
1019         /* temp storage until we figure out how many we have */
1020         OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
1021         if (!exclude_list)
1022                 RETURN(-ENOMEM);
1023
1024         /* we enter this fn pointing at the '=' */
1025         while (*s1 && *s1 != ' ' && *s1 != ',') {
1026                 s1++;
1027                 rc = server_name2index(s1, &index, &s2);
1028                 if (rc < 0) {
1029                         CERROR("Can't parse server name '%s': rc = %d\n",
1030                                s1, rc);
1031                         break;
1032                 }
1033                 if (rc == LDD_F_SV_TYPE_OST)
1034                         exclude_list[lmd->lmd_exclude_count++] = index;
1035                 else
1036                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1037                                (uint)(s2-s1), s1, rc);
1038                 s1 = s2;
1039                 /*
1040                  * now we are pointing at ':' (next exclude)
1041                  * or ',' (end of excludes)
1042                  */
1043                 if (lmd->lmd_exclude_count >= devmax)
1044                         break;
1045         }
1046         if (rc >= 0) /* non-err */
1047                 rc = 0;
1048
1049         if (lmd->lmd_exclude_count) {
1050                 /* permanent, freed in lustre_free_lsi */
1051                 OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
1052                                     lmd->lmd_exclude_count);
1053                 if (lmd->lmd_exclude) {
1054                         memcpy(lmd->lmd_exclude, exclude_list,
1055                                sizeof(index) * lmd->lmd_exclude_count);
1056                 } else {
1057                         rc = -ENOMEM;
1058                         lmd->lmd_exclude_count = 0;
1059                 }
1060         }
1061         OBD_FREE_PTR_ARRAY(exclude_list, devmax);
1062         RETURN(rc);
1063 }
1064
1065 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1066 {
1067         char *tail;
1068         int length;
1069
1070         if (lmd->lmd_mgssec != NULL) {
1071                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1072                 lmd->lmd_mgssec = NULL;
1073         }
1074
1075         tail = strchr(ptr, ',');
1076         if (tail == NULL)
1077                 length = strlen(ptr);
1078         else
1079                 length = tail - ptr;
1080
1081         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1082         if (lmd->lmd_mgssec == NULL)
1083                 return -ENOMEM;
1084
1085         memcpy(lmd->lmd_mgssec, ptr, length);
1086         lmd->lmd_mgssec[length] = '\0';
1087         return 0;
1088 }
1089
1090 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1091 {
1092         char *tail;
1093         int length;
1094
1095         if (lmd->lmd_nidnet != NULL) {
1096                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1097                 lmd->lmd_nidnet = NULL;
1098         }
1099
1100         tail = strchr(ptr, ',');
1101         if (tail == NULL)
1102                 length = strlen(ptr);
1103         else
1104                 length = tail - ptr;
1105
1106         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1107         if (lmd->lmd_nidnet == NULL)
1108                 return -ENOMEM;
1109
1110         memcpy(lmd->lmd_nidnet, ptr, length);
1111         lmd->lmd_nidnet[length] = '\0';
1112         return 0;
1113 }
1114
1115 static int lmd_parse_string(char **handle, char *ptr)
1116 {
1117         char *tail;
1118         int length;
1119
1120         if ((handle == NULL) || (ptr == NULL))
1121                 return -EINVAL;
1122
1123         if (*handle != NULL) {
1124                 OBD_FREE(*handle, strlen(*handle) + 1);
1125                 *handle = NULL;
1126         }
1127
1128         tail = strchr(ptr, ',');
1129         if (tail == NULL)
1130                 length = strlen(ptr);
1131         else
1132                 length = tail - ptr;
1133
1134         OBD_ALLOC(*handle, length + 1);
1135         if (*handle == NULL)
1136                 return -ENOMEM;
1137
1138         memcpy(*handle, ptr, length);
1139         (*handle)[length] = '\0';
1140
1141         return 0;
1142 }
1143
1144 /* Collect multiple values for mgsnid specifiers */
1145 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1146 {
1147         lnet_nid_t nid;
1148         char *tail = *ptr;
1149         char *mgsnid;
1150         int length;
1151         int oldlen = 0;
1152
1153         /* Find end of NID-list */
1154         while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
1155                 ; /* do nothing */
1156
1157         length = tail - *ptr;
1158         if (length == 0) {
1159                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1160                 return -EINVAL;
1161         }
1162
1163         if (lmd->lmd_mgs != NULL)
1164                 oldlen = strlen(lmd->lmd_mgs) + 1;
1165
1166         OBD_ALLOC(mgsnid, oldlen + length + 1);
1167         if (mgsnid == NULL)
1168                 return -ENOMEM;
1169
1170         if (lmd->lmd_mgs != NULL) {
1171                 /* Multiple mgsnid= are taken to mean failover locations */
1172                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1173                 mgsnid[oldlen - 1] = ':';
1174                 OBD_FREE(lmd->lmd_mgs, oldlen);
1175         }
1176         memcpy(mgsnid + oldlen, *ptr, length);
1177         mgsnid[oldlen + length] = '\0';
1178         lmd->lmd_mgs = mgsnid;
1179         *ptr = tail;
1180
1181         return 0;
1182 }
1183
1184 /**
1185  * Find the first delimiter (comma or colon) from the specified \a buf and
1186  * make \a *endh point to the string starting with the delimiter. The commas
1187  * in expression list [...] will be skipped.
1188  *
1189  * @buf         a delimiter-separated string
1190  * @endh        a pointer to a pointer that will point to the string
1191  *              starting with the delimiter
1192  *
1193  * RETURNS      true if delimiter is found, false if delimiter is not found
1194  */
1195 static bool lmd_find_delimiter(char *buf, char **endh)
1196 {
1197         char *c = buf;
1198         size_t pos;
1199         bool found;
1200
1201         if (!buf)
1202                 return false;
1203 try_again:
1204         if (*c == ',' || *c == ':')
1205                 return true;
1206
1207         pos = strcspn(c, "[:,]");
1208         if (!pos)
1209                 return false;
1210
1211         /* Not a valid mount string */
1212         if (*c == ']') {
1213                 CWARN("invalid mount string format\n");
1214                 return false;
1215         }
1216
1217         c += pos;
1218         if (*c == '[') {
1219                 c = strchr(c, ']');
1220
1221                 /* invalid mount string */
1222                 if (!c) {
1223                         CWARN("invalid mount string format\n");
1224                         return false;
1225                 }
1226                 c++;
1227                 goto try_again;
1228         }
1229
1230         found = *c != '\0';
1231         if (found && endh)
1232                 *endh = c;
1233
1234         return found;
1235 }
1236
1237 /**
1238  * Find the first valid string delimited by comma or colon from the specified
1239  * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
1240  * will point to the next string starting with the delimiter.
1241  *
1242  * \param[in] buf       a delimiter-separated string
1243  * \param[in] endh      a pointer to a pointer that will point to the string
1244  *                      starting with the delimiter
1245  *
1246  * \retval 0            if the string is a valid NID list
1247  * \retval 1            if the string is not a valid NID list
1248  */
1249 static int lmd_parse_nidlist(char *buf, char **endh)
1250 {
1251         LIST_HEAD(nidlist);
1252         char *endp = buf;
1253         char tmp;
1254         int rc = 0;
1255
1256         if (buf == NULL)
1257                 return 1;
1258         while (*buf == ',' || *buf == ':')
1259                 buf++;
1260         if (*buf == ' ' || *buf == '/' || *buf == '\0')
1261                 return 1;
1262
1263         if (!lmd_find_delimiter(buf, &endp))
1264                 endp = buf + strlen(buf);
1265
1266         tmp = *endp;
1267         *endp = '\0';
1268
1269         if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1270                 rc = 1;
1271         cfs_free_nidlist(&nidlist);
1272
1273         *endp = tmp;
1274         if (rc != 0)
1275                 return rc;
1276         if (endh != NULL)
1277                 *endh = endp;
1278         return 0;
1279 }
1280
1281 /**
1282  * Parse mount line options
1283  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1284  * dev is passed as device=uml1:/lustre by mount.lustre_tgt
1285  */
1286 int lmd_parse(char *options, struct lustre_mount_data *lmd)
1287 {
1288         char *s1, *s2, *devname = NULL;
1289         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1290         int rc = 0;
1291
1292         ENTRY;
1293
1294         LASSERT(lmd);
1295         if (!options) {
1296                 LCONSOLE_ERROR_MSG(0x162,
1297                                    "Missing mount data: check /sbin/mount.lustre_tgt is installed.\n");
1298                 RETURN(-EINVAL);
1299         }
1300
1301         /* Options should be a string - try to detect old lmd data */
1302         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1303                 LCONSOLE_ERROR_MSG(0x163,
1304                                    "Using an old version of /sbin/mount.lustre. Please install version %s\n",
1305                                    LUSTRE_VERSION_STRING);
1306                 RETURN(-EINVAL);
1307         }
1308         lmd->lmd_magic = LMD_MAGIC;
1309
1310         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1311         if (lmd->lmd_params == NULL)
1312                 RETURN(-ENOMEM);
1313         lmd->lmd_params[0] = '\0';
1314
1315         /* Set default flags here */
1316
1317         s1 = options;
1318         while (*s1) {
1319                 int clear = 0;
1320                 int time_min = OBD_RECOVERY_TIME_MIN;
1321                 char *s3;
1322
1323                 /* Skip whitespace and extra commas */
1324                 while (*s1 == ' ' || *s1 == ',')
1325                         s1++;
1326                 s3 = s1;
1327
1328                 /*
1329                  * Client options are parsed in ll_options: eg. flock,
1330                  * user_xattr, acl
1331                  */
1332
1333                 /*
1334                  * Parse non-ldiskfs options here. Rather than modifying
1335                  * ldiskfs, we just zero these out here
1336                  */
1337                 if (!strncmp(s1, "abort_recov_mdt", 15) ||
1338                     !strncmp(s1, "abort_recovery_mdt", 18)) {
1339                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV_MDT;
1340                         clear++;
1341                 } else if (strncmp(s1, "abort_recov", 11) == 0) {
1342                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1343                         clear++;
1344                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1345                         lmd->lmd_recovery_time_soft =
1346                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1347                                       time_min);
1348                         clear++;
1349                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1350                         lmd->lmd_recovery_time_hard =
1351                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1352                                       time_min);
1353                         clear++;
1354                 } else if (strncmp(s1, "no_precreate", 12) == 0) {
1355                         lmd->lmd_flags |= LMD_FLG_NO_PRECREATE;
1356                         clear++;
1357                 } else if (strncmp(s1, "noir", 4) == 0) {
1358                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1359                         clear++;
1360                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1361                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1362                         clear++;
1363                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1364                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1365                         clear++;
1366                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1367                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1368                         clear++;
1369                 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1370                         lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1371                         clear++;
1372                 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1373                         lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1374                         clear++;
1375                 } else if (strncmp(s1, PARAM_MGSNODE,
1376                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1377                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1378                         /*
1379                          * Assume the next mount opt is the first
1380                          * invalid NID we get to.
1381                          */
1382                         rc = lmd_parse_mgs(lmd, &s2);
1383                         if (rc)
1384                                 goto invalid;
1385                         s3 = s2;
1386                         clear++;
1387                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1388                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1389                         clear++;
1390                 } else if (strncmp(s1, "nolocallogs", 11) == 0) {
1391                         lmd->lmd_flags |= LMD_FLG_NO_LOCAL_LOGS;
1392                         clear++;
1393                 } else if (strncmp(s1, "update", 6) == 0) {
1394                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1395                         clear++;
1396                 } else if (strncmp(s1, "virgin", 6) == 0) {
1397                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1398                         clear++;
1399                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1400                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1401                         clear++;
1402                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1403                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1404                         if (rc)
1405                                 goto invalid;
1406                         clear++;
1407                         /* ost exclusion list */
1408                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1409                         rc = lmd_make_exclusion(lmd, s1 + 7);
1410                         if (rc)
1411                                 goto invalid;
1412                         clear++;
1413                 } else if (strncmp(s1, "mgs", 3) == 0) {
1414                         /* We are an MGS */
1415                         lmd->lmd_flags |= LMD_FLG_MGS;
1416                         clear++;
1417                 } else if (strncmp(s1, "svname=", 7) == 0) {
1418                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1419                         if (rc)
1420                                 goto invalid;
1421                         clear++;
1422                 } else if (strncmp(s1, "param=", 6) == 0) {
1423                         size_t length, params_length;
1424                         char  *tail = s1;
1425
1426                         if (lmd_find_delimiter(s1 + 6, &tail)) {
1427                                 char *param_str = tail + 1;
1428                                 int   supplementary = 1;
1429
1430                                 while (lmd_parse_nidlist(param_str,
1431                                                          &param_str) == 0) {
1432                                         supplementary = 0;
1433                                 }
1434                                 length = param_str - s1 - supplementary;
1435                         } else {
1436                                 length = strlen(s1);
1437                         }
1438                         length -= 6;
1439                         params_length = strlen(lmd->lmd_params);
1440                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1441                                 RETURN(-E2BIG);
1442                         strncat(lmd->lmd_params, s1 + 6, length);
1443                         lmd->lmd_params[params_length + length] = '\0';
1444                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1445                         s3 = s1 + 6 + length;
1446                         clear++;
1447                 } else if (strncmp(s1, "localrecov", 10) == 0) {
1448                         lmd->lmd_flags |= LMD_FLG_LOCAL_RECOV;
1449                         clear++;
1450                 } else if (strncmp(s1, "osd=", 4) == 0) {
1451                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1452                         if (rc)
1453                                 goto invalid;
1454                         clear++;
1455                 }
1456                 /*
1457                  * Linux 2.4 doesn't pass the device, so we stuck it at
1458                  * the end of the options.
1459                  */
1460                 else if (strncmp(s1, "device=", 7) == 0) {
1461                         devname = s1 + 7;
1462                         /*
1463                          * terminate options right before device.  device
1464                          * must be the last one.
1465                          */
1466                         *s1 = '\0';
1467                         break;
1468                 } else if (strncmp(s1, "network=", 8) == 0) {
1469                         rc = lmd_parse_network(lmd, s1 + 8);
1470                         if (rc)
1471                                 goto invalid;
1472
1473                         /* check if LNet dynamic peer discovery is activated */
1474                         if (LNetGetPeerDiscoveryStatus()) {
1475                                 CERROR("LNet Dynamic Peer Discovery is enabled "
1476                                        "on this node. 'network' mount option "
1477                                        "cannot be taken into account.\n");
1478                                 goto invalid;
1479                         }
1480
1481                         clear++;
1482                 }
1483
1484                 /* Find next opt */
1485                 s2 = strchr(s3, ',');
1486                 if (s2 == NULL) {
1487                         if (clear)
1488                                 *s1 = '\0';
1489                         break;
1490                 }
1491                 s2++;
1492                 if (clear)
1493                         memmove(s1, s2, strlen(s2) + 1);
1494                 else
1495                         s1 = s2;
1496         }
1497
1498         if (!devname) {
1499                 LCONSOLE_ERROR_MSG(0x164,
1500                                    "Can't find device name (need mount option 'device=...')\n");
1501                 goto invalid;
1502         }
1503
1504         s1 = strstr(devname, ":/");
1505         if (s1) {
1506                 ++s1;
1507                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1508                 /* Remove leading /s from fsname */
1509                 while (*++s1 == '/')
1510                         ;
1511                 s2 = s1;
1512                 while (*s2 != '/' && *s2 != '\0')
1513                         s2++;
1514                 /* Freed in lustre_free_lsi */
1515                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1516                 if (!lmd->lmd_profile)
1517                         RETURN(-ENOMEM);
1518
1519                 strncat(lmd->lmd_profile, s1, s2 - s1);
1520                 strncat(lmd->lmd_profile, "-client", 7);
1521
1522                 s1 = s2;
1523                 s2 = s1 + strlen(s1) - 1;
1524                 /* Remove padding /s from fileset */
1525                 while (*s2 == '/')
1526                         s2--;
1527                 if (s2 > s1) {
1528                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1529                         if (lmd->lmd_fileset == NULL) {
1530                                 OBD_FREE(lmd->lmd_profile,
1531                                          strlen(lmd->lmd_profile) + 1);
1532                                 RETURN(-ENOMEM);
1533                         }
1534                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1535                 }
1536         } else {
1537                 /* server mount */
1538                 if (lmd->lmd_nidnet != NULL) {
1539                         /* 'network=' mount option forbidden for server */
1540                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1541                         lmd->lmd_nidnet = NULL;
1542                         rc = -EINVAL;
1543                         CERROR(
1544                                "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1545                                devname, rc);
1546                         RETURN(rc);
1547                 }
1548         }
1549
1550         /* Freed in lustre_free_lsi */
1551         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1552         if (!lmd->lmd_dev)
1553                 RETURN(-ENOMEM);
1554         strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1555
1556         /* Save mount options */
1557         s1 = options + strlen(options) - 1;
1558         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1559                 *s1-- = 0;
1560         while (*options && (*options == ',' || *options == ' '))
1561                 options++;
1562         if (*options != 0) {
1563                 /* Freed in lustre_free_lsi */
1564                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1565                 if (!lmd->lmd_opts)
1566                         RETURN(-ENOMEM);
1567                 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1568         }
1569
1570         lmd_print(lmd);
1571         lmd->lmd_magic = LMD_MAGIC;
1572
1573         RETURN(rc);
1574
1575 invalid:
1576         CERROR("Bad mount options %s\n", options);
1577         RETURN(-EINVAL);
1578 }
1579 EXPORT_SYMBOL(lmd_parse);