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