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