Whamcloud - gitweb
309080e601bbc9e1147234714d6066b1614cad2a
[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                 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         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(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
513         if (!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                        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         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(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, atomic_read(&lsi->lsi_mounts));
653         if (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 /**
754  * Get the index from the target name MDTXXXX/OSTXXXX
755  * rc = server type, or rc < 0  on error
756  **/
757 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
758 {
759         const char *dash = tgtname;
760         unsigned long index;
761         int rc;
762
763         if (strncmp(dash, "MDT", 3) == 0)
764                 rc = LDD_F_SV_TYPE_MDT;
765         else if (strncmp(dash, "OST", 3) == 0)
766                 rc = LDD_F_SV_TYPE_OST;
767         else
768                 return -EINVAL;
769
770         dash += 3;
771
772         if (strncmp(dash, "all", 3) == 0) {
773                 if (endptr != NULL)
774                         *endptr = dash + 3;
775                 return rc | LDD_F_SV_ALL;
776         }
777
778         index = simple_strtoul(dash, (char **)endptr, 16);
779         if (idx != NULL)
780                 *idx = index;
781         return rc;
782 }
783 EXPORT_SYMBOL(target_name2index);
784
785 /* Get the index from the obd name.
786    rc = server type, or
787    rc < 0  on error
788    if endptr isn't NULL it is set to end of name */
789 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
790 {
791         const char *dash;
792         int rc;
793
794         /* We use server_name2fsname() just for parsing */
795         rc = server_name2fsname(svname, NULL, &dash);
796         if (rc != 0)
797                 return rc;
798
799         dash++;
800         rc = target_name2index(dash, idx, endptr);
801         if (rc < 0)
802                 return rc;
803
804         /* Account for -mdc after index that is possible when specifying mdt */
805         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
806                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
807                 *endptr += sizeof(LUSTRE_MDC_NAME);
808
809         return rc;
810 }
811 EXPORT_SYMBOL(server_name2index);
812
813 /*************** mount common betweeen server and client ***************/
814
815 /* Common umount */
816 int lustre_common_put_super(struct super_block *sb)
817 {
818         int rc;
819         ENTRY;
820
821         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
822
823         /* Drop a ref to the MGC */
824         rc = lustre_stop_mgc(sb);
825         if (rc && (rc != -ENOENT)) {
826                 if (rc != -EBUSY) {
827                         CERROR("Can't stop MGC: %d\n", rc);
828                         RETURN(rc);
829                 }
830                 /* BUSY just means that there's some other obd that
831                    needs the mgc.  Let him clean it up. */
832                 CDEBUG(D_MOUNT, "MGC still in use\n");
833         }
834         /* Drop a ref to the mounted disk */
835         lustre_put_lsi(sb);
836         lu_types_stop();
837         RETURN(rc);
838 }
839 EXPORT_SYMBOL(lustre_common_put_super);
840
841 static void lmd_print(struct lustre_mount_data *lmd)
842 {
843         int i;
844
845         PRINT_CMD(D_MOUNT, "  mount data:\n");
846         if (lmd_is_client(lmd))
847                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
848         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
849         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
850
851         if (lmd->lmd_opts)
852                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
853
854         if (lmd->lmd_recovery_time_soft)
855                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
856                           lmd->lmd_recovery_time_soft);
857
858         if (lmd->lmd_recovery_time_hard)
859                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
860                           lmd->lmd_recovery_time_hard);
861
862         for (i = 0; i < lmd->lmd_exclude_count; i++) {
863                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
864                           lmd->lmd_exclude[i]);
865         }
866 }
867
868 /* Is this server on the exclusion list */
869 int lustre_check_exclusion(struct super_block *sb, char *svname)
870 {
871         struct lustre_sb_info *lsi = s2lsi(sb);
872         struct lustre_mount_data *lmd = lsi->lsi_lmd;
873         __u32 index;
874         int i, rc;
875         ENTRY;
876
877         rc = server_name2index(svname, &index, NULL);
878         if (rc != LDD_F_SV_TYPE_OST)
879                 /* Only exclude OSTs */
880                 RETURN(0);
881
882         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
883                index, lmd->lmd_exclude_count, lmd->lmd_dev);
884
885         for(i = 0; i < lmd->lmd_exclude_count; i++) {
886                 if (index == lmd->lmd_exclude[i]) {
887                         CWARN("Excluding %s (on exclusion list)\n", svname);
888                         RETURN(1);
889                 }
890         }
891         RETURN(0);
892 }
893
894 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
895 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
896 {
897         const char *s1 = ptr, *s2;
898         __u32 index, *exclude_list;
899         int rc = 0, devmax;
900         ENTRY;
901
902         /* The shortest an ost name can be is 8 chars: -OST0000.
903            We don't actually know the fsname at this time, so in fact
904            a user could specify any fsname. */
905         devmax = strlen(ptr) / 8 + 1;
906
907         /* temp storage until we figure out how many we have */
908         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
909         if (!exclude_list)
910                 RETURN(-ENOMEM);
911
912         /* we enter this fn pointing at the '=' */
913         while (*s1 && *s1 != ' ' && *s1 != ',') {
914                 s1++;
915                 rc = server_name2index(s1, &index, &s2);
916                 if (rc < 0) {
917                         CERROR("Can't parse server name '%s': rc = %d\n",
918                                s1, rc);
919                         break;
920                 }
921                 if (rc == LDD_F_SV_TYPE_OST)
922                         exclude_list[lmd->lmd_exclude_count++] = index;
923                 else
924                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
925                                (uint)(s2-s1), s1, rc);
926                 s1 = s2;
927                 /* now we are pointing at ':' (next exclude)
928                    or ',' (end of excludes) */
929                 if (lmd->lmd_exclude_count >= devmax)
930                         break;
931         }
932         if (rc >= 0) /* non-err */
933                 rc = 0;
934
935         if (lmd->lmd_exclude_count) {
936                 /* permanent, freed in lustre_free_lsi */
937                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
938                           lmd->lmd_exclude_count);
939                 if (lmd->lmd_exclude) {
940                         memcpy(lmd->lmd_exclude, exclude_list,
941                                sizeof(index) * lmd->lmd_exclude_count);
942                 } else {
943                         rc = -ENOMEM;
944                         lmd->lmd_exclude_count = 0;
945                 }
946         }
947         OBD_FREE(exclude_list, sizeof(index) * devmax);
948         RETURN(rc);
949 }
950
951 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
952 {
953         char   *tail;
954         int     length;
955
956         if (lmd->lmd_mgssec != NULL) {
957                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
958                 lmd->lmd_mgssec = NULL;
959         }
960
961         tail = strchr(ptr, ',');
962         if (tail == NULL)
963                 length = strlen(ptr);
964         else
965                 length = tail - ptr;
966
967         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
968         if (lmd->lmd_mgssec == NULL)
969                 return -ENOMEM;
970
971         memcpy(lmd->lmd_mgssec, ptr, length);
972         lmd->lmd_mgssec[length] = '\0';
973         return 0;
974 }
975
976 static int lmd_parse_string(char **handle, char *ptr)
977 {
978         char   *tail;
979         int     length;
980
981         if ((handle == NULL) || (ptr == NULL))
982                 return -EINVAL;
983
984         if (*handle != NULL) {
985                 OBD_FREE(*handle, strlen(*handle) + 1);
986                 *handle = NULL;
987         }
988
989         tail = strchr(ptr, ',');
990         if (tail == NULL)
991                 length = strlen(ptr);
992         else
993                 length = tail - ptr;
994
995         OBD_ALLOC(*handle, length + 1);
996         if (*handle == NULL)
997                 return -ENOMEM;
998
999         memcpy(*handle, ptr, length);
1000         (*handle)[length] = '\0';
1001
1002         return 0;
1003 }
1004
1005 /* Collect multiple values for mgsnid specifiers */
1006 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1007 {
1008         lnet_nid_t nid;
1009         char *tail = *ptr;
1010         char *mgsnid;
1011         int   length;
1012         int   oldlen = 0;
1013
1014         /* Find end of nidlist */
1015         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1016         length = tail - *ptr;
1017         if (length == 0) {
1018                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1019                 return -EINVAL;
1020         }
1021
1022         if (lmd->lmd_mgs != NULL)
1023                 oldlen = strlen(lmd->lmd_mgs) + 1;
1024
1025         OBD_ALLOC(mgsnid, oldlen + length + 1);
1026         if (mgsnid == NULL)
1027                 return -ENOMEM;
1028
1029         if (lmd->lmd_mgs != NULL) {
1030                 /* Multiple mgsnid= are taken to mean failover locations */
1031                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1032                 mgsnid[oldlen - 1] = ':';
1033                 OBD_FREE(lmd->lmd_mgs, oldlen);
1034         }
1035         memcpy(mgsnid + oldlen, *ptr, length);
1036         mgsnid[oldlen + length] = '\0';
1037         lmd->lmd_mgs = mgsnid;
1038         *ptr = tail;
1039
1040         return 0;
1041 }
1042
1043 /** Parse mount line options
1044  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1045  * dev is passed as device=uml1:/lustre by mount.lustre
1046  */
1047 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1048 {
1049         char *s1, *s2, *devname = NULL;
1050         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1051         int rc = 0;
1052         ENTRY;
1053
1054         LASSERT(lmd);
1055         if (!options) {
1056                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1057                                    "/sbin/mount.lustre is installed.\n");
1058                 RETURN(-EINVAL);
1059         }
1060
1061         /* Options should be a string - try to detect old lmd data */
1062         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1063                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1064                                    "/sbin/mount.lustre.  Please install "
1065                                    "version %s\n", LUSTRE_VERSION_STRING);
1066                 RETURN(-EINVAL);
1067         }
1068         lmd->lmd_magic = LMD_MAGIC;
1069
1070         OBD_ALLOC(lmd->lmd_params, 4096);
1071         if (lmd->lmd_params == NULL)
1072                 RETURN(-ENOMEM);
1073         lmd->lmd_params[0] = '\0';
1074
1075         /* Set default flags here */
1076
1077         s1 = options;
1078         while (*s1) {
1079                 int clear = 0;
1080                 int time_min = OBD_RECOVERY_TIME_MIN;
1081
1082                 /* Skip whitespace and extra commas */
1083                 while (*s1 == ' ' || *s1 == ',')
1084                         s1++;
1085
1086                 /* Client options are parsed in ll_options: eg. flock,
1087                    user_xattr, acl */
1088
1089                 /* Parse non-ldiskfs options here. Rather than modifying
1090                    ldiskfs, we just zero these out here */
1091                 if (strncmp(s1, "abort_recov", 11) == 0) {
1092                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1093                         clear++;
1094                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1095                         lmd->lmd_recovery_time_soft = max_t(int,
1096                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
1097                         clear++;
1098                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1099                         lmd->lmd_recovery_time_hard = max_t(int,
1100                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
1101                         clear++;
1102                 } else if (strncmp(s1, "noir", 4) == 0) {
1103                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1104                         clear++;
1105                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1106                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1107                         clear++;
1108                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1109                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1110                         clear++;
1111                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1112                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1113                         clear++;
1114                 } else if (strncmp(s1, PARAM_MGSNODE,
1115                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1116                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1117                         /* Assume the next mount opt is the first
1118                            invalid nid we get to. */
1119                         rc = lmd_parse_mgs(lmd, &s2);
1120                         if (rc)
1121                                 goto invalid;
1122                         clear++;
1123                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1124                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1125                         clear++;
1126                 } else if (strncmp(s1, "update", 6) == 0) {
1127                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1128                         clear++;
1129                 } else if (strncmp(s1, "virgin", 6) == 0) {
1130                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1131                         clear++;
1132                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1133                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1134                         clear++;
1135                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1136                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1137                         if (rc)
1138                                 goto invalid;
1139                         clear++;
1140                 /* ost exclusion list */
1141                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1142                         rc = lmd_make_exclusion(lmd, s1 + 7);
1143                         if (rc)
1144                                 goto invalid;
1145                         clear++;
1146                 } else if (strncmp(s1, "mgs", 3) == 0) {
1147                         /* We are an MGS */
1148                         lmd->lmd_flags |= LMD_FLG_MGS;
1149                         clear++;
1150                 } else if (strncmp(s1, "svname=", 7) == 0) {
1151                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1152                         if (rc)
1153                                 goto invalid;
1154                         clear++;
1155                 } else if (strncmp(s1, "param=", 6) == 0) {
1156                         int length;
1157                         char *tail = strchr(s1 + 6, ',');
1158                         if (tail == NULL)
1159                                 length = strlen(s1);
1160                         else
1161                                 length = tail - s1;
1162                         length -= 6;
1163                         strncat(lmd->lmd_params, s1 + 6, length);
1164                         strcat(lmd->lmd_params, " ");
1165                         clear++;
1166                 } else if (strncmp(s1, "osd=", 4) == 0) {
1167                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1168                         if (rc)
1169                                 goto invalid;
1170                         clear++;
1171                 }
1172                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1173                    end of the options. */
1174                 else if (strncmp(s1, "device=", 7) == 0) {
1175                         devname = s1 + 7;
1176                         /* terminate options right before device.  device
1177                            must be the last one. */
1178                         *s1 = '\0';
1179                         break;
1180                 }
1181
1182                 /* Find next opt */
1183                 s2 = strchr(s1, ',');
1184                 if (s2 == NULL) {
1185                         if (clear)
1186                                 *s1 = '\0';
1187                         break;
1188                 }
1189                 s2++;
1190                 if (clear)
1191                         memmove(s1, s2, strlen(s2) + 1);
1192                 else
1193                         s1 = s2;
1194         }
1195
1196         if (!devname) {
1197                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1198                                    "(need mount option 'device=...')\n");
1199                 goto invalid;
1200         }
1201
1202         s1 = strstr(devname, ":/");
1203         if (s1) {
1204                 ++s1;
1205                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1206                 /* Remove leading /s from fsname */
1207                 while (*++s1 == '/') ;
1208                 /* Freed in lustre_free_lsi */
1209                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
1210                 if (!lmd->lmd_profile)
1211                         RETURN(-ENOMEM);
1212                 sprintf(lmd->lmd_profile, "%s-client", s1);
1213         }
1214
1215         /* Freed in lustre_free_lsi */
1216         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1217         if (!lmd->lmd_dev)
1218                 RETURN(-ENOMEM);
1219         strcpy(lmd->lmd_dev, devname);
1220
1221         /* Save mount options */
1222         s1 = options + strlen(options) - 1;
1223         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1224                 *s1-- = 0;
1225         if (*options != 0) {
1226                 /* Freed in lustre_free_lsi */
1227                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1228                 if (!lmd->lmd_opts)
1229                         RETURN(-ENOMEM);
1230                 strcpy(lmd->lmd_opts, options);
1231         }
1232
1233         lmd_print(lmd);
1234         lmd->lmd_magic = LMD_MAGIC;
1235
1236         RETURN(rc);
1237
1238 invalid:
1239         CERROR("Bad mount options %s\n", options);
1240         RETURN(-EINVAL);
1241 }
1242
1243 struct lustre_mount_data2 {
1244         void *lmd2_data;
1245         struct vfsmount *lmd2_mnt;
1246 };
1247
1248 /** This is the entry point for the mount call into Lustre.
1249  * This is called when a server or client is mounted,
1250  * and this is where we start setting things up.
1251  * @param data Mount options (e.g. -o flock,abort_recov)
1252  */
1253 int lustre_fill_super(struct super_block *sb, void *data, int silent)
1254 {
1255         struct lustre_mount_data *lmd;
1256         struct lustre_mount_data2 *lmd2 = data;
1257         struct lustre_sb_info *lsi;
1258         int rc;
1259         ENTRY;
1260
1261         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1262
1263         lsi = lustre_init_lsi(sb);
1264         if (!lsi)
1265                 RETURN(-ENOMEM);
1266         lmd = lsi->lsi_lmd;
1267
1268         /*
1269          * Disable lockdep during mount, because mount locking patterns are
1270          * `special'.
1271          */
1272         lockdep_off();
1273
1274         /*
1275          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1276          */
1277         obd_zombie_barrier();
1278
1279         /* Figure out the lmd from the mount options */
1280         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1281                 lustre_put_lsi(sb);
1282                 GOTO(out, rc = -EINVAL);
1283         }
1284
1285         if (lmd_is_client(lmd)) {
1286                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1287                 if (!client_fill_super) {
1288                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1289                                            "client mount! Is the 'lustre' "
1290                                            "module loaded?\n");
1291                         lustre_put_lsi(sb);
1292                         rc = -ENODEV;
1293                 } else {
1294                         rc = lustre_start_mgc(sb);
1295                         if (rc) {
1296                                 lustre_put_lsi(sb);
1297                                 GOTO(out, rc);
1298                         }
1299                         /* Connect and start */
1300                         /* (should always be ll_fill_super) */
1301                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1302                         /* c_f_s will call lustre_common_put_super on failure */
1303                 }
1304         } else {
1305 #ifdef HAVE_SERVER_SUPPORT
1306                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1307                 rc = server_fill_super(sb);
1308                 /* s_f_s calls lustre_start_mgc after the mount because we need
1309                    the MGS nids which are stored on disk.  Plus, we may
1310                    need to start the MGS first. */
1311                 /* s_f_s will call server_put_super on failure */
1312 #else
1313                 CERROR("This is client-side-only module, "
1314                        "cannot handle server mount.\n");
1315                 rc = -EINVAL;
1316 #endif
1317         }
1318
1319         /* If error happens in fill_super() call, @lsi will be killed there.
1320          * This is why we do not put it here. */
1321         GOTO(out, rc);
1322 out:
1323         if (rc) {
1324                 CERROR("Unable to mount %s (%d)\n",
1325                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1326         } else {
1327                 CDEBUG(D_SUPER, "Mount %s complete\n",
1328                        lmd->lmd_dev);
1329         }
1330         lockdep_on();
1331         return rc;
1332 }
1333
1334
1335 /* We can't call ll_fill_super by name because it lives in a module that
1336    must be loaded after this one. */
1337 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1338                                                   struct vfsmount *mnt))
1339 {
1340         client_fill_super = cfs;
1341 }
1342 EXPORT_SYMBOL(lustre_register_client_fill_super);
1343
1344 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1345 {
1346         kill_super_cb = cfs;
1347 }
1348 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1349
1350 /***************** FS registration ******************/
1351 #ifdef HAVE_FSTYPE_MOUNT
1352 struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1353                                 const char *devname, void *data)
1354 {
1355         struct lustre_mount_data2 lmd2 = { data, NULL };
1356
1357         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1358 }
1359 #else
1360 int lustre_get_sb(struct file_system_type *fs_type, int flags,
1361                   const char *devname, void * data, struct vfsmount *mnt)
1362 {
1363         struct lustre_mount_data2 lmd2 = { data, mnt };
1364
1365         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1366 }
1367 #endif
1368
1369 void lustre_kill_super(struct super_block *sb)
1370 {
1371         struct lustre_sb_info *lsi = s2lsi(sb);
1372
1373         if (kill_super_cb && lsi && !IS_SERVER(lsi))
1374                 (*kill_super_cb)(sb);
1375
1376         kill_anon_super(sb);
1377 }
1378
1379 /** Register the "lustre" fs type
1380  */
1381 struct file_system_type lustre_fs_type = {
1382         .owner        = THIS_MODULE,
1383         .name         = "lustre",
1384 #ifdef HAVE_FSTYPE_MOUNT
1385         .mount        = lustre_mount,
1386 #else
1387         .get_sb       = lustre_get_sb,
1388 #endif
1389         .kill_sb      = lustre_kill_super,
1390         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
1391                         FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1392 };
1393
1394 int lustre_register_fs(void)
1395 {
1396         return register_filesystem(&lustre_fs_type);
1397 }
1398
1399 int lustre_unregister_fs(void)
1400 {
1401         return unregister_filesystem(&lustre_fs_type);
1402 }