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