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