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