Whamcloud - gitweb
LU-12514 obdclass: Drop FS_HAS_FIEMAP compat macro
[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         spin_lock_init(&lsi->lsi_lwp_lock);
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         return rc;
897 }
898 EXPORT_SYMBOL(target_name2index);
899
900 /*
901  * Get the index from the OBD name.
902  * rc = server type, or
903  * rc < 0  on error
904  * if endptr isn't NULL it is set to end of name
905  */
906 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
907 {
908         const char *dash;
909         int rc;
910
911         /* We use server_name2fsname() just for parsing */
912         rc = server_name2fsname(svname, NULL, &dash);
913         if (rc != 0)
914                 return rc;
915
916         dash++;
917         rc = target_name2index(dash, idx, endptr);
918         if (rc < 0)
919                 return rc;
920
921         /* Account for -mdc after index that is possible when specifying mdt */
922         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
923                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
924                 *endptr += sizeof(LUSTRE_MDC_NAME);
925
926         return rc;
927 }
928 EXPORT_SYMBOL(server_name2index);
929
930 /*************** mount common betweeen server and client ***************/
931
932 /* Common umount */
933 int lustre_common_put_super(struct super_block *sb)
934 {
935         int rc;
936
937         ENTRY;
938
939         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
940
941         /* Drop a ref to the MGC */
942         rc = lustre_stop_mgc(sb);
943         if (rc && (rc != -ENOENT)) {
944                 if (rc != -EBUSY) {
945                         CERROR("Can't stop MGC: %d\n", rc);
946                         RETURN(rc);
947                 }
948                 /*
949                  * BUSY just means that there's some other OBD that
950                  * needs the mgc.  Let him clean it up.
951                  */
952                 CDEBUG(D_MOUNT, "MGC still in use\n");
953         }
954         /* Drop a ref to the mounted disk */
955         lustre_put_lsi(sb);
956
957         RETURN(rc);
958 }
959 EXPORT_SYMBOL(lustre_common_put_super);
960
961 static void lmd_print(struct lustre_mount_data *lmd)
962 {
963         int i;
964
965         PRINT_CMD(D_MOUNT, "  mount data:\n");
966         if (lmd_is_client(lmd))
967                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
968         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
969         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
970
971         if (lmd->lmd_opts)
972                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
973
974         if (lmd->lmd_recovery_time_soft)
975                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
976                           lmd->lmd_recovery_time_soft);
977
978         if (lmd->lmd_recovery_time_hard)
979                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
980                           lmd->lmd_recovery_time_hard);
981
982         for (i = 0; i < lmd->lmd_exclude_count; i++) {
983                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
984                           lmd->lmd_exclude[i]);
985         }
986 }
987
988 /* Is this server on the exclusion list */
989 int lustre_check_exclusion(struct super_block *sb, char *svname)
990 {
991         struct lustre_sb_info *lsi = s2lsi(sb);
992         struct lustre_mount_data *lmd = lsi->lsi_lmd;
993         __u32 index;
994         int i, rc;
995
996         ENTRY;
997
998         rc = server_name2index(svname, &index, NULL);
999         if (rc != LDD_F_SV_TYPE_OST)
1000                 /* Only exclude OSTs */
1001                 RETURN(0);
1002
1003         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1004                index, lmd->lmd_exclude_count, lmd->lmd_dev);
1005
1006         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1007                 if (index == lmd->lmd_exclude[i]) {
1008                         CWARN("Excluding %s (on exclusion list)\n", svname);
1009                         RETURN(1);
1010                 }
1011         }
1012         RETURN(0);
1013 }
1014
1015 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1016 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1017 {
1018         const char *s1 = ptr, *s2;
1019         __u32 *exclude_list;
1020         __u32 index = 0;
1021         int rc = 0, devmax;
1022
1023         ENTRY;
1024
1025         /*
1026          * The shortest an ost name can be is 8 chars: -OST0000.
1027          * We don't actually know the fsname at this time, so in fact
1028          * a user could specify any fsname.
1029          */
1030         devmax = strlen(ptr) / 8 + 1;
1031
1032         /* temp storage until we figure out how many we have */
1033         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
1034         if (!exclude_list)
1035                 RETURN(-ENOMEM);
1036
1037         /* we enter this fn pointing at the '=' */
1038         while (*s1 && *s1 != ' ' && *s1 != ',') {
1039                 s1++;
1040                 rc = server_name2index(s1, &index, &s2);
1041                 if (rc < 0) {
1042                         CERROR("Can't parse server name '%s': rc = %d\n",
1043                                s1, rc);
1044                         break;
1045                 }
1046                 if (rc == LDD_F_SV_TYPE_OST)
1047                         exclude_list[lmd->lmd_exclude_count++] = index;
1048                 else
1049                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1050                                (uint)(s2-s1), s1, rc);
1051                 s1 = s2;
1052                 /*
1053                  * now we are pointing at ':' (next exclude)
1054                  * or ',' (end of excludes)
1055                  */
1056                 if (lmd->lmd_exclude_count >= devmax)
1057                         break;
1058         }
1059         if (rc >= 0) /* non-err */
1060                 rc = 0;
1061
1062         if (lmd->lmd_exclude_count) {
1063                 /* permanent, freed in lustre_free_lsi */
1064                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
1065                           lmd->lmd_exclude_count);
1066                 if (lmd->lmd_exclude) {
1067                         memcpy(lmd->lmd_exclude, exclude_list,
1068                                sizeof(index) * lmd->lmd_exclude_count);
1069                 } else {
1070                         rc = -ENOMEM;
1071                         lmd->lmd_exclude_count = 0;
1072                 }
1073         }
1074         OBD_FREE(exclude_list, sizeof(index) * devmax);
1075         RETURN(rc);
1076 }
1077
1078 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1079 {
1080         char *tail;
1081         int length;
1082
1083         if (lmd->lmd_mgssec != NULL) {
1084                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1085                 lmd->lmd_mgssec = NULL;
1086         }
1087
1088         tail = strchr(ptr, ',');
1089         if (tail == NULL)
1090                 length = strlen(ptr);
1091         else
1092                 length = tail - ptr;
1093
1094         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1095         if (lmd->lmd_mgssec == NULL)
1096                 return -ENOMEM;
1097
1098         memcpy(lmd->lmd_mgssec, ptr, length);
1099         lmd->lmd_mgssec[length] = '\0';
1100         return 0;
1101 }
1102
1103 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1104 {
1105         char *tail;
1106         int length;
1107
1108         if (lmd->lmd_nidnet != NULL) {
1109                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1110                 lmd->lmd_nidnet = NULL;
1111         }
1112
1113         tail = strchr(ptr, ',');
1114         if (tail == NULL)
1115                 length = strlen(ptr);
1116         else
1117                 length = tail - ptr;
1118
1119         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1120         if (lmd->lmd_nidnet == NULL)
1121                 return -ENOMEM;
1122
1123         memcpy(lmd->lmd_nidnet, ptr, length);
1124         lmd->lmd_nidnet[length] = '\0';
1125         return 0;
1126 }
1127
1128 static int lmd_parse_string(char **handle, char *ptr)
1129 {
1130         char *tail;
1131         int length;
1132
1133         if ((handle == NULL) || (ptr == NULL))
1134                 return -EINVAL;
1135
1136         if (*handle != NULL) {
1137                 OBD_FREE(*handle, strlen(*handle) + 1);
1138                 *handle = NULL;
1139         }
1140
1141         tail = strchr(ptr, ',');
1142         if (tail == NULL)
1143                 length = strlen(ptr);
1144         else
1145                 length = tail - ptr;
1146
1147         OBD_ALLOC(*handle, length + 1);
1148         if (*handle == NULL)
1149                 return -ENOMEM;
1150
1151         memcpy(*handle, ptr, length);
1152         (*handle)[length] = '\0';
1153
1154         return 0;
1155 }
1156
1157 /* Collect multiple values for mgsnid specifiers */
1158 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1159 {
1160         lnet_nid_t nid;
1161         char *tail = *ptr;
1162         char *mgsnid;
1163         int length;
1164         int oldlen = 0;
1165
1166         /* Find end of NID-list */
1167         while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
1168                 ; /* do nothing */
1169
1170         length = tail - *ptr;
1171         if (length == 0) {
1172                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1173                 return -EINVAL;
1174         }
1175
1176         if (lmd->lmd_mgs != NULL)
1177                 oldlen = strlen(lmd->lmd_mgs) + 1;
1178
1179         OBD_ALLOC(mgsnid, oldlen + length + 1);
1180         if (mgsnid == NULL)
1181                 return -ENOMEM;
1182
1183         if (lmd->lmd_mgs != NULL) {
1184                 /* Multiple mgsnid= are taken to mean failover locations */
1185                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1186                 mgsnid[oldlen - 1] = ':';
1187                 OBD_FREE(lmd->lmd_mgs, oldlen);
1188         }
1189         memcpy(mgsnid + oldlen, *ptr, length);
1190         mgsnid[oldlen + length] = '\0';
1191         lmd->lmd_mgs = mgsnid;
1192         *ptr = tail;
1193
1194         return 0;
1195 }
1196
1197 /**
1198  * Find the first delimiter (comma or colon) from the specified \a buf and
1199  * make \a *endh point to the string starting with the delimiter. The commas
1200  * in expression list [...] will be skipped.
1201  *
1202  * @buf         a delimiter-separated string
1203  * @endh        a pointer to a pointer that will point to the string
1204  *              starting with the delimiter
1205  *
1206  * RETURNS      true if delimiter is found, false if delimiter is not found
1207  */
1208 static bool lmd_find_delimiter(char *buf, char **endh)
1209 {
1210         char *c = buf;
1211         size_t pos;
1212         bool found;
1213
1214         if (!buf)
1215                 return false;
1216 try_again:
1217         if (*c == ',' || *c == ':')
1218                 return true;
1219
1220         pos = strcspn(c, "[:,]");
1221         if (!pos)
1222                 return false;
1223
1224         /* Not a valid mount string */
1225         if (*c == ']') {
1226                 CWARN("invalid mount string format\n");
1227                 return false;
1228         }
1229
1230         c += pos;
1231         if (*c == '[') {
1232                 c = strchr(c, ']');
1233
1234                 /* invalid mount string */
1235                 if (!c) {
1236                         CWARN("invalid mount string format\n");
1237                         return false;
1238                 }
1239                 c++;
1240                 goto try_again;
1241         }
1242
1243         found = *c != '\0';
1244         if (found && endh)
1245                 *endh = c;
1246
1247         return found;
1248 }
1249
1250 /**
1251  * Find the first valid string delimited by comma or colon from the specified
1252  * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
1253  * will point to the next string starting with the delimiter.
1254  *
1255  * \param[in] buf       a delimiter-separated string
1256  * \param[in] endh      a pointer to a pointer that will point to the string
1257  *                      starting with the delimiter
1258  *
1259  * \retval 0            if the string is a valid NID list
1260  * \retval 1            if the string is not a valid NID list
1261  */
1262 static int lmd_parse_nidlist(char *buf, char **endh)
1263 {
1264         struct list_head nidlist;
1265         char *endp = buf;
1266         char tmp;
1267         int rc = 0;
1268
1269         if (buf == NULL)
1270                 return 1;
1271         while (*buf == ',' || *buf == ':')
1272                 buf++;
1273         if (*buf == ' ' || *buf == '/' || *buf == '\0')
1274                 return 1;
1275
1276         if (!lmd_find_delimiter(buf, &endp))
1277                 endp = buf + strlen(buf);
1278
1279         tmp = *endp;
1280         *endp = '\0';
1281
1282         INIT_LIST_HEAD(&nidlist);
1283         if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1284                 rc = 1;
1285         cfs_free_nidlist(&nidlist);
1286
1287         *endp = tmp;
1288         if (rc != 0)
1289                 return rc;
1290         if (endh != NULL)
1291                 *endh = endp;
1292         return 0;
1293 }
1294
1295 /**
1296  * Parse mount line options
1297  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1298  * dev is passed as device=uml1:/lustre by mount.lustre
1299  */
1300 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1301 {
1302         char *s1, *s2, *devname = NULL;
1303         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1304         int rc = 0;
1305
1306         ENTRY;
1307
1308         LASSERT(lmd);
1309         if (!options) {
1310                 LCONSOLE_ERROR_MSG(0x162,
1311                                    "Missing mount data: check /sbin/mount.lustre is installed.\n");
1312                 RETURN(-EINVAL);
1313         }
1314
1315         /* Options should be a string - try to detect old lmd data */
1316         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1317                 LCONSOLE_ERROR_MSG(0x163,
1318                                    "Using an old version of /sbin/mount lustre. Please install version %s\n",
1319                                    LUSTRE_VERSION_STRING);
1320                 RETURN(-EINVAL);
1321         }
1322         lmd->lmd_magic = LMD_MAGIC;
1323
1324         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1325         if (lmd->lmd_params == NULL)
1326                 RETURN(-ENOMEM);
1327         lmd->lmd_params[0] = '\0';
1328
1329         /* Set default flags here */
1330
1331         s1 = options;
1332         while (*s1) {
1333                 int clear = 0;
1334                 int time_min = OBD_RECOVERY_TIME_MIN;
1335                 char *s3;
1336
1337                 /* Skip whitespace and extra commas */
1338                 while (*s1 == ' ' || *s1 == ',')
1339                         s1++;
1340                 s3 = s1;
1341
1342                 /*
1343                  * Client options are parsed in ll_options: eg. flock,
1344                  * user_xattr, acl
1345                  */
1346
1347                 /*
1348                  * Parse non-ldiskfs options here. Rather than modifying
1349                  * ldiskfs, we just zero these out here
1350                  */
1351                 if (strncmp(s1, "abort_recov", 11) == 0) {
1352                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1353                         clear++;
1354                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1355                         lmd->lmd_recovery_time_soft =
1356                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1357                                       time_min);
1358                         clear++;
1359                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1360                         lmd->lmd_recovery_time_hard =
1361                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1362                                       time_min);
1363                         clear++;
1364                 } else if (strncmp(s1, "noir", 4) == 0) {
1365                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1366                         clear++;
1367                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1368                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1369                         clear++;
1370                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1371                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1372                         clear++;
1373                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1374                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1375                         clear++;
1376                 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1377                         lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1378                         clear++;
1379                 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1380                         lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1381                         clear++;
1382                 } else if (strncmp(s1, PARAM_MGSNODE,
1383                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1384                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1385                         /*
1386                          * Assume the next mount opt is the first
1387                          * invalid NID we get to.
1388                          */
1389                         rc = lmd_parse_mgs(lmd, &s2);
1390                         if (rc)
1391                                 goto invalid;
1392                         s3 = s2;
1393                         clear++;
1394                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1395                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1396                         clear++;
1397                 } else if (strncmp(s1, "update", 6) == 0) {
1398                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1399                         clear++;
1400                 } else if (strncmp(s1, "virgin", 6) == 0) {
1401                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1402                         clear++;
1403                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1404                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1405                         clear++;
1406                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1407                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1408                         if (rc)
1409                                 goto invalid;
1410                         clear++;
1411                         /* ost exclusion list */
1412                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1413                         rc = lmd_make_exclusion(lmd, s1 + 7);
1414                         if (rc)
1415                                 goto invalid;
1416                         clear++;
1417                 } else if (strncmp(s1, "mgs", 3) == 0) {
1418                         /* We are an MGS */
1419                         lmd->lmd_flags |= LMD_FLG_MGS;
1420                         clear++;
1421                 } else if (strncmp(s1, "svname=", 7) == 0) {
1422                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1423                         if (rc)
1424                                 goto invalid;
1425                         clear++;
1426                 } else if (strncmp(s1, "param=", 6) == 0) {
1427                         size_t length, params_length;
1428                         char  *tail = s1;
1429
1430                         if (lmd_find_delimiter(s1 + 6, &tail)) {
1431                                 char *param_str = tail + 1;
1432                                 int   supplementary = 1;
1433
1434                                 while (lmd_parse_nidlist(param_str,
1435                                                          &param_str) == 0) {
1436                                         supplementary = 0;
1437                                 }
1438                                 length = param_str - s1 - supplementary;
1439                         } else {
1440                                 length = strlen(s1);
1441                         }
1442                         length -= 6;
1443                         params_length = strlen(lmd->lmd_params);
1444                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1445                                 RETURN(-E2BIG);
1446                         strncat(lmd->lmd_params, s1 + 6, length);
1447                         lmd->lmd_params[params_length + length] = '\0';
1448                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1449                         s3 = s1 + 6 + length;
1450                         clear++;
1451                 } else if (strncmp(s1, "osd=", 4) == 0) {
1452                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1453                         if (rc)
1454                                 goto invalid;
1455                         clear++;
1456                 }
1457                 /*
1458                  * Linux 2.4 doesn't pass the device, so we stuck it at
1459                  * the end of the options.
1460                  */
1461                 else if (strncmp(s1, "device=", 7) == 0) {
1462                         devname = s1 + 7;
1463                         /*
1464                          * terminate options right before device.  device
1465                          * must be the last one.
1466                          */
1467                         *s1 = '\0';
1468                         break;
1469                 } else if (strncmp(s1, "network=", 8) == 0) {
1470                         rc = lmd_parse_network(lmd, s1 + 8);
1471                         if (rc)
1472                                 goto invalid;
1473
1474                         /* check if LNet dynamic peer discovery is activated */
1475                         if (LNetGetPeerDiscoveryStatus()) {
1476                                 CERROR("LNet Dynamic Peer Discovery is enabled "
1477                                        "on this node. 'network' mount option "
1478                                        "cannot be taken into account.\n");
1479                                 goto invalid;
1480                         }
1481
1482                         clear++;
1483                 }
1484
1485                 /* Find next opt */
1486                 s2 = strchr(s3, ',');
1487                 if (s2 == NULL) {
1488                         if (clear)
1489                                 *s1 = '\0';
1490                         break;
1491                 }
1492                 s2++;
1493                 if (clear)
1494                         memmove(s1, s2, strlen(s2) + 1);
1495                 else
1496                         s1 = s2;
1497         }
1498
1499         if (!devname) {
1500                 LCONSOLE_ERROR_MSG(0x164,
1501                                    "Can't find device name (need mount option 'device=...')\n");
1502                 goto invalid;
1503         }
1504
1505         s1 = strstr(devname, ":/");
1506         if (s1) {
1507                 ++s1;
1508                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1509                 /* Remove leading /s from fsname */
1510                 while (*++s1 == '/')
1511                         ;
1512                 s2 = s1;
1513                 while (*s2 != '/' && *s2 != '\0')
1514                         s2++;
1515                 /* Freed in lustre_free_lsi */
1516                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1517                 if (!lmd->lmd_profile)
1518                         RETURN(-ENOMEM);
1519
1520                 strncat(lmd->lmd_profile, s1, s2 - s1);
1521                 strncat(lmd->lmd_profile, "-client", 7);
1522
1523                 s1 = s2;
1524                 s2 = s1 + strlen(s1) - 1;
1525                 /* Remove padding /s from fileset */
1526                 while (*s2 == '/')
1527                         s2--;
1528                 if (s2 > s1) {
1529                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1530                         if (lmd->lmd_fileset == NULL) {
1531                                 OBD_FREE(lmd->lmd_profile,
1532                                          strlen(lmd->lmd_profile) + 1);
1533                                 RETURN(-ENOMEM);
1534                         }
1535                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1536                 }
1537         } else {
1538                 /* server mount */
1539                 if (lmd->lmd_nidnet != NULL) {
1540                         /* 'network=' mount option forbidden for server */
1541                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1542                         lmd->lmd_nidnet = NULL;
1543                         rc = -EINVAL;
1544                         CERROR(
1545                                "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1546                                devname, rc);
1547                         RETURN(rc);
1548                 }
1549         }
1550
1551         /* Freed in lustre_free_lsi */
1552         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1553         if (!lmd->lmd_dev)
1554                 RETURN(-ENOMEM);
1555         strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1556
1557         /* Save mount options */
1558         s1 = options + strlen(options) - 1;
1559         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1560                 *s1-- = 0;
1561         while (*options && (*options == ',' || *options == ' '))
1562                 options++;
1563         if (*options != 0) {
1564                 /* Freed in lustre_free_lsi */
1565                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1566                 if (!lmd->lmd_opts)
1567                         RETURN(-ENOMEM);
1568                 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1569         }
1570
1571         lmd_print(lmd);
1572         lmd->lmd_magic = LMD_MAGIC;
1573
1574         RETURN(rc);
1575
1576 invalid:
1577         CERROR("Bad mount options %s\n", options);
1578         RETURN(-EINVAL);
1579 }
1580
1581 struct lustre_mount_data2 {
1582         void *lmd2_data;
1583         struct vfsmount *lmd2_mnt;
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 *data, int silent)
1593 {
1594         struct lustre_mount_data *lmd;
1595         struct lustre_mount_data2 *lmd2 = data;
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((char *)(lmd2->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, lmd2->lmd2_mnt);
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                                                   struct vfsmount *mnt))
1685 {
1686         client_fill_super = cfs;
1687 }
1688 EXPORT_SYMBOL(lustre_register_client_fill_super);
1689
1690 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1691 {
1692         kill_super_cb = cfs;
1693 }
1694 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1695
1696 /***************** FS registration ******************/
1697 #ifdef HAVE_FSTYPE_MOUNT
1698 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1699                                    const char *devname, void *data)
1700 {
1701         struct lustre_mount_data2 lmd2 = {
1702                 .lmd2_data = data,
1703         };
1704
1705         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1706 }
1707 #else
1708 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1709                          const char *devname, void *data, struct vfsmount *mnt)
1710 {
1711         struct lustre_mount_data2 lmd2 = {
1712                 .lmd2_data = data,
1713                 .lmd2_mnt = mnt,
1714         };
1715
1716         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1717 }
1718 #endif
1719
1720 static void lustre_kill_super(struct super_block *sb)
1721 {
1722         struct lustre_sb_info *lsi = s2lsi(sb);
1723
1724         if (kill_super_cb && lsi && !IS_SERVER(lsi))
1725                 (*kill_super_cb)(sb);
1726
1727         kill_anon_super(sb);
1728 }
1729
1730 /* Register the "lustre" fs type */
1731 static struct file_system_type lustre_fs_type = {
1732         .owner        = THIS_MODULE,
1733         .name         = "lustre",
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");
1743
1744 int lustre_register_fs(void)
1745 {
1746         return register_filesystem(&lustre_fs_type);
1747 }
1748
1749 int lustre_unregister_fs(void)
1750 {
1751         return unregister_filesystem(&lustre_fs_type);
1752 }