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