Whamcloud - gitweb
LU-5396 obdclass: make some functions static
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/obd_mount.c
37  *
38  * Client mount routines
39  *
40  * Author: Nathan Rutman <nathan@clusterfs.com>
41  */
42
43
44 #define DEBUG_SUBSYSTEM S_CLASS
45 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
46 #define PRINT_CMD CDEBUG
47
48 #include <obd.h>
49 #include <obd_class.h>
50 #include <lustre/lustre_user.h>
51 #include <linux/version.h>
52 #include <lustre_log.h>
53 #include <lustre_disk.h>
54 #include <lustre_param.h>
55
56 static int (*client_fill_super)(struct super_block *sb,
57                                 struct vfsmount *mnt);
58
59 static void (*kill_super_cb)(struct super_block *sb);
60
61 /**************** config llog ********************/
62
63 /** Get a config log from the MGS and process it.
64  * This func is called for both clients and servers.
65  * Continue to process new statements appended to the logs
66  * (whenever the config lock is revoked) until lustre_end_log
67  * is called.
68  * @param sb The superblock is used by the MGC to write to the local copy of
69  *   the config log
70  * @param logname The name of the llog to replicate from the MGS
71  * @param cfg Since the same mgc may be used to follow multiple config logs
72  *   (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
73  *   this log, and is added to the mgc's list of logs to follow.
74  */
75 int lustre_process_log(struct super_block *sb, char *logname,
76                      struct config_llog_instance *cfg)
77 {
78         struct lustre_cfg *lcfg;
79         struct lustre_cfg_bufs *bufs;
80         struct lustre_sb_info *lsi = s2lsi(sb);
81         struct obd_device *mgc = lsi->lsi_mgc;
82         int rc;
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         lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
98         if (lcfg == NULL)
99                 GOTO(out, rc = -ENOMEM);
100         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
101         lustre_cfg_free(lcfg);
102 out:
103         OBD_FREE_PTR(bufs);
104
105         if (rc == -EINVAL)
106                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
107                                    "failed from the MGS (%d).  Make sure this "
108                                    "client and the MGS are running compatible "
109                                    "versions of Lustre.\n",
110                                    mgc->obd_name, logname, rc);
111         else if (rc != 0)
112                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
113                                    "failed (%d). This may be the result of "
114                                    "communication errors between this node and "
115                                    "the MGS, a bad configuration, or other "
116                                    "errors. See the syslog for more "
117                                    "information.\n", mgc->obd_name, logname,
118                                    rc);
119
120         /* class_obd_list(); */
121         RETURN(rc);
122 }
123 EXPORT_SYMBOL(lustre_process_log);
124
125 /* Stop watching this config log for updates */
126 int lustre_end_log(struct super_block *sb, char *logname,
127                        struct config_llog_instance *cfg)
128 {
129         struct lustre_cfg *lcfg;
130         struct lustre_cfg_bufs bufs;
131         struct lustre_sb_info *lsi = s2lsi(sb);
132         struct obd_device *mgc = lsi->lsi_mgc;
133         int rc;
134         ENTRY;
135
136         if (!mgc)
137                 RETURN(-ENOENT);
138
139         /* mgc_process_config */
140         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
141         lustre_cfg_bufs_set_string(&bufs, 1, logname);
142         if (cfg)
143                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
144         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
145         if (lcfg == NULL)
146                 RETURN(-ENOMEM);
147         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
148         lustre_cfg_free(lcfg);
149         RETURN(rc);
150 }
151 EXPORT_SYMBOL(lustre_end_log);
152
153 /**************** obd start *******************/
154
155 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
156  * lctl (and do for echo cli/srv.
157  */
158 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
159             char *s1, char *s2, char *s3, char *s4)
160 {
161         struct lustre_cfg_bufs bufs;
162         struct lustre_cfg    * lcfg = NULL;
163         int rc;
164
165         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
166                cmd, s1, s2, s3, s4);
167
168         lustre_cfg_bufs_reset(&bufs, cfgname);
169         if (s1)
170                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
171         if (s2)
172                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
173         if (s3)
174                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
175         if (s4)
176                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
177
178         lcfg = lustre_cfg_new(cmd, &bufs);
179         if (lcfg == NULL)
180                 return -ENOMEM;
181         lcfg->lcfg_nid = nid;
182         rc = class_process_config(lcfg);
183         lustre_cfg_free(lcfg);
184         return(rc);
185 }
186 EXPORT_SYMBOL(do_lcfg);
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         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
196
197         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, 0, 0);
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, 0, 0, 0, 0);
206         }
207         return rc;
208 }
209
210 static DEFINE_MUTEX(mgc_start_lock);
211
212 /** Set up a mgc obd to process startup logs
213  *
214  * \param sb [in] super block of the mgc obd
215  *
216  * \retval 0 success, otherwise error code
217  */
218 int lustre_start_mgc(struct super_block *sb)
219 {
220         struct obd_connect_data *data = NULL;
221         struct lustre_sb_info *lsi = s2lsi(sb);
222         struct obd_device *obd;
223         struct obd_export *exp;
224         struct obd_uuid *uuid;
225         class_uuid_t uuidc;
226         lnet_nid_t nid;
227         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
228         char *ptr;
229         int rc = 0, i = 0, j, len;
230         ENTRY;
231
232         LASSERT(lsi->lsi_lmd);
233
234         /* Find the first non-lo MGS nid for our MGC name */
235         if (IS_SERVER(lsi)) {
236                 /* mount -o mgsnode=nid */
237                 ptr = lsi->lsi_lmd->lmd_mgs;
238                 if (lsi->lsi_lmd->lmd_mgs &&
239                     (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
240                         i++;
241                 } else if (IS_MGS(lsi)) {
242                         lnet_process_id_t id;
243                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
244                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
245                                         continue;
246                                 nid = id.nid;
247                                 i++;
248                                 break;
249                         }
250                 }
251         } else { /* client */
252                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
253                 ptr = lsi->lsi_lmd->lmd_dev;
254                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
255                         i++;
256         }
257         if (i == 0) {
258                 CERROR("No valid MGS nids found.\n");
259                 RETURN(-EINVAL);
260         }
261
262         mutex_lock(&mgc_start_lock);
263
264         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(libcfs_nid2str(nid)) + 1;
265         OBD_ALLOC(mgcname, len);
266         OBD_ALLOC(niduuid, len + 2);
267         if (!mgcname || !niduuid)
268                 GOTO(out_free, rc = -ENOMEM);
269         sprintf(mgcname, "%s%s", LUSTRE_MGC_OBDNAME, libcfs_nid2str(nid));
270
271         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
272
273         OBD_ALLOC_PTR(data);
274         if (data == NULL)
275                 GOTO(out_free, rc = -ENOMEM);
276
277         obd = class_name2obd(mgcname);
278         if (obd && !obd->obd_stopping) {
279                 int recov_bk;
280
281                 rc = obd_set_info_async(NULL, obd->obd_self_export,
282                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
283                                         strlen(mgssec), mgssec, NULL);
284                 if (rc)
285                         GOTO(out_free, rc);
286
287                 /* Re-using an existing MGC */
288                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
289
290                 /* IR compatibility check, only for clients */
291                 if (lmd_is_client(lsi->lsi_lmd)) {
292                         int has_ir;
293                         int vallen = sizeof(*data);
294                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
295
296                         rc = obd_get_info(NULL, obd->obd_self_export,
297                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
298                                           &vallen, data, NULL);
299                         LASSERT(rc == 0);
300                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
301                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
302                                 /* LMD_FLG_NOIR is for test purpose only */
303                                 LCONSOLE_WARN(
304                                     "Trying to mount a client with IR setting "
305                                     "not compatible with current mgc. "
306                                     "Force to use current mgc setting that is "
307                                     "IR %s.\n",
308                                     has_ir ? "enabled" : "disabled");
309                                 if (has_ir)
310                                         *flags &= ~LMD_FLG_NOIR;
311                                 else
312                                         *flags |= LMD_FLG_NOIR;
313                         }
314                 }
315
316                 recov_bk = 0;
317                 /* If we are restarting the MGS, don't try to keep the MGC's
318                    old connection, or registration will fail. */
319                 if (IS_MGS(lsi)) {
320                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
321                         recov_bk = 1;
322                 }
323
324                 /* Try all connections, but only once (again).
325                    We don't want to block another target from starting
326                    (using its local copy of the log), but we do want to connect
327                    if at all possible. */
328                 recov_bk++;
329                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
330                 rc = obd_set_info_async(NULL, obd->obd_self_export,
331                                         sizeof(KEY_INIT_RECOV_BACKUP),
332                                         KEY_INIT_RECOV_BACKUP,
333                                         sizeof(recov_bk), &recov_bk, NULL);
334                 GOTO(out, rc = 0);
335         }
336
337         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
338
339         /* Add the primary nids for the MGS */
340         i = 0;
341         sprintf(niduuid, "%s_%x", mgcname, i);
342         if (IS_SERVER(lsi)) {
343                 ptr = lsi->lsi_lmd->lmd_mgs;
344                 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
345                 if (IS_MGS(lsi)) {
346                         /* Use local nids (including LO) */
347                         lnet_process_id_t id;
348                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
349                                 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
350                                              niduuid, 0, 0, 0);
351                         }
352                 } else {
353                         /* Use mgsnode= nids */
354                         /* mount -o mgsnode=nid */
355                         if (lsi->lsi_lmd->lmd_mgs) {
356                                 ptr = lsi->lsi_lmd->lmd_mgs;
357                         } else if (class_find_param(ptr, PARAM_MGSNODE,
358                                                     &ptr) != 0) {
359                                 CERROR("No MGS nids given.\n");
360                                 GOTO(out_free, rc = -EINVAL);
361                         }
362                         /*
363                          * LU-3829.
364                          * Here we only take the first mgsnid as its primary
365                          * serving mgs node, the rest mgsnid will be taken as
366                          * failover mgs node, otherwise they would be takens
367                          * as multiple nids of a single mgs node.
368                          */
369                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
370                                 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
371                                              niduuid, 0, 0, 0);
372                                 if (rc == 0) {
373                                         i = 1;
374                                         break;
375                                 }
376                         }
377                 }
378         } else { /* client */
379                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
380                 ptr = lsi->lsi_lmd->lmd_dev;
381                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
382                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
383                                      niduuid, 0, 0, 0);
384                         if (rc == 0)
385                                 ++i;
386                         /* Stop at the first failover nid */
387                         if (*ptr == ':')
388                                 break;
389                 }
390         }
391         if (i == 0) {
392                 CERROR("No valid MGS nids found.\n");
393                 GOTO(out_free, rc = -EINVAL);
394         }
395         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
396
397         /* Random uuid for MGC allows easier reconnects */
398         OBD_ALLOC_PTR(uuid);
399         ll_generate_random_uuid(uuidc);
400         class_uuid_unparse(uuidc, uuid);
401
402         /* Start the MGC */
403         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
404                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
405                                  niduuid, 0, 0);
406         OBD_FREE_PTR(uuid);
407         if (rc)
408                 GOTO(out_free, rc);
409
410         /* Add any failover MGS nids */
411         i = 1;
412         while (ptr && ((*ptr == ':' ||
413                class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
414                 /* New failover node */
415                 sprintf(niduuid, "%s_%x", mgcname, i);
416                 j = 0;
417                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
418                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
419                                      niduuid, 0, 0, 0);
420                         if (rc == 0)
421                                 ++j;
422                         if (*ptr == ':')
423                                 break;
424                 }
425                 if (j > 0) {
426                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
427                                      niduuid, 0, 0, 0);
428                         if (rc == 0)
429                                 ++i;
430                 } else {
431                         /* at ":/fsname" */
432                         break;
433                 }
434         }
435         lsi->lsi_lmd->lmd_mgs_failnodes = i;
436
437         obd = class_name2obd(mgcname);
438         if (!obd) {
439                 CERROR("Can't find mgcobd %s\n", mgcname);
440                 GOTO(out_free, rc = -ENOTCONN);
441         }
442
443         rc = obd_set_info_async(NULL, obd->obd_self_export,
444                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
445                                 strlen(mgssec), mgssec, NULL);
446         if (rc)
447                 GOTO(out_free, rc);
448
449         /* Keep a refcount of servers/clients who started with "mount",
450            so we know when we can get rid of the mgc. */
451         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
452
453         /* We connect to the MGS at setup, and don't disconnect until cleanup */
454         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
455                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
456                                   OBD_CONNECT_LVB_TYPE;
457
458 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
459         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
460 #endif
461
462         if (lmd_is_client(lsi->lsi_lmd) &&
463             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
464                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
465         data->ocd_version = LUSTRE_VERSION_CODE;
466         rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
467         if (rc) {
468                 CERROR("connect failed %d\n", rc);
469                 GOTO(out, rc);
470         }
471
472         obd->u.cli.cl_mgc_mgsexp = exp;
473
474 out:
475         /* Keep the mgc info in the sb. Note that many lsi's can point
476            to the same mgc.*/
477         lsi->lsi_mgc = obd;
478 out_free:
479         mutex_unlock(&mgc_start_lock);
480
481         if (data)
482                 OBD_FREE_PTR(data);
483         if (mgcname)
484                 OBD_FREE(mgcname, len);
485         if (niduuid)
486                 OBD_FREE(niduuid, len + 2);
487         RETURN(rc);
488 }
489
490 static int lustre_stop_mgc(struct super_block *sb)
491 {
492         struct lustre_sb_info *lsi = s2lsi(sb);
493         struct obd_device *obd;
494         char *niduuid = 0, *ptr = 0;
495         int i, rc = 0, len = 0;
496         ENTRY;
497
498         if (!lsi)
499                 RETURN(-ENOENT);
500         obd = lsi->lsi_mgc;
501         if (!obd)
502                 RETURN(-ENOENT);
503         lsi->lsi_mgc = NULL;
504
505         mutex_lock(&mgc_start_lock);
506         LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
507         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
508                 /* This is not fatal, every client that stops
509                    will call in here. */
510                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
511                        atomic_read(&obd->u.cli.cl_mgc_refcount));
512                 GOTO(out, rc = -EBUSY);
513         }
514
515         /* The MGC has no recoverable data in any case.
516          * force shotdown set in umount_begin */
517         obd->obd_no_recov = 1;
518
519         if (obd->u.cli.cl_mgc_mgsexp) {
520                 /* An error is not fatal, if we are unable to send the
521                    disconnect mgs ping evictor cleans up the export */
522                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
523                 if (rc)
524                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
525         }
526
527         /* Save the obdname for cleaning the nid uuids, which are
528            obdname_XX */
529         len = strlen(obd->obd_name) + 6;
530         OBD_ALLOC(niduuid, len);
531         if (niduuid) {
532                 strcpy(niduuid, obd->obd_name);
533                 ptr = niduuid + strlen(niduuid);
534         }
535
536         rc = class_manual_cleanup(obd);
537         if (rc)
538                 GOTO(out, rc);
539
540         /* Clean the nid uuids */
541         if (!niduuid)
542                 GOTO(out, rc = -ENOMEM);
543
544         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
545                 sprintf(ptr, "_%x", i);
546                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
547                              niduuid, 0, 0, 0);
548                 if (rc)
549                         CERROR("del MDC UUID %s failed: rc = %d\n",
550                                niduuid, rc);
551         }
552 out:
553         if (niduuid)
554                 OBD_FREE(niduuid, len);
555
556         /* class_import_put will get rid of the additional connections */
557         mutex_unlock(&mgc_start_lock);
558         RETURN(rc);
559 }
560
561 /***************** lustre superblock **************/
562
563 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
564 {
565         struct lustre_sb_info *lsi;
566         ENTRY;
567
568         OBD_ALLOC_PTR(lsi);
569         if (!lsi)
570                 RETURN(NULL);
571         OBD_ALLOC_PTR(lsi->lsi_lmd);
572         if (!lsi->lsi_lmd) {
573                 OBD_FREE_PTR(lsi);
574                 RETURN(NULL);
575         }
576
577         lsi->lsi_lmd->lmd_exclude_count = 0;
578         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
579         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
580         s2lsi_nocast(sb) = lsi;
581         /* we take 1 extra ref for our setup */
582         atomic_set(&lsi->lsi_mounts, 1);
583
584         /* Default umount style */
585         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
586         INIT_LIST_HEAD(&lsi->lsi_lwp_list);
587         spin_lock_init(&lsi->lsi_lwp_lock);
588
589         RETURN(lsi);
590 }
591
592 static int lustre_free_lsi(struct super_block *sb)
593 {
594         struct lustre_sb_info *lsi = s2lsi(sb);
595         ENTRY;
596
597         LASSERT(lsi != NULL);
598         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
599
600         /* someone didn't call server_put_mount. */
601         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
602
603         if (lsi->lsi_lmd != NULL) {
604                 if (lsi->lsi_lmd->lmd_dev != NULL)
605                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
606                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
607                 if (lsi->lsi_lmd->lmd_profile != NULL)
608                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
609                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
610                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
611                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
612                                  strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
613                 if (lsi->lsi_lmd->lmd_opts != NULL)
614                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
615                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
616                 if (lsi->lsi_lmd->lmd_exclude_count)
617                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
618                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
619                                  lsi->lsi_lmd->lmd_exclude_count);
620                 if (lsi->lsi_lmd->lmd_mgs != NULL)
621                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
622                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
623                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
624                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
625                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
626                 if (lsi->lsi_lmd->lmd_params != NULL)
627                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
628
629                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
630         }
631
632         LASSERT(lsi->lsi_llsbi == NULL);
633         OBD_FREE(lsi, sizeof(*lsi));
634         s2lsi_nocast(sb) = NULL;
635
636         RETURN(0);
637 }
638
639 /* The lsi has one reference for every server that is using the disk -
640    e.g. MDT, MGS, and potentially MGC */
641 int lustre_put_lsi(struct super_block *sb)
642 {
643         struct lustre_sb_info *lsi = s2lsi(sb);
644         ENTRY;
645
646         LASSERT(lsi != NULL);
647
648         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
649         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
650                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
651                         lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
652                         lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
653                         lsi->lsi_dt_dev = NULL;
654                         obd_disconnect(lsi->lsi_osd_exp);
655                         /* wait till OSD is gone */
656                         obd_zombie_barrier();
657                 }
658                 lustre_free_lsi(sb);
659                 RETURN(1);
660         }
661         RETURN(0);
662 }
663
664 /*** SERVER NAME ***
665  * <FSNAME><SEPERATOR><TYPE><INDEX>
666  * FSNAME is between 1 and 8 characters (inclusive).
667  *      Excluded characters are '/' and ':'
668  * SEPERATOR is either ':' or '-'
669  * TYPE: "OST", "MDT", etc.
670  * INDEX: Hex representation of the index
671  */
672
673 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
674  * @param [in] svname server name including type and index
675  * @param [out] fsname Buffer to copy filesystem name prefix into.
676  *  Must have at least 'strlen(fsname) + 1' chars.
677  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
678  * rc < 0  on error
679  */
680 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
681 {
682         const char *dash;
683
684         dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
685         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
686                 ;
687         if (dash == svname)
688                 return -EINVAL;
689
690         if (fsname != NULL) {
691                 strncpy(fsname, svname, dash - svname);
692                 fsname[dash - svname] = '\0';
693         }
694
695         if (endptr != NULL)
696                 *endptr = dash;
697
698         return 0;
699 }
700 EXPORT_SYMBOL(server_name2fsname);
701
702 /**
703  * Get service name (svname) from string
704  * rc < 0 on error
705  * if endptr isn't NULL it is set to end of fsname *
706  */
707 int server_name2svname(const char *label, char *svname, const char **endptr,
708                        size_t svsize)
709 {
710         int rc;
711         const char *dash;
712
713         /* We use server_name2fsname() just for parsing */
714         rc = server_name2fsname(label, NULL, &dash);
715         if (rc != 0)
716                 return rc;
717
718         if (endptr != NULL)
719                 *endptr = dash;
720
721         if (strlcpy(svname, dash + 1, svsize) >= svsize)
722                 return -E2BIG;
723
724         return 0;
725 }
726 EXPORT_SYMBOL(server_name2svname);
727
728 /**
729  * check server name is OST.
730  **/
731 int server_name_is_ost(const char *svname)
732 {
733         const char *dash;
734         int rc;
735
736         /* We use server_name2fsname() just for parsing */
737         rc = server_name2fsname(svname, NULL, &dash);
738         if (rc != 0)
739                 return rc;
740
741         dash++;
742
743         if (strncmp(dash, "OST", 3) == 0)
744                 return 1;
745         return 0;
746 }
747 EXPORT_SYMBOL(server_name_is_ost);
748
749 /**
750  * Get the index from the target name MDTXXXX/OSTXXXX
751  * rc = server type, or rc < 0  on error
752  **/
753 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
754 {
755         const char *dash = tgtname;
756         unsigned long index;
757         int rc;
758
759         if (strncmp(dash, "MDT", 3) == 0)
760                 rc = LDD_F_SV_TYPE_MDT;
761         else if (strncmp(dash, "OST", 3) == 0)
762                 rc = LDD_F_SV_TYPE_OST;
763         else
764                 return -EINVAL;
765
766         dash += 3;
767
768         if (strncmp(dash, "all", 3) == 0) {
769                 if (endptr != NULL)
770                         *endptr = dash + 3;
771                 return rc | LDD_F_SV_ALL;
772         }
773
774         index = simple_strtoul(dash, (char **)endptr, 16);
775         if (idx != NULL)
776                 *idx = index;
777         return rc;
778 }
779 EXPORT_SYMBOL(target_name2index);
780
781 /* Get the index from the obd name.
782    rc = server type, or
783    rc < 0  on error
784    if endptr isn't NULL it is set to end of name */
785 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
786 {
787         const char *dash;
788         int rc;
789
790         /* We use server_name2fsname() just for parsing */
791         rc = server_name2fsname(svname, NULL, &dash);
792         if (rc != 0)
793                 return rc;
794
795         dash++;
796         rc = target_name2index(dash, idx, endptr);
797         if (rc < 0)
798                 return rc;
799
800         /* Account for -mdc after index that is possible when specifying mdt */
801         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
802                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
803                 *endptr += sizeof(LUSTRE_MDC_NAME);
804
805         return rc;
806 }
807 EXPORT_SYMBOL(server_name2index);
808
809 /*************** mount common betweeen server and client ***************/
810
811 /* Common umount */
812 int lustre_common_put_super(struct super_block *sb)
813 {
814         int rc;
815         ENTRY;
816
817         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
818
819         /* Drop a ref to the MGC */
820         rc = lustre_stop_mgc(sb);
821         if (rc && (rc != -ENOENT)) {
822                 if (rc != -EBUSY) {
823                         CERROR("Can't stop MGC: %d\n", rc);
824                         RETURN(rc);
825                 }
826                 /* BUSY just means that there's some other obd that
827                    needs the mgc.  Let him clean it up. */
828                 CDEBUG(D_MOUNT, "MGC still in use\n");
829         }
830         /* Drop a ref to the mounted disk */
831         lustre_put_lsi(sb);
832
833         RETURN(rc);
834 }
835 EXPORT_SYMBOL(lustre_common_put_super);
836
837 static void lmd_print(struct lustre_mount_data *lmd)
838 {
839         int i;
840
841         PRINT_CMD(D_MOUNT, "  mount data:\n");
842         if (lmd_is_client(lmd))
843                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
844         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
845         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
846
847         if (lmd->lmd_opts)
848                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
849
850         if (lmd->lmd_recovery_time_soft)
851                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
852                           lmd->lmd_recovery_time_soft);
853
854         if (lmd->lmd_recovery_time_hard)
855                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
856                           lmd->lmd_recovery_time_hard);
857
858         for (i = 0; i < lmd->lmd_exclude_count; i++) {
859                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
860                           lmd->lmd_exclude[i]);
861         }
862 }
863
864 /* Is this server on the exclusion list */
865 int lustre_check_exclusion(struct super_block *sb, char *svname)
866 {
867         struct lustre_sb_info *lsi = s2lsi(sb);
868         struct lustre_mount_data *lmd = lsi->lsi_lmd;
869         __u32 index;
870         int i, rc;
871         ENTRY;
872
873         rc = server_name2index(svname, &index, NULL);
874         if (rc != LDD_F_SV_TYPE_OST)
875                 /* Only exclude OSTs */
876                 RETURN(0);
877
878         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
879                index, lmd->lmd_exclude_count, lmd->lmd_dev);
880
881         for(i = 0; i < lmd->lmd_exclude_count; i++) {
882                 if (index == lmd->lmd_exclude[i]) {
883                         CWARN("Excluding %s (on exclusion list)\n", svname);
884                         RETURN(1);
885                 }
886         }
887         RETURN(0);
888 }
889
890 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
891 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
892 {
893         const char *s1 = ptr, *s2;
894         __u32 *exclude_list;
895         __u32 index = 0;
896         int rc = 0, devmax;
897         ENTRY;
898
899         /* The shortest an ost name can be is 8 chars: -OST0000.
900            We don't actually know the fsname at this time, so in fact
901            a user could specify any fsname. */
902         devmax = strlen(ptr) / 8 + 1;
903
904         /* temp storage until we figure out how many we have */
905         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
906         if (!exclude_list)
907                 RETURN(-ENOMEM);
908
909         /* we enter this fn pointing at the '=' */
910         while (*s1 && *s1 != ' ' && *s1 != ',') {
911                 s1++;
912                 rc = server_name2index(s1, &index, &s2);
913                 if (rc < 0) {
914                         CERROR("Can't parse server name '%s': rc = %d\n",
915                                s1, rc);
916                         break;
917                 }
918                 if (rc == LDD_F_SV_TYPE_OST)
919                         exclude_list[lmd->lmd_exclude_count++] = index;
920                 else
921                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
922                                (uint)(s2-s1), s1, rc);
923                 s1 = s2;
924                 /* now we are pointing at ':' (next exclude)
925                    or ',' (end of excludes) */
926                 if (lmd->lmd_exclude_count >= devmax)
927                         break;
928         }
929         if (rc >= 0) /* non-err */
930                 rc = 0;
931
932         if (lmd->lmd_exclude_count) {
933                 /* permanent, freed in lustre_free_lsi */
934                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
935                           lmd->lmd_exclude_count);
936                 if (lmd->lmd_exclude) {
937                         memcpy(lmd->lmd_exclude, exclude_list,
938                                sizeof(index) * lmd->lmd_exclude_count);
939                 } else {
940                         rc = -ENOMEM;
941                         lmd->lmd_exclude_count = 0;
942                 }
943         }
944         OBD_FREE(exclude_list, sizeof(index) * devmax);
945         RETURN(rc);
946 }
947
948 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
949 {
950         char   *tail;
951         int     length;
952
953         if (lmd->lmd_mgssec != NULL) {
954                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
955                 lmd->lmd_mgssec = NULL;
956         }
957
958         tail = strchr(ptr, ',');
959         if (tail == NULL)
960                 length = strlen(ptr);
961         else
962                 length = tail - ptr;
963
964         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
965         if (lmd->lmd_mgssec == NULL)
966                 return -ENOMEM;
967
968         memcpy(lmd->lmd_mgssec, ptr, length);
969         lmd->lmd_mgssec[length] = '\0';
970         return 0;
971 }
972
973 static int lmd_parse_string(char **handle, char *ptr)
974 {
975         char   *tail;
976         int     length;
977
978         if ((handle == NULL) || (ptr == NULL))
979                 return -EINVAL;
980
981         if (*handle != NULL) {
982                 OBD_FREE(*handle, strlen(*handle) + 1);
983                 *handle = NULL;
984         }
985
986         tail = strchr(ptr, ',');
987         if (tail == NULL)
988                 length = strlen(ptr);
989         else
990                 length = tail - ptr;
991
992         OBD_ALLOC(*handle, length + 1);
993         if (*handle == NULL)
994                 return -ENOMEM;
995
996         memcpy(*handle, ptr, length);
997         (*handle)[length] = '\0';
998
999         return 0;
1000 }
1001
1002 /* Collect multiple values for mgsnid specifiers */
1003 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1004 {
1005         lnet_nid_t nid;
1006         char *tail = *ptr;
1007         char *mgsnid;
1008         int   length;
1009         int   oldlen = 0;
1010
1011         /* Find end of nidlist */
1012         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1013         length = tail - *ptr;
1014         if (length == 0) {
1015                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1016                 return -EINVAL;
1017         }
1018
1019         if (lmd->lmd_mgs != NULL)
1020                 oldlen = strlen(lmd->lmd_mgs) + 1;
1021
1022         OBD_ALLOC(mgsnid, oldlen + length + 1);
1023         if (mgsnid == NULL)
1024                 return -ENOMEM;
1025
1026         if (lmd->lmd_mgs != NULL) {
1027                 /* Multiple mgsnid= are taken to mean failover locations */
1028                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1029                 mgsnid[oldlen - 1] = ':';
1030                 OBD_FREE(lmd->lmd_mgs, oldlen);
1031         }
1032         memcpy(mgsnid + oldlen, *ptr, length);
1033         mgsnid[oldlen + length] = '\0';
1034         lmd->lmd_mgs = mgsnid;
1035         *ptr = tail;
1036
1037         return 0;
1038 }
1039
1040 /** Parse mount line options
1041  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1042  * dev is passed as device=uml1:/lustre by mount.lustre
1043  */
1044 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1045 {
1046         char *s1, *s2, *s3, *devname = NULL;
1047         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1048         int rc = 0;
1049         ENTRY;
1050
1051         LASSERT(lmd);
1052         if (!options) {
1053                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1054                                    "/sbin/mount.lustre is installed.\n");
1055                 RETURN(-EINVAL);
1056         }
1057
1058         /* Options should be a string - try to detect old lmd data */
1059         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1060                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1061                                    "/sbin/mount.lustre.  Please install "
1062                                    "version %s\n", LUSTRE_VERSION_STRING);
1063                 RETURN(-EINVAL);
1064         }
1065         lmd->lmd_magic = LMD_MAGIC;
1066
1067         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1068         if (lmd->lmd_params == NULL)
1069                 RETURN(-ENOMEM);
1070         lmd->lmd_params[0] = '\0';
1071
1072         /* Set default flags here */
1073
1074         s1 = options;
1075         while (*s1) {
1076                 int clear = 0;
1077                 int time_min = OBD_RECOVERY_TIME_MIN;
1078
1079                 /* Skip whitespace and extra commas */
1080                 while (*s1 == ' ' || *s1 == ',')
1081                         s1++;
1082                 s3 = s1;
1083
1084                 /* Client options are parsed in ll_options: eg. flock,
1085                    user_xattr, acl */
1086
1087                 /* Parse non-ldiskfs options here. Rather than modifying
1088                    ldiskfs, we just zero these out here */
1089                 if (strncmp(s1, "abort_recov", 11) == 0) {
1090                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1091                         clear++;
1092                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1093                         lmd->lmd_recovery_time_soft = max_t(int,
1094                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
1095                         clear++;
1096                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1097                         lmd->lmd_recovery_time_hard = max_t(int,
1098                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
1099                         clear++;
1100                 } else if (strncmp(s1, "noir", 4) == 0) {
1101                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1102                         clear++;
1103                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1104                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1105                         clear++;
1106                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1107                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1108                         clear++;
1109                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1110                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1111                         clear++;
1112                 } else if (strncmp(s1, PARAM_MGSNODE,
1113                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1114                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1115                         /* Assume the next mount opt is the first
1116                            invalid nid we get to. */
1117                         rc = lmd_parse_mgs(lmd, &s2);
1118                         if (rc)
1119                                 goto invalid;
1120                         s3 = s2;
1121                         clear++;
1122                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1123                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1124                         clear++;
1125                 } else if (strncmp(s1, "update", 6) == 0) {
1126                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1127                         clear++;
1128                 } else if (strncmp(s1, "virgin", 6) == 0) {
1129                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1130                         clear++;
1131                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1132                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1133                         clear++;
1134                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1135                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1136                         if (rc)
1137                                 goto invalid;
1138                         clear++;
1139                 /* ost exclusion list */
1140                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1141                         rc = lmd_make_exclusion(lmd, s1 + 7);
1142                         if (rc)
1143                                 goto invalid;
1144                         clear++;
1145                 } else if (strncmp(s1, "mgs", 3) == 0) {
1146                         /* We are an MGS */
1147                         lmd->lmd_flags |= LMD_FLG_MGS;
1148                         clear++;
1149                 } else if (strncmp(s1, "svname=", 7) == 0) {
1150                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1151                         if (rc)
1152                                 goto invalid;
1153                         clear++;
1154                 } else if (strncmp(s1, "param=", 6) == 0) {
1155                         size_t length, params_length;
1156                         char *tail = strchr(s1 + 6, ',');
1157                         if (tail == NULL) {
1158                                 length = strlen(s1);
1159                         } else {
1160                                 lnet_nid_t nid;
1161                                 char      *param_str = tail + 1;
1162                                 int        supplementary = 1;
1163
1164                                 while (class_parse_nid_quiet(param_str, &nid,
1165                                                              &param_str) == 0) {
1166                                         supplementary = 0;
1167                                 }
1168                                 length = param_str - s1 - supplementary;
1169                         }
1170                         length -= 6;
1171                         params_length = strlen(lmd->lmd_params);
1172                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1173                                 RETURN(-E2BIG);
1174                         strncat(lmd->lmd_params, s1 + 6, length);
1175                         lmd->lmd_params[params_length + length] = '\0';
1176                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1177                         s3 = s1 + 6 + length;
1178                         clear++;
1179                 } else if (strncmp(s1, "osd=", 4) == 0) {
1180                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1181                         if (rc)
1182                                 goto invalid;
1183                         clear++;
1184                 }
1185                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1186                    end of the options. */
1187                 else if (strncmp(s1, "device=", 7) == 0) {
1188                         devname = s1 + 7;
1189                         /* terminate options right before device.  device
1190                            must be the last one. */
1191                         *s1 = '\0';
1192                         break;
1193                 }
1194
1195                 /* Find next opt */
1196                 s2 = strchr(s3, ',');
1197                 if (s2 == NULL) {
1198                         if (clear)
1199                                 *s1 = '\0';
1200                         break;
1201                 }
1202                 s2++;
1203                 if (clear)
1204                         memmove(s1, s2, strlen(s2) + 1);
1205                 else
1206                         s1 = s2;
1207         }
1208
1209         if (!devname) {
1210                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1211                                    "(need mount option 'device=...')\n");
1212                 goto invalid;
1213         }
1214
1215         s1 = strstr(devname, ":/");
1216         if (s1) {
1217                 ++s1;
1218                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1219                 /* Remove leading /s from fsname */
1220                 while (*++s1 == '/') ;
1221                 /* Freed in lustre_free_lsi */
1222                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
1223                 if (!lmd->lmd_profile)
1224                         RETURN(-ENOMEM);
1225                 sprintf(lmd->lmd_profile, "%s-client", s1);
1226         }
1227
1228         /* Freed in lustre_free_lsi */
1229         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1230         if (!lmd->lmd_dev)
1231                 RETURN(-ENOMEM);
1232         strcpy(lmd->lmd_dev, devname);
1233
1234         /* Save mount options */
1235         s1 = options + strlen(options) - 1;
1236         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1237                 *s1-- = 0;
1238         if (*options != 0) {
1239                 /* Freed in lustre_free_lsi */
1240                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1241                 if (!lmd->lmd_opts)
1242                         RETURN(-ENOMEM);
1243                 strcpy(lmd->lmd_opts, options);
1244         }
1245
1246         lmd_print(lmd);
1247         lmd->lmd_magic = LMD_MAGIC;
1248
1249         RETURN(rc);
1250
1251 invalid:
1252         CERROR("Bad mount options %s\n", options);
1253         RETURN(-EINVAL);
1254 }
1255
1256 struct lustre_mount_data2 {
1257         void *lmd2_data;
1258         struct vfsmount *lmd2_mnt;
1259 };
1260
1261 /** This is the entry point for the mount call into Lustre.
1262  * This is called when a server or client is mounted,
1263  * and this is where we start setting things up.
1264  * @param data Mount options (e.g. -o flock,abort_recov)
1265  */
1266 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1267 {
1268         struct lustre_mount_data *lmd;
1269         struct lustre_mount_data2 *lmd2 = data;
1270         struct lustre_sb_info *lsi;
1271         int rc;
1272         ENTRY;
1273
1274         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1275
1276         lsi = lustre_init_lsi(sb);
1277         if (!lsi)
1278                 RETURN(-ENOMEM);
1279         lmd = lsi->lsi_lmd;
1280
1281         /*
1282          * Disable lockdep during mount, because mount locking patterns are
1283          * `special'.
1284          */
1285         lockdep_off();
1286
1287         /*
1288          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1289          */
1290         obd_zombie_barrier();
1291
1292         /* Figure out the lmd from the mount options */
1293         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1294                 lustre_put_lsi(sb);
1295                 GOTO(out, rc = -EINVAL);
1296         }
1297
1298         if (lmd_is_client(lmd)) {
1299                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1300                 if (client_fill_super == NULL)
1301                         request_module("lustre");
1302                 if (client_fill_super == NULL) {
1303                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1304                                            "client mount! Is the 'lustre' "
1305                                            "module loaded?\n");
1306                         lustre_put_lsi(sb);
1307                         rc = -ENODEV;
1308                 } else {
1309                         rc = lustre_start_mgc(sb);
1310                         if (rc) {
1311                                 lustre_put_lsi(sb);
1312                                 GOTO(out, rc);
1313                         }
1314                         /* Connect and start */
1315                         /* (should always be ll_fill_super) */
1316                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1317                         /* c_f_s will call lustre_common_put_super on failure */
1318                 }
1319         } else {
1320 #ifdef HAVE_SERVER_SUPPORT
1321                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1322                 rc = server_fill_super(sb);
1323                 /* s_f_s calls lustre_start_mgc after the mount because we need
1324                    the MGS nids which are stored on disk.  Plus, we may
1325                    need to start the MGS first. */
1326                 /* s_f_s will call server_put_super on failure */
1327 #else
1328                 CERROR("This is client-side-only module, "
1329                        "cannot handle server mount.\n");
1330                 rc = -EINVAL;
1331 #endif
1332         }
1333
1334         /* If error happens in fill_super() call, @lsi will be killed there.
1335          * This is why we do not put it here. */
1336         GOTO(out, rc);
1337 out:
1338         if (rc) {
1339                 CERROR("Unable to mount %s (%d)\n",
1340                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1341         } else {
1342                 CDEBUG(D_SUPER, "Mount %s complete\n",
1343                        lmd->lmd_dev);
1344         }
1345         lockdep_on();
1346         return rc;
1347 }
1348
1349
1350 /* We can't call ll_fill_super by name because it lives in a module that
1351    must be loaded after this one. */
1352 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1353                                                   struct vfsmount *mnt))
1354 {
1355         client_fill_super = cfs;
1356 }
1357 EXPORT_SYMBOL(lustre_register_client_fill_super);
1358
1359 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1360 {
1361         kill_super_cb = cfs;
1362 }
1363 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1364
1365 /***************** FS registration ******************/
1366 #ifdef HAVE_FSTYPE_MOUNT
1367 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1368                                    const char *devname, void *data)
1369 {
1370         struct lustre_mount_data2 lmd2 = { data, NULL };
1371
1372         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1373 }
1374 #else
1375 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1376                          const char *devname, void *data, struct vfsmount *mnt)
1377 {
1378         struct lustre_mount_data2 lmd2 = { data, mnt };
1379
1380         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1381 }
1382 #endif
1383
1384 static void lustre_kill_super(struct super_block *sb)
1385 {
1386         struct lustre_sb_info *lsi = s2lsi(sb);
1387
1388         if (kill_super_cb && lsi && !IS_SERVER(lsi))
1389                 (*kill_super_cb)(sb);
1390
1391         kill_anon_super(sb);
1392 }
1393
1394 /** Register the "lustre" fs type
1395  */
1396 static struct file_system_type lustre_fs_type = {
1397         .owner        = THIS_MODULE,
1398         .name         = "lustre",
1399 #ifdef HAVE_FSTYPE_MOUNT
1400         .mount        = lustre_mount,
1401 #else
1402         .get_sb       = lustre_get_sb,
1403 #endif
1404         .kill_sb      = lustre_kill_super,
1405         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
1406                         FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1407 };
1408 MODULE_ALIAS_FS("lustre");
1409
1410 int lustre_register_fs(void)
1411 {
1412         return register_filesystem(&lustre_fs_type);
1413 }
1414
1415 int lustre_unregister_fs(void)
1416 {
1417         return unregister_filesystem(&lustre_fs_type);
1418 }