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