Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / obdclass / obd_mount.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/obdclass/obd_mount.c
5  *  Client/server mount routines
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Nathan Rutman <nathan@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org/
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28 #define D_MOUNT D_SUPER|D_CONFIG /*|D_WARNING */
29 #define PRINT_CMD CDEBUG
30 #define PRINT_MASK D_SUPER|D_CONFIG
31
32 #include <obd.h>
33 #include <lvfs.h>
34 #include <lustre_fsfilt.h>
35 #include <obd_class.h>
36 #include <lustre/lustre_user.h>
37 #include <linux/version.h>
38 #include <lustre_log.h>
39 #include <lustre_disk.h>
40 #include <lustre_param.h>
41
42 static int (*client_fill_super)(struct super_block *sb) = NULL;
43 static void (*kill_super_cb)(struct super_block *sb) = NULL;
44
45 /*********** mount lookup *********/
46
47 DECLARE_MUTEX(lustre_mount_info_lock);
48 static CFS_LIST_HEAD(server_mount_info_list);
49
50 static struct lustre_mount_info *server_find_mount(const char *name)
51 {
52         struct list_head *tmp;
53         struct lustre_mount_info *lmi;
54         ENTRY;
55
56         list_for_each(tmp, &server_mount_info_list) {
57                 lmi = list_entry(tmp, struct lustre_mount_info, lmi_list_chain);
58                 if (strcmp(name, lmi->lmi_name) == 0)
59                         RETURN(lmi);
60         }
61         RETURN(NULL);
62 }
63
64 /* we must register an obd for a mount before we call the setup routine.
65    *_setup will call lustre_get_mount to get the mnt struct
66    by obd_name, since we can't pass the pointer to setup. */
67 static int server_register_mount(const char *name, struct super_block *sb,
68                           struct vfsmount *mnt)
69 {
70         struct lustre_mount_info *lmi;
71         char *name_cp;
72         ENTRY;
73
74         LASSERT(mnt);
75         LASSERT(sb);
76
77         OBD_ALLOC(lmi, sizeof(*lmi));
78         if (!lmi)
79                 RETURN(-ENOMEM);
80         OBD_ALLOC(name_cp, strlen(name) + 1);
81         if (!name_cp) {
82                 OBD_FREE(lmi, sizeof(*lmi));
83                 RETURN(-ENOMEM);
84         }
85         strcpy(name_cp, name);
86
87         down(&lustre_mount_info_lock);
88
89         if (server_find_mount(name)) {
90                 up(&lustre_mount_info_lock);
91                 OBD_FREE(lmi, sizeof(*lmi));
92                 OBD_FREE(name_cp, strlen(name) + 1);
93                 CERROR("Already registered %s\n", name);
94                 RETURN(-EEXIST);
95         }
96         lmi->lmi_name = name_cp;
97         lmi->lmi_sb = sb;
98         lmi->lmi_mnt = mnt;
99         list_add(&lmi->lmi_list_chain, &server_mount_info_list);
100
101         up(&lustre_mount_info_lock);
102
103         CDEBUG(D_MOUNT, "reg_mnt %p from %s, vfscount=%d\n",
104                lmi->lmi_mnt, name, atomic_read(&lmi->lmi_mnt->mnt_count));
105
106         RETURN(0);
107 }
108
109 /* when an obd no longer needs a mount */
110 static int server_deregister_mount(const char *name)
111 {
112         struct lustre_mount_info *lmi;
113         ENTRY;
114
115         down(&lustre_mount_info_lock);
116         lmi = server_find_mount(name);
117         if (!lmi) {
118                 up(&lustre_mount_info_lock);
119                 CERROR("%s not registered\n", name);
120                 RETURN(-ENOENT);
121         }
122
123         CDEBUG(D_MOUNT, "dereg_mnt %p from %s, vfscount=%d\n",
124                lmi->lmi_mnt, name, atomic_read(&lmi->lmi_mnt->mnt_count));
125
126         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
127         list_del(&lmi->lmi_list_chain);
128         OBD_FREE(lmi, sizeof(*lmi));
129         up(&lustre_mount_info_lock);
130
131         RETURN(0);
132 }
133
134 /* obd's look up a registered mount using their obdname. This is just
135    for initial obd setup to find the mount struct.  It should not be
136    called every time you want to mntget. */
137 struct lustre_mount_info *server_get_mount(const char *name)
138 {
139         struct lustre_mount_info *lmi;
140         struct lustre_sb_info *lsi;
141         ENTRY;
142
143         down(&lustre_mount_info_lock);
144         lmi = server_find_mount(name);
145         up(&lustre_mount_info_lock);
146         if (!lmi) {
147                 CERROR("Can't find mount for %s\n", name);
148                 RETURN(NULL);
149         }
150         lsi = s2lsi(lmi->lmi_sb);
151         mntget(lmi->lmi_mnt);
152         atomic_inc(&lsi->lsi_mounts);
153
154         CDEBUG(D_MOUNT, "get_mnt %p from %s, refs=%d, vfscount=%d\n",
155                lmi->lmi_mnt, name, atomic_read(&lsi->lsi_mounts),
156                atomic_read(&lmi->lmi_mnt->mnt_count));
157
158         RETURN(lmi);
159 }
160
161 /*
162  * Used by mdt to get mount_info from obdname.
163  * There are no blocking when using the mount_info.
164  * Do not use server_get_mount for this purpose.
165  */
166 struct lustre_mount_info *server_get_mount_2(const char *name)
167 {
168         struct lustre_mount_info *lmi;
169         ENTRY;
170
171         down(&lustre_mount_info_lock);
172         lmi = server_find_mount(name);
173         up(&lustre_mount_info_lock);
174         if (!lmi)
175                 CERROR("Can't find mount for %s\n", name);
176
177         RETURN(lmi);
178 }
179
180 static void unlock_mntput(struct vfsmount *mnt)
181 {
182         if (kernel_locked()) {
183                 unlock_kernel();
184                 mntput(mnt);
185                 lock_kernel();
186         } else {
187                 mntput(mnt);
188         }
189 }
190
191 static int lustre_put_lsi(struct super_block *sb);
192
193 /* to be called from obd_cleanup methods */
194 int server_put_mount(const char *name, struct vfsmount *mnt)
195 {
196         struct lustre_mount_info *lmi;
197         struct lustre_sb_info *lsi;
198         int count = atomic_read(&mnt->mnt_count) - 1;
199         ENTRY;
200
201         /* This might be the last one, can't deref after this */
202         unlock_mntput(mnt);
203
204         down(&lustre_mount_info_lock);
205         lmi = server_find_mount(name);
206         up(&lustre_mount_info_lock);
207         if (!lmi) {
208                 CERROR("Can't find mount for %s\n", name);
209                 RETURN(-ENOENT);
210         }
211         lsi = s2lsi(lmi->lmi_sb);
212         LASSERT(lmi->lmi_mnt == mnt);
213
214         CDEBUG(D_MOUNT, "put_mnt %p from %s, refs=%d, vfscount=%d\n",
215                lmi->lmi_mnt, name, atomic_read(&lsi->lsi_mounts), count);
216
217         if (lustre_put_lsi(lmi->lmi_sb)) {
218                 CDEBUG(D_MOUNT, "Last put of mnt %p from %s, vfscount=%d\n",
219                        lmi->lmi_mnt, name, count);
220                 /* last mount is the One True Mount */
221                 if (count > 1)
222                         CERROR("%s: mount busy, vfscount=%d!\n", name, count);
223         }
224
225         /* this obd should never need the mount again */
226         server_deregister_mount(name);
227
228         RETURN(0);
229 }
230
231 /* Corresponding to server_get_mount_2 */
232 int server_put_mount_2(const char *name, struct vfsmount *mnt)
233 {
234         ENTRY;
235         RETURN(0);
236 }
237
238 /******* mount helper utilities *********/
239
240 #if 0
241 static void ldd_print(struct lustre_disk_data *ldd)
242 {
243         PRINT_CMD(PRINT_MASK, "  disk data:\n");
244         PRINT_CMD(PRINT_MASK, "server:  %s\n", ldd->ldd_svname);
245         PRINT_CMD(PRINT_MASK, "uuid:    %s\n", (char *)ldd->ldd_uuid);
246         PRINT_CMD(PRINT_MASK, "fs:      %s\n", ldd->ldd_fsname);
247         PRINT_CMD(PRINT_MASK, "index:   %04x\n", ldd->ldd_svindex);
248         PRINT_CMD(PRINT_MASK, "config:  %d\n", ldd->ldd_config_ver);
249         PRINT_CMD(PRINT_MASK, "flags:   %#x\n", ldd->ldd_flags);
250         PRINT_CMD(PRINT_MASK, "diskfs:  %s\n", MT_STR(ldd));
251         PRINT_CMD(PRINT_MASK, "options: %s\n", ldd->ldd_mount_opts);
252         PRINT_CMD(PRINT_MASK, "params:  %s\n", ldd->ldd_params);
253         PRINT_CMD(PRINT_MASK, "comment: %s\n", ldd->ldd_userdata);
254 }
255 #endif
256
257 static int ldd_parse(struct lvfs_run_ctxt *mount_ctxt,
258                            struct lustre_disk_data *ldd)
259 {
260         struct lvfs_run_ctxt saved;
261         struct file *file;
262         loff_t off = 0;
263         unsigned long len;
264         int rc;
265         ENTRY;
266
267         push_ctxt(&saved, mount_ctxt, NULL);
268
269         file = filp_open(MOUNT_DATA_FILE, O_RDONLY, 0644);
270         if (IS_ERR(file)) {
271                 rc = PTR_ERR(file);
272                 CERROR("cannot open %s: rc = %d\n", MOUNT_DATA_FILE, rc);
273                 GOTO(out, rc);
274         }
275
276         len = i_size_read(file->f_dentry->d_inode);
277         CDEBUG(D_MOUNT, "Have %s, size %lu\n", MOUNT_DATA_FILE, len);
278         if (len != sizeof(*ldd)) {
279                 CERROR("disk data size does not match: see %lu expect "LPSZ"\n",
280                        len, sizeof(*ldd));
281                 GOTO(out_close, rc = -EINVAL);
282         }
283
284         rc = lustre_fread(file, ldd, len, &off);
285         if (rc != len) {
286                 CERROR("error reading %s: read %d of %lu\n",
287                        MOUNT_DATA_FILE, rc, len);
288                 GOTO(out_close, rc = -EINVAL);
289         }
290         rc = 0;
291
292         if (ldd->ldd_magic != LDD_MAGIC) {
293                 /* FIXME add swabbing support */
294                 CERROR("Bad magic in %s: %x!=%x\n", MOUNT_DATA_FILE,
295                        ldd->ldd_magic, LDD_MAGIC);
296                 GOTO(out_close, rc = -EINVAL);
297         }
298
299         if (ldd->ldd_feature_incompat & ~LDD_INCOMPAT_SUPP) {
300                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
301                        ldd->ldd_svname,
302                        ldd->ldd_feature_incompat & ~LDD_INCOMPAT_SUPP);
303                 GOTO(out_close, rc = -EINVAL);
304         }
305         if (ldd->ldd_feature_rocompat & ~LDD_ROCOMPAT_SUPP) {
306                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
307                        ldd->ldd_svname,
308                        ldd->ldd_feature_rocompat & ~LDD_ROCOMPAT_SUPP);
309                 /* Do something like remount filesystem read-only */
310                 GOTO(out_close, rc = -EINVAL);
311         }
312
313 out_close:
314         filp_close(file, 0);
315 out:
316         pop_ctxt(&saved, mount_ctxt, NULL);
317         RETURN(rc);
318 }
319
320 static int ldd_write(struct lvfs_run_ctxt *mount_ctxt,
321                      struct lustre_disk_data *ldd)
322 {
323         struct lvfs_run_ctxt saved;
324         struct file *file;
325         loff_t off = 0;
326         unsigned long len = sizeof(struct lustre_disk_data);
327         int rc = 0;
328         ENTRY;
329
330         LASSERT(ldd->ldd_magic == LDD_MAGIC);
331
332         ldd->ldd_config_ver++;
333
334         push_ctxt(&saved, mount_ctxt, NULL);
335
336         file = filp_open(MOUNT_DATA_FILE, O_RDWR, 0644);
337         if (IS_ERR(file)) {
338                 rc = PTR_ERR(file);
339                 CERROR("cannot open %s: rc = %d\n", MOUNT_DATA_FILE, rc);
340                 GOTO(out, rc);
341         }
342
343         rc = lustre_fwrite(file, ldd, len, &off);
344         if (rc != len) {
345                 CERROR("error writing %s: read %d of %lu\n",
346                        MOUNT_DATA_FILE, rc, len);
347                 GOTO(out_close, rc = -EINVAL);
348         }
349
350         rc = 0;
351
352 out_close:
353         filp_close(file, 0);
354 out:
355         pop_ctxt(&saved, mount_ctxt, NULL);
356         RETURN(rc);
357 }
358
359
360 /**************** config llog ********************/
361
362 /* Get a config log from the MGS and process it.
363    This func is called for both clients and servers.
364    Continue to process new statements appended to the logs
365    (whenever the config lock is revoked) until lustre_end_log
366    is called. */
367 int lustre_process_log(struct super_block *sb, char *logname,
368                      struct config_llog_instance *cfg)
369 {
370         struct lustre_cfg *lcfg;
371         struct lustre_cfg_bufs bufs;
372         struct lustre_sb_info *lsi = s2lsi(sb);
373         struct obd_device *mgc = lsi->lsi_mgc;
374         int rc;
375         ENTRY;
376
377         LASSERT(mgc);
378         LASSERT(cfg);
379
380         /* mgc_process_config */
381         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
382         lustre_cfg_bufs_set_string(&bufs, 1, logname);
383         lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
384         lustre_cfg_bufs_set(&bufs, 3, &sb, sizeof(sb));
385         lcfg = lustre_cfg_new(LCFG_LOG_START, &bufs);
386         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
387         lustre_cfg_free(lcfg);
388
389         if (rc == -EINVAL)
390                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
391                                    "failed from the MGS (%d).  Make sure this "
392                                    "client and the MGS are running compatible "
393                                    "versions of Lustre.\n",
394                                    mgc->obd_name, logname, rc);
395
396         if (rc)
397                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
398                                    "failed (%d). This may be the result of "
399                                    "communication errors between this node and "
400                                    "the MGS, a bad configuration, or other "
401                                    "errors. See the syslog for more "
402                                    "information.\n", mgc->obd_name, logname, 
403                                    rc);
404
405         /* class_obd_list(); */
406         RETURN(rc);
407 }
408
409 /* Stop watching this config log for updates */
410 int lustre_end_log(struct super_block *sb, char *logname,
411                        struct config_llog_instance *cfg)
412 {
413         struct lustre_cfg *lcfg;
414         struct lustre_cfg_bufs bufs;
415         struct lustre_sb_info *lsi = s2lsi(sb);
416         struct obd_device *mgc = lsi->lsi_mgc;
417         int rc;
418         ENTRY;
419
420         if (!mgc)
421                 RETURN(-ENOENT);
422
423         /* mgc_process_config */
424         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
425         lustre_cfg_bufs_set_string(&bufs, 1, logname);
426         if (cfg)
427                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
428         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
429         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
430         lustre_cfg_free(lcfg);
431         RETURN(rc);
432 }
433
434 /**************** obd start *******************/
435
436 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
437             char *s1, char *s2, char *s3, char *s4)
438 {
439         struct lustre_cfg_bufs bufs;
440         struct lustre_cfg    * lcfg = NULL;
441         int rc;
442
443         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
444                cmd, s1, s2, s3, s4);
445
446         lustre_cfg_bufs_reset(&bufs, cfgname);
447         if (s1)
448                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
449         if (s2)
450                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
451         if (s3)
452                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
453         if (s4)
454                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
455
456         lcfg = lustre_cfg_new(cmd, &bufs);
457         lcfg->lcfg_nid = nid;
458         rc = class_process_config(lcfg);
459         lustre_cfg_free(lcfg);
460         return(rc);
461 }
462
463 static int lustre_start_simple(char *obdname, char *type, char *uuid,
464                                char *s1, char *s2)
465 {
466         int rc;
467         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
468
469         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, 0, 0);
470         if (rc) {
471                 CERROR("%s attach error %d\n", obdname, rc);
472                 return(rc);
473         }
474         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, 0, 0);
475         if (rc) {
476                 CERROR("%s setup error %d\n", obdname, rc);
477                 do_lcfg(obdname, 0, LCFG_DETACH, 0, 0, 0, 0);
478         }
479         return rc;
480 }
481
482 /* Set up a MGS to serve startup logs */
483 static int server_start_mgs(struct super_block *sb)
484 {
485         struct lustre_sb_info    *lsi = s2lsi(sb);
486         struct vfsmount          *mnt = lsi->lsi_srv_mnt;
487         struct lustre_mount_info *lmi;
488         int    rc = 0;
489         ENTRY;
490         LASSERT(mnt);
491
492         /* It is impossible to have more than 1 MGS per node, since
493            MGC wouldn't know which to connect to */
494         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
495         if (lmi) {
496                 lsi = s2lsi(lmi->lmi_sb);
497                 LCONSOLE_ERROR_MSG(0x15d, "The MGS service was already started"
498                                    " from server %s\n",
499                                    lsi->lsi_ldd->ldd_svname);
500                 RETURN(-EALREADY);
501         }
502
503         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
504
505         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb, mnt);
506
507         if (!rc &&
508             ((rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
509                                        LUSTRE_MGS_OBDNAME, 0, 0))))
510                 server_deregister_mount(LUSTRE_MGS_OBDNAME);
511
512         if (rc)
513                 LCONSOLE_ERROR_MSG(0x15e, "Failed to start MGS '%s' (%d). "
514                                    "Is the 'mgs' module loaded?\n",
515                                    LUSTRE_MGS_OBDNAME, rc);
516         RETURN(rc);
517 }
518
519 static int server_stop_mgs(struct super_block *sb)
520 {
521         struct obd_device *obd;
522         int rc;
523         ENTRY;
524
525         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
526
527         /* There better be only one MGS */
528         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
529         if (!obd) {
530                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
531                 RETURN(-EALREADY);
532         }
533
534         /* The MGS should always stop when we say so */
535         obd->obd_force = 1;
536         rc = class_manual_cleanup(obd);
537         RETURN(rc);
538 }
539
540 DECLARE_MUTEX(mgc_start_lock);
541
542 /* Set up a mgcobd to process startup logs */
543 static int lustre_start_mgc(struct super_block *sb)
544 {
545         struct lustre_handle mgc_conn = {0, };
546         struct obd_connect_data *data = NULL;
547         struct lustre_sb_info *lsi = s2lsi(sb);
548         struct obd_device *obd;
549         struct obd_export *exp;
550         struct obd_uuid *uuid;
551         class_uuid_t uuidc;
552         lnet_nid_t nid;
553         char *mgcname, *niduuid;
554         char *ptr;
555         int recov_bk;
556         int rc = 0, i = 0, j, len;
557         ENTRY;
558
559         LASSERT(lsi->lsi_lmd);
560
561         /* Find the first non-lo MGS nid for our MGC name */
562         if (lsi->lsi_flags & LSI_SERVER) {
563                 ptr = lsi->lsi_ldd->ldd_params;
564                 /* Use mgsnode= nids */
565                 if ((class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0) &&
566                     (class_parse_nid(ptr, &nid, &ptr) == 0)) {
567                         i++;
568                 } else if (IS_MGS(lsi->lsi_ldd)) {
569                         lnet_process_id_t id;
570                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
571                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
572                                         continue;
573                                 nid = id.nid;
574                                 i++;
575                                 break;
576                         }
577                 }
578         } else { /* client */
579                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
580                 ptr = lsi->lsi_lmd->lmd_dev;
581                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
582                         i++;
583         }
584         if (i == 0) {
585                 CERROR("No valid MGS nids found.\n");
586                 RETURN(-EINVAL);
587         }
588
589         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(libcfs_nid2str(nid)) + 1;
590         OBD_ALLOC(mgcname, len);
591         OBD_ALLOC(niduuid, len + 2);
592         if (!mgcname || !niduuid)
593                 GOTO(out_free, rc = -ENOMEM);
594         sprintf(mgcname, "%s%s", LUSTRE_MGC_OBDNAME, libcfs_nid2str(nid));
595
596         mutex_down(&mgc_start_lock);
597
598         obd = class_name2obd(mgcname);
599         if (obd) {
600                 /* Re-using an existing MGC */
601                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
602
603                 recov_bk = 0;
604                 /* If we are restarting the MGS, don't try to keep the MGC's
605                    old connection, or registration will fail. */
606                 if ((lsi->lsi_flags & LSI_SERVER) && IS_MGS(lsi->lsi_ldd)) {
607                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
608                         recov_bk = 1;
609                 }
610
611                 /* Try all connections, but only once (again).
612                    We don't want to block another target from starting
613                    (using its local copy of the log), but we do want to connect
614                    if at all possible. */
615                 recov_bk++;
616                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
617                 rc = obd_set_info_async(obd->obd_self_export,
618                                         sizeof(KEY_INIT_RECOV_BACKUP),
619                                         KEY_INIT_RECOV_BACKUP,
620                                         sizeof(recov_bk), &recov_bk, NULL);
621                 GOTO(out, rc = 0);
622         }
623
624         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
625
626         /* Add the primary nids for the MGS */
627         i = 0;
628         sprintf(niduuid, "%s_%x", mgcname, i);
629         if (lsi->lsi_flags & LSI_SERVER) {
630                 ptr = lsi->lsi_ldd->ldd_params;
631                 if (IS_MGS(lsi->lsi_ldd)) {
632                         /* Use local nids (including LO) */
633                         lnet_process_id_t id;
634                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
635                                 rc = do_lcfg(mgcname, id.nid,
636                                              LCFG_ADD_UUID, niduuid, 0,0,0);
637                         }
638                 } else {
639                         /* Use mgsnode= nids */
640                         if (class_find_param(ptr, PARAM_MGSNODE, &ptr) != 0) {
641                                 CERROR("No MGS nids given.\n");
642                                 GOTO(out_free, rc = -EINVAL);
643                         }
644                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
645                                 rc = do_lcfg(mgcname, nid,
646                                              LCFG_ADD_UUID, niduuid, 0,0,0);
647                                 i++;
648                         }
649                 }
650         } else { /* client */
651                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
652                 ptr = lsi->lsi_lmd->lmd_dev;
653                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
654                         rc = do_lcfg(mgcname, nid,
655                                      LCFG_ADD_UUID, niduuid, 0,0,0);
656                         i++;
657                         /* Stop at the first failover nid */
658                         if (*ptr == ':')
659                                 break;
660                 }
661         }
662         if (i == 0) {
663                 CERROR("No valid MGS nids found.\n");
664                 GOTO(out_free, rc = -EINVAL);
665         }
666         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
667
668         /* Random uuid for MGC allows easier reconnects */
669         OBD_ALLOC_PTR(uuid);
670         ll_generate_random_uuid(uuidc);
671         class_uuid_unparse(uuidc, uuid);
672
673         /* Start the MGC */
674         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
675                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
676                                  niduuid);
677         OBD_FREE_PTR(uuid);
678         if (rc)
679                 GOTO(out_free, rc);
680
681         /* Add any failover MGS nids */
682         i = 1;
683         while ((*ptr == ':' ||
684                 class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0)) {
685                 /* New failover node */
686                 sprintf(niduuid, "%s_%x", mgcname, i);
687                 j = 0;
688                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
689                         j++;
690                         rc = do_lcfg(mgcname, nid,
691                                      LCFG_ADD_UUID, niduuid, 0,0,0);
692                         if (*ptr == ':')
693                                 break;
694                 }
695                 if (j > 0) {
696                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
697                                      niduuid, 0, 0, 0);
698                         i++;
699                 } else {
700                         /* at ":/fsname" */
701                         break;
702                 }
703         }
704         lsi->lsi_lmd->lmd_mgs_failnodes = i;
705
706         obd = class_name2obd(mgcname);
707         if (!obd) {
708                 CERROR("Can't find mgcobd %s\n", mgcname);
709                 GOTO(out_free, rc = -ENOTCONN);
710         }
711
712         /* Keep a refcount of servers/clients who started with "mount",
713            so we know when we can get rid of the mgc. */
714         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
715
716         /* Try all connections, but only once. */
717         recov_bk = 1;
718         rc = obd_set_info_async(obd->obd_self_export,
719                                 sizeof(KEY_INIT_RECOV_BACKUP),
720                                 KEY_INIT_RECOV_BACKUP,
721                                 sizeof(recov_bk), &recov_bk, NULL);
722         if (rc)
723                 /* nonfatal */
724                 CWARN("can't set %s %d\n", KEY_INIT_RECOV_BACKUP, rc);
725         /* We connect to the MGS at setup, and don't disconnect until cleanup */
726         OBD_ALLOC_PTR(data);
727         if (data == NULL)
728                 GOTO(out, rc = -ENOMEM);
729         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_FID |
730                                   OBD_CONNECT_AT;
731         data->ocd_version = LUSTRE_VERSION_CODE;
732         rc = obd_connect(NULL, &mgc_conn, obd, &(obd->obd_uuid), data, NULL);
733         OBD_FREE_PTR(data);
734         if (rc) {
735                 CERROR("connect failed %d\n", rc);
736                 GOTO(out, rc);
737         }
738
739         exp = class_conn2export(&mgc_conn);
740         obd->u.cli.cl_mgc_mgsexp = exp;
741
742 out:
743         /* Keep the mgc info in the sb. Note that many lsi's can point
744            to the same mgc.*/
745         lsi->lsi_mgc = obd;
746 out_free:
747         mutex_up(&mgc_start_lock);
748
749         if (mgcname)
750                 OBD_FREE(mgcname, len);
751         if (niduuid)
752                 OBD_FREE(niduuid, len + 2);
753         RETURN(rc);
754 }
755
756 static int lustre_stop_mgc(struct super_block *sb)
757 {
758         struct lustre_sb_info *lsi = s2lsi(sb);
759         struct obd_device *obd;
760         char *niduuid = 0, *ptr = 0;
761         int i, rc = 0, len = 0;
762         ENTRY;
763
764         if (!lsi)
765                 RETURN(-ENOENT);
766         obd = lsi->lsi_mgc;
767         if (!obd)
768                 RETURN(-ENOENT);
769         lsi->lsi_mgc = NULL;
770
771         mutex_down(&mgc_start_lock);
772         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
773                 /* This is not fatal, every client that stops
774                    will call in here. */
775                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
776                        atomic_read(&obd->u.cli.cl_mgc_refcount));
777                 GOTO(out, rc = -EBUSY);
778         }
779
780         /* The MGC has no recoverable data in any case. 
781          * force shotdown set in umount_begin */
782         obd->obd_no_recov = 1;
783
784         if (obd->u.cli.cl_mgc_mgsexp) {
785                 /* An error is not fatal, if we are unable to send the
786                    disconnect mgs ping evictor cleans up the export */
787                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
788                 if (rc)
789                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
790         }
791
792         /* Save the obdname for cleaning the nid uuids, which are
793            obdname_XX */
794         len = strlen(obd->obd_name) + 6;
795         OBD_ALLOC(niduuid, len);
796         if (niduuid) {
797                 strcpy(niduuid, obd->obd_name);
798                 ptr = niduuid + strlen(niduuid);
799         }
800
801         rc = class_manual_cleanup(obd);
802         if (rc)
803                 GOTO(out, rc);
804
805         /* Clean the nid uuids */
806         if (!niduuid)
807                 RETURN(-ENOMEM);
808         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
809                 sprintf(ptr, "_%x", i);
810                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
811                              niduuid, 0, 0, 0);
812                 if (rc)
813                         CERROR("del MDC UUID %s failed: rc = %d\n",
814                                niduuid, rc);
815         }
816         OBD_FREE(niduuid, len);
817         /* class_import_put will get rid of the additional connections */
818
819 out:
820         mutex_up(&mgc_start_lock);
821         RETURN(rc);
822 }
823
824 /* Since there's only one mgc per node, we have to change it's fs to get
825    access to the right disk. */
826 static int server_mgc_set_fs(struct obd_device *mgc, struct super_block *sb)
827 {
828         struct lustre_sb_info *lsi = s2lsi(sb);
829         int rc;
830         ENTRY;
831
832         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
833
834         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
835         rc = obd_set_info_async(mgc->obd_self_export,
836                                 sizeof(KEY_SET_FS), KEY_SET_FS,
837                                 sizeof(*sb), sb, NULL);
838         if (rc) {
839                 CERROR("can't set_fs %d\n", rc);
840         }
841
842         RETURN(rc);
843 }
844
845 static int server_mgc_clear_fs(struct obd_device *mgc)
846 {
847         int rc;
848         ENTRY;
849
850         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
851
852         rc = obd_set_info_async(mgc->obd_self_export,
853                                 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
854                                 0, NULL, NULL);
855         RETURN(rc);
856 }
857
858 DECLARE_MUTEX(server_start_lock);
859
860 /* Stop MDS/OSS if nobody is using them */
861 static int server_stop_servers(int lddflags, int lsiflags)
862 {
863         struct obd_device *obd = NULL;
864         struct obd_type *type = NULL;
865         int rc = 0;
866         ENTRY;
867
868         mutex_down(&server_start_lock);
869
870         /* Either an MDT or an OST or neither  */
871         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
872         if ((lddflags & LDD_F_SV_TYPE_MDT) &&
873             (obd = class_name2obd(LUSTRE_MDS_OBDNAME))) {
874                 /*FIXME pre-rename, should eventually be LUSTRE_MDT_NAME*/
875                 type = class_search_type(LUSTRE_MDS_NAME);
876         }
877         /* if this was an OST, and there are no more OST's, clean up the OSS */
878         if ((lddflags & LDD_F_SV_TYPE_OST) &&
879             (obd = class_name2obd(LUSTRE_OSS_OBDNAME))) {
880                 type = class_search_type(LUSTRE_OST_NAME);
881         }
882
883         if (obd && (!type || !type->typ_refcnt)) {
884                 int err;
885                 obd->obd_force = 1;
886                 /* obd_fail doesn't mean much on a server obd */
887                 err = class_manual_cleanup(obd);
888                 if (!rc)
889                         rc = err;
890         }
891
892         mutex_up(&server_start_lock);
893
894         RETURN(rc);
895 }
896
897 int server_mti_print(char *title, struct mgs_target_info *mti)
898 {
899         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
900         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
901         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
902         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
903         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
904                   mti->mti_config_ver, mti->mti_flags);
905         return(0);
906 }
907
908 static int server_sb2mti(struct super_block *sb, struct mgs_target_info *mti)
909 {
910         struct lustre_sb_info    *lsi = s2lsi(sb);
911         struct lustre_disk_data  *ldd = lsi->lsi_ldd;
912         lnet_process_id_t         id;
913         int i = 0;
914         ENTRY;
915
916         if (!(lsi->lsi_flags & LSI_SERVER))
917                 RETURN(-EINVAL);
918
919         strncpy(mti->mti_fsname, ldd->ldd_fsname,
920                 sizeof(mti->mti_fsname));
921         strncpy(mti->mti_svname, ldd->ldd_svname,
922                 sizeof(mti->mti_svname));
923
924         mti->mti_nid_count = 0;
925         while (LNetGetId(i++, &id) != -ENOENT) {
926                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
927                         continue;
928                 mti->mti_nids[mti->mti_nid_count] = id.nid;
929                 mti->mti_nid_count++;
930                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
931                         CWARN("Only using first %d nids for %s\n",
932                               mti->mti_nid_count, mti->mti_svname);
933                         break;
934                 }
935         }
936
937         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
938         mti->mti_config_ver = 0;
939         mti->mti_flags = ldd->ldd_flags;
940         mti->mti_stripe_index = ldd->ldd_svindex;
941         memcpy(mti->mti_uuid, ldd->ldd_uuid, sizeof(mti->mti_uuid));
942         if (strlen(ldd->ldd_params) > sizeof(mti->mti_params)) {
943                 CERROR("params too big for mti\n");
944                 RETURN(-ENOMEM);
945         }
946         memcpy(mti->mti_params, ldd->ldd_params, sizeof(mti->mti_params));
947         RETURN(0);
948 }
949
950 /* Register an old or new target with the MGS. If needed MGS will construct
951    startup logs and assign index */
952 int server_register_target(struct super_block *sb)
953 {
954         struct lustre_sb_info *lsi = s2lsi(sb);
955         struct obd_device *mgc = lsi->lsi_mgc;
956         struct lustre_disk_data *ldd = lsi->lsi_ldd;
957         struct mgs_target_info *mti = NULL;
958         int rc;
959         ENTRY;
960
961         LASSERT(mgc);
962
963         if (!(lsi->lsi_flags & LSI_SERVER))
964                 RETURN(-EINVAL);
965
966         OBD_ALLOC_PTR(mti);
967         if (!mti)
968                 RETURN(-ENOMEM);
969         rc = server_sb2mti(sb, mti);
970         if (rc)
971                 GOTO(out, rc);
972
973         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
974                mti->mti_svname, mti->mti_fsname,
975                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
976                mti->mti_flags);
977
978         /* Register the target */
979         /* FIXME use mgc_process_config instead */
980         rc = obd_set_info_async(mgc->u.cli.cl_mgc_mgsexp,
981                                 sizeof(KEY_REGISTER_TARGET), KEY_REGISTER_TARGET,
982                                 sizeof(*mti), mti, NULL);
983         if (rc)
984                 GOTO(out, rc);
985
986         /* Always update our flags */
987         ldd->ldd_flags = mti->mti_flags & ~LDD_F_REWRITE_LDD;
988
989         /* If this flag is set, it means the MGS wants us to change our
990            on-disk data. (So far this means just the index.) */
991         if (mti->mti_flags & LDD_F_REWRITE_LDD) {
992                 char *label;
993                 int err;
994                 CDEBUG(D_MOUNT, "Changing on-disk index from %#x to %#x "
995                        "for %s\n", ldd->ldd_svindex, mti->mti_stripe_index,
996                        mti->mti_svname);
997                 ldd->ldd_svindex = mti->mti_stripe_index;
998                 strncpy(ldd->ldd_svname, mti->mti_svname,
999                         sizeof(ldd->ldd_svname));
1000                 /* or ldd_make_sv_name(ldd); */
1001                 ldd_write(&mgc->obd_lvfs_ctxt, ldd);
1002                 err = fsfilt_set_label(mgc, lsi->lsi_srv_mnt->mnt_sb,
1003                                        mti->mti_svname);
1004                 if (err)
1005                         CERROR("Label set error %d\n", err);
1006                 label = fsfilt_get_label(mgc, lsi->lsi_srv_mnt->mnt_sb);
1007                 if (label)
1008                         CDEBUG(D_MOUNT, "Disk label changed to %s\n", label);
1009
1010                 /* Flush the new ldd to disk */
1011                 fsfilt_sync(mgc, lsi->lsi_srv_mnt->mnt_sb);
1012         }
1013
1014 out:
1015         if (mti)
1016                 OBD_FREE_PTR(mti);
1017         RETURN(rc);
1018 }
1019
1020 /* Start targets */
1021 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
1022 {
1023         struct obd_device *obd;
1024         struct lustre_sb_info *lsi = s2lsi(sb);
1025         struct config_llog_instance cfg;
1026         int rc;
1027         ENTRY;
1028
1029         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_ldd->ldd_svname);
1030
1031 #if 0
1032         /* If we're an MDT, make sure the global MDS is running */
1033         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_MDT) {
1034                 /* make sure the MDS is started */
1035                 mutex_down(&server_start_lock);
1036                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1037                 if (!obd) {
1038                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1039                     /* FIXME pre-rename, should eventually be LUSTRE_MDS_NAME */
1040                                                  LUSTRE_MDT_NAME,
1041                                                  LUSTRE_MDS_OBDNAME"_uuid",
1042                                                  0, 0);
1043                         if (rc) {
1044                                 mutex_up(&server_start_lock);
1045                                 CERROR("failed to start MDS: %d\n", rc);
1046                                 RETURN(rc);
1047                         }
1048                 }
1049                 mutex_up(&server_start_lock);
1050         }
1051 #endif
1052
1053         /* If we're an OST, make sure the global OSS is running */
1054         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
1055                 /* make sure OSS is started */
1056                 mutex_down(&server_start_lock);
1057                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1058                 if (!obd) {
1059                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1060                                                  LUSTRE_OSS_NAME,
1061                                                  LUSTRE_OSS_OBDNAME"_uuid",
1062                                                  0, 0);
1063                         if (rc) {
1064                                 mutex_up(&server_start_lock);
1065                                 CERROR("failed to start OSS: %d\n", rc);
1066                                 RETURN(rc);
1067                         }
1068                 }
1069                 mutex_up(&server_start_lock);
1070         }
1071
1072         /* Set the mgc fs to our server disk.  This allows the MGC
1073            to read and write configs locally. */
1074         rc = server_mgc_set_fs(lsi->lsi_mgc, sb);
1075         if (rc)
1076                 RETURN(rc);
1077
1078         /* Register with MGS */
1079         rc = server_register_target(sb);
1080         if (rc && (lsi->lsi_ldd->ldd_flags &
1081                    (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_UPGRADE14))){
1082                 CERROR("Required registration failed for %s: %d\n",
1083                        lsi->lsi_ldd->ldd_svname, rc);
1084                 if (rc == -EIO) {
1085                         LCONSOLE_ERROR_MSG(0x15f, "Communication error with "
1086                                            "the MGS.  Is the MGS running?\n");
1087                 }
1088                 GOTO(out_mgc, rc);
1089         }
1090         if (rc == -EINVAL) {
1091                 LCONSOLE_ERROR_MSG(0x160, "The MGS is refusing to allow this "
1092                                    "server (%s) to start. Please see messages"
1093                                    " on the MGS node.\n",
1094                                    lsi->lsi_ldd->ldd_svname);
1095                 GOTO(out_mgc, rc);
1096         }
1097         /* non-fatal error of registeration with MGS */
1098         if (rc)
1099                 CDEBUG(D_MOUNT, "Cannot register with MGS: %d\n", rc);
1100
1101         /* Let the target look up the mount using the target's name
1102            (we can't pass the sb or mnt through class_process_config.) */
1103         rc = server_register_mount(lsi->lsi_ldd->ldd_svname, sb, mnt);
1104         if (rc)
1105                 GOTO(out_mgc, rc);
1106
1107         /* Start targets using the llog named for the target */
1108         memset(&cfg, 0, sizeof(cfg));
1109         rc = lustre_process_log(sb, lsi->lsi_ldd->ldd_svname, &cfg);
1110         if (rc) {
1111                 CERROR("failed to start server %s: %d\n",
1112                        lsi->lsi_ldd->ldd_svname, rc);
1113                 GOTO(out_mgc, rc);
1114         }
1115
1116 out_mgc:
1117         /* Release the mgc fs for others to use */
1118         server_mgc_clear_fs(lsi->lsi_mgc);
1119
1120         if (!rc) {
1121                 obd = class_name2obd(lsi->lsi_ldd->ldd_svname);
1122                 if (!obd) {
1123                         CERROR("no server named %s was started\n",
1124                                lsi->lsi_ldd->ldd_svname);
1125                         RETURN(-ENXIO);
1126                 }
1127
1128                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1129                     (OBP(obd, iocontrol))) {
1130                         obd_iocontrol(OBD_IOC_ABORT_RECOVERY,
1131                                       obd->obd_self_export, 0, NULL, NULL);
1132                 }
1133
1134                 /* log has been fully processed */
1135                 obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1136         }
1137
1138         RETURN(rc);
1139 }
1140
1141 /***************** lustre superblock **************/
1142
1143 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
1144 {
1145         struct lustre_sb_info *lsi = NULL;
1146         ENTRY;
1147
1148         OBD_ALLOC(lsi, sizeof(*lsi));
1149         if (!lsi)
1150                 RETURN(NULL);
1151         OBD_ALLOC(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1152         if (!lsi->lsi_lmd) {
1153                 OBD_FREE(lsi, sizeof(*lsi));
1154                 RETURN(NULL);
1155         }
1156
1157         lsi->lsi_lmd->lmd_exclude_count = 0;
1158         s2lsi_nocast(sb) = lsi;
1159         /* we take 1 extra ref for our setup */
1160         atomic_set(&lsi->lsi_mounts, 1);
1161
1162         /* Default umount style */
1163         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
1164
1165         RETURN(lsi);
1166 }
1167
1168 static int lustre_free_lsi(struct super_block *sb)
1169 {
1170         struct lustre_sb_info *lsi = s2lsi(sb);
1171         ENTRY;
1172
1173         if (!lsi)
1174                 RETURN(0);
1175
1176         CDEBUG(D_MOUNT, "Freeing lsi\n");
1177
1178         /* someone didn't call server_put_mount. */
1179         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
1180
1181         if (lsi->lsi_ldd != NULL)
1182                 OBD_FREE(lsi->lsi_ldd, sizeof(*lsi->lsi_ldd));
1183
1184         if (lsi->lsi_lmd != NULL) {
1185                 if (lsi->lsi_lmd->lmd_dev != NULL)
1186                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
1187                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1188                 if (lsi->lsi_lmd->lmd_profile != NULL)
1189                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
1190                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
1191                 if (lsi->lsi_lmd->lmd_opts != NULL)
1192                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
1193                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1194                 if (lsi->lsi_lmd->lmd_exclude_count)
1195                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
1196                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
1197                                  lsi->lsi_lmd->lmd_exclude_count);
1198                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1199         }
1200
1201         LASSERT(lsi->lsi_llsbi == NULL);
1202         OBD_FREE(lsi, sizeof(*lsi));
1203         s2lsi_nocast(sb) = NULL;
1204
1205         RETURN(0);
1206 }
1207
1208 /* The lsi has one reference for every server that is using the disk -
1209    e.g. MDT, MGS, and potentially MGC */
1210 static int lustre_put_lsi(struct super_block *sb)
1211 {
1212         struct lustre_sb_info *lsi = s2lsi(sb);
1213         ENTRY;
1214
1215         LASSERT(lsi);
1216
1217         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
1218
1219         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
1220                 lustre_free_lsi(sb);
1221                 RETURN(1);
1222         }
1223         RETURN(0);
1224 }
1225
1226 /*************** server mount ******************/
1227
1228 /* Kernel mount using mount options in MOUNT_DATA_FILE */
1229 static struct vfsmount *server_kernel_mount(struct super_block *sb)
1230 {
1231         struct lvfs_run_ctxt mount_ctxt;
1232         struct lustre_sb_info *lsi = s2lsi(sb);
1233         struct lustre_disk_data *ldd;
1234         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1235         struct vfsmount *mnt;
1236         char *options = NULL;
1237         unsigned long page, s_flags;
1238         struct page *__page;
1239         int rc;
1240         ENTRY;
1241
1242         OBD_ALLOC(ldd, sizeof(*ldd));
1243         if (!ldd)
1244                 RETURN(ERR_PTR(-ENOMEM));
1245
1246         /* In the past, we have always used flags = 0.
1247            Note ext3/ldiskfs can't be mounted ro. */
1248         s_flags = sb->s_flags;
1249
1250         /* Pre-mount ldiskfs to read the MOUNT_DATA_FILE */
1251         CDEBUG(D_MOUNT, "Pre-mount ldiskfs %s\n", lmd->lmd_dev);
1252         mnt = ll_kern_mount("ldiskfs", s_flags, lmd->lmd_dev, 0);
1253         if (IS_ERR(mnt)) {
1254                 rc = PTR_ERR(mnt);
1255                 CERROR("premount %s:%#lx ldiskfs failed: %d "
1256                         "Is the ldiskfs module available?\n",
1257                         lmd->lmd_dev, s_flags, rc );
1258                 GOTO(out_free, rc);
1259         }
1260
1261         OBD_SET_CTXT_MAGIC(&mount_ctxt);
1262         mount_ctxt.pwdmnt = mnt;
1263         mount_ctxt.pwd = mnt->mnt_root;
1264         mount_ctxt.fs = get_ds();
1265
1266         rc = ldd_parse(&mount_ctxt, ldd);
1267         unlock_mntput(mnt);
1268
1269         if (rc) {
1270                 CERROR("premount parse options failed: rc = %d\n", rc);
1271                 GOTO(out_free, rc);
1272         }
1273
1274         /* Done with our pre-mount, now do the real mount. */
1275
1276         /* Glom up mount options */
1277         OBD_PAGE_ALLOC(__page, CFS_ALLOC_STD);
1278         if (!__page)
1279                 GOTO(out_free, rc = -ENOMEM);
1280         page = (unsigned long)cfs_page_address(__page);
1281
1282         options = (char *)page;
1283         memset(options, 0, CFS_PAGE_SIZE);
1284         strncpy(options, ldd->ldd_mount_opts, CFS_PAGE_SIZE - 2);
1285
1286         /* Add in any mount-line options */
1287         if (lmd->lmd_opts && (*(lmd->lmd_opts) != 0)) {
1288                 int len = CFS_PAGE_SIZE - strlen(options) - 2;
1289                 if (*options != 0)
1290                         strcat(options, ",");
1291                 strncat(options, lmd->lmd_opts, len);
1292         }
1293
1294         /* Special permanent mount flags */
1295         if (IS_OST(ldd))
1296             s_flags |= MS_NOATIME | MS_NODIRATIME;
1297
1298         CDEBUG(D_MOUNT, "kern_mount: %s %s %s\n",
1299                MT_STR(ldd), lmd->lmd_dev, options);
1300         mnt = ll_kern_mount(MT_STR(ldd), s_flags, lmd->lmd_dev,
1301                             (void *)options);
1302         OBD_PAGE_FREE(__page);
1303         if (IS_ERR(mnt)) {
1304                 rc = PTR_ERR(mnt);
1305                 CERROR("ll_kern_mount failed: rc = %d\n", rc);
1306                 GOTO(out_free, rc);
1307         }
1308
1309         lsi->lsi_ldd = ldd;   /* freed at lsi cleanup */
1310         CDEBUG(D_SUPER, "%s: mnt = %p\n", lmd->lmd_dev, mnt);
1311         RETURN(mnt);
1312
1313 out_free:
1314         OBD_FREE(ldd, sizeof(*ldd));
1315         lsi->lsi_ldd = NULL;
1316         RETURN(ERR_PTR(rc));
1317 }
1318
1319 static void server_wait_finished(struct vfsmount *mnt)
1320 {
1321         wait_queue_head_t   waitq;
1322         struct l_wait_info  lwi;
1323         int                 retries = 330;
1324
1325         init_waitqueue_head(&waitq);
1326
1327         while ((atomic_read(&mnt->mnt_count) > 1) && (retries > 0)) {
1328                 LCONSOLE_WARN("Mount still busy with %d refs, waiting for "
1329                               "%d secs...\n",
1330                               atomic_read(&mnt->mnt_count), retries);
1331
1332                 /* Wait for a bit */
1333                 retries -= 5;
1334                 lwi = LWI_TIMEOUT(5 * HZ, NULL, NULL);
1335                 l_wait_event(waitq, 0, &lwi);
1336         }
1337         if (atomic_read(&mnt->mnt_count) > 1) {
1338                 CERROR("Mount %p is still busy (%d refs), giving up.\n",
1339                        mnt, atomic_read(&mnt->mnt_count));
1340         }
1341 }
1342
1343 static void server_put_super(struct super_block *sb)
1344 {
1345         struct lustre_sb_info *lsi = s2lsi(sb);
1346         struct obd_device     *obd;
1347         struct vfsmount       *mnt = lsi->lsi_srv_mnt;
1348         char *tmpname, *extraname = NULL;
1349         int tmpname_sz;
1350         int lddflags = lsi->lsi_ldd->ldd_flags;
1351         int lsiflags = lsi->lsi_flags;
1352         int rc;
1353         ENTRY;
1354
1355         LASSERT(lsiflags & LSI_SERVER);
1356
1357         tmpname_sz = strlen(lsi->lsi_ldd->ldd_svname) + 1;
1358         OBD_ALLOC(tmpname, tmpname_sz);
1359         memcpy(tmpname, lsi->lsi_ldd->ldd_svname, tmpname_sz);
1360         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1361
1362         /* Stop the target */
1363         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) && 
1364             (IS_MDT(lsi->lsi_ldd) || IS_OST(lsi->lsi_ldd))) {
1365                 struct lustre_profile *lprof = NULL;
1366
1367                 /* tell the mgc to drop the config log */
1368                 lustre_end_log(sb, lsi->lsi_ldd->ldd_svname, NULL);
1369
1370                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1371                    If there are any setup/cleanup errors, save the lov
1372                    name for safety cleanup later. */
1373                 lprof = class_get_profile(lsi->lsi_ldd->ldd_svname);
1374                 if (lprof && lprof->lp_dt) {
1375                         OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1376                         strcpy(extraname, lprof->lp_dt);
1377                 }
1378
1379                 obd = class_name2obd(lsi->lsi_ldd->ldd_svname);
1380                 if (obd) {
1381                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1382                         if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
1383                                 obd->obd_fail = 1;
1384                         /* We can't seem to give an error return code
1385                          * to .put_super, so we better make sure we clean up! */
1386                         obd->obd_force = 1;
1387                         class_manual_cleanup(obd);
1388                 } else {
1389                         CERROR("no obd %s\n", lsi->lsi_ldd->ldd_svname);
1390                         server_deregister_mount(lsi->lsi_ldd->ldd_svname);
1391                 }
1392         }
1393
1394         /* If they wanted the mgs to stop separately from the mdt, they
1395            should have put it on a different device. */
1396         if (IS_MGS(lsi->lsi_ldd)) {
1397                 /* stop the mgc before the mgs so the connection gets cleaned
1398                    up */
1399                 lustre_stop_mgc(sb);
1400                 /* if MDS start with --nomgs, don't stop MGS then */
1401                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
1402                         server_stop_mgs(sb);
1403         }
1404
1405         /* Clean the mgc and sb */
1406         rc = lustre_common_put_super(sb);
1407         /* FIXME how can I report a failure to umount? */
1408
1409         /* Wait for the targets to really clean up - can't exit (and let the
1410            sb get destroyed) while the mount is still in use */
1411         server_wait_finished(mnt);
1412
1413         /* drop the One True Mount */
1414         unlock_mntput(mnt);
1415
1416         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1417            until the target is really gone so that our type refcount check
1418            is right. */
1419         server_stop_servers(lddflags, lsiflags);
1420
1421         /* In case of startup or cleanup err, stop related obds */
1422         if (extraname) {
1423                 obd = class_name2obd(extraname);
1424                 if (obd) {
1425                         CWARN("Cleaning orphaned obd %s\n", extraname);
1426                         obd->obd_force = 1;
1427                         class_manual_cleanup(obd);
1428                 }
1429                 OBD_FREE(extraname, strlen(extraname) + 1);
1430         }
1431
1432         LCONSOLE_WARN("server umount %s complete\n", tmpname);
1433         OBD_FREE(tmpname, tmpname_sz);
1434         EXIT;
1435 }
1436
1437 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1438 static void server_umount_begin(struct vfsmount *vfsmnt, int flags)
1439 {
1440         struct super_block *sb = vfsmnt->mnt_sb;
1441 #else
1442 static void server_umount_begin(struct super_block *sb)
1443 {
1444 #endif
1445         struct lustre_sb_info *lsi = s2lsi(sb);
1446         ENTRY;
1447
1448 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1449         if (!(flags & MNT_FORCE)) {
1450                 EXIT;
1451                 return;
1452         }
1453 #endif
1454
1455         CDEBUG(D_MOUNT, "umount -f\n");
1456         /* umount = failover
1457            umount -f = force
1458            no third way to do non-force, non-failover */
1459         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1460         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
1461         EXIT;
1462 }
1463
1464 #ifndef HAVE_STATFS_DENTRY_PARAM
1465 static int server_statfs (struct super_block *sb, struct kstatfs *buf)
1466 {
1467 #else
1468 static int server_statfs (struct dentry *dentry, struct kstatfs *buf)
1469 {
1470         struct super_block *sb = dentry->d_sb;
1471 #endif
1472         struct vfsmount *mnt = s2lsi(sb)->lsi_srv_mnt;
1473         ENTRY;
1474
1475         if (mnt && mnt->mnt_sb && mnt->mnt_sb->s_op->statfs) {
1476 #ifdef HAVE_STATFS_DENTRY_PARAM
1477                 int rc = mnt->mnt_sb->s_op->statfs(mnt->mnt_root, buf);
1478 #else
1479                 int rc = mnt->mnt_sb->s_op->statfs(mnt->mnt_sb, buf);
1480 #endif
1481                 if (!rc) {
1482                         buf->f_type = sb->s_magic;
1483                         RETURN(0);
1484                 }
1485         }
1486
1487         /* just return 0 */
1488         buf->f_type = sb->s_magic;
1489         buf->f_bsize = sb->s_blocksize;
1490         buf->f_blocks = 1;
1491         buf->f_bfree = 0;
1492         buf->f_bavail = 0;
1493         buf->f_files = 1;
1494         buf->f_ffree = 0;
1495         buf->f_namelen = NAME_MAX;
1496         RETURN(0);
1497 }
1498
1499 static struct super_operations server_ops =
1500 {
1501         .put_super      = server_put_super,
1502         .umount_begin   = server_umount_begin, /* umount -f */
1503         .statfs         = server_statfs,
1504 };
1505
1506 #define log2(n) ffz(~(n))
1507 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1508
1509 static int server_fill_super_common(struct super_block *sb)
1510 {
1511         struct inode *root = 0;
1512         ENTRY;
1513
1514         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1515
1516         sb->s_blocksize = 4096;
1517         sb->s_blocksize_bits = log2(sb->s_blocksize);
1518         sb->s_magic = LUSTRE_SUPER_MAGIC;
1519         sb->s_maxbytes = 0; //PAGE_CACHE_MAXBYTES;
1520         sb->s_flags |= MS_RDONLY;
1521         sb->s_op = &server_ops;
1522
1523         root = new_inode(sb);
1524         if (!root) {
1525                 CERROR("Can't make root inode\n");
1526                 RETURN(-EIO);
1527         }
1528
1529         /* returns -EIO for every operation */
1530         /* make_bad_inode(root); -- badness - can't umount */
1531         /* apparently we need to be a directory for the mount to finish */
1532         root->i_mode = S_IFDIR;
1533
1534         sb->s_root = d_alloc_root(root);
1535         if (!sb->s_root) {
1536                 CERROR("Can't make root dentry\n");
1537                 iput(root);
1538                 RETURN(-EIO);
1539         }
1540
1541         RETURN(0);
1542 }
1543
1544 static int server_fill_super(struct super_block *sb)
1545 {
1546         struct lustre_sb_info *lsi = s2lsi(sb);
1547         struct vfsmount *mnt;
1548         int rc;
1549         ENTRY;
1550
1551         /* the One True Mount */
1552         mnt = server_kernel_mount(sb);
1553         if (IS_ERR(mnt)) {
1554                 rc = PTR_ERR(mnt);
1555                 CERROR("Unable to mount device %s: %d\n",
1556                       lsi->lsi_lmd->lmd_dev, rc);
1557                 lustre_put_lsi(sb);
1558                 GOTO(out, rc);
1559         }
1560         lsi->lsi_srv_mnt = mnt;
1561
1562         LASSERT(lsi->lsi_ldd);
1563         CDEBUG(D_MOUNT, "Found service %s for fs '%s' on device %s\n",
1564                lsi->lsi_ldd->ldd_svname, lsi->lsi_ldd->ldd_fsname,
1565                lsi->lsi_lmd->lmd_dev);
1566
1567         if (class_name2obd(lsi->lsi_ldd->ldd_svname)) {
1568                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
1569                                    "running. Double-mount may have compromised"
1570                                    " the disk journal.\n",
1571                                    lsi->lsi_ldd->ldd_svname);
1572                 unlock_mntput(mnt);
1573                 lustre_put_lsi(sb);
1574                 GOTO(out, rc = -EALREADY);
1575         }
1576
1577         /* start MGS before MGC */
1578         if (IS_MGS(lsi->lsi_ldd) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)) {
1579                 rc = server_start_mgs(sb);
1580                 if (rc)
1581                         GOTO(out_mnt, rc);
1582         }
1583
1584         rc = lustre_start_mgc(sb);
1585         if (rc)
1586                 GOTO(out_mnt, rc);
1587
1588         /* Set up all obd devices for service */
1589         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1590                 (IS_OST(lsi->lsi_ldd) || IS_MDT(lsi->lsi_ldd))) {
1591                 rc = server_start_targets(sb, mnt);
1592                 if (rc < 0) {
1593                         CERROR("Unable to start targets: %d\n", rc);
1594                         GOTO(out_mnt, rc);
1595                 }
1596         /* FIXME overmount client here,
1597            or can we just start a client log and client_fill_super on this sb?
1598            We need to make sure server_put_super gets called too - ll_put_super
1599            calls lustre_common_put_super; check there for LSI_SERVER flag,
1600            call s_p_s if so.
1601            Probably should start client from new thread so we can return.
1602            Client will not finish until all servers are connected.
1603            Note - MGS-only server does NOT get a client, since there is no
1604            lustre fs associated - the MGS is for all lustre fs's */
1605         }
1606
1607         rc = server_fill_super_common(sb);
1608         if (rc)
1609                 GOTO(out_mnt, rc);
1610
1611         LCONSOLE_WARN("Server %s on device %s has started\n",
1612                       lsi->lsi_ldd->ldd_svname, lsi->lsi_lmd->lmd_dev);
1613
1614         RETURN(0);
1615
1616 out_mnt:
1617         server_put_super(sb);
1618 out:
1619         RETURN(rc);
1620 }
1621
1622 /* Get the index from the obd name.
1623    rc = server type, or
1624    rc < 0  on error
1625    if endptr isn't NULL it is set to end of name */
1626 int server_name2index(char *svname, __u32 *idx, char **endptr)
1627 {
1628         unsigned long index;
1629         int rc;
1630         char *dash = strchr(svname, '-');
1631         if (!dash)
1632                 return(-EINVAL);
1633
1634         if (strncmp(dash + 1, "MDT", 3) == 0)
1635                 rc = LDD_F_SV_TYPE_MDT;
1636         else if (strncmp(dash + 1, "OST", 3) == 0)
1637                 rc = LDD_F_SV_TYPE_OST;
1638         else
1639                 return(-EINVAL);
1640
1641         index = simple_strtoul(dash + 4, endptr, 16);
1642         *idx = index;
1643         return rc;
1644 }
1645
1646 /*************** mount common betweeen server and client ***************/
1647
1648 /* Common umount */
1649 int lustre_common_put_super(struct super_block *sb)
1650 {
1651         int rc;
1652         ENTRY;
1653
1654         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
1655
1656         /* Drop a ref to the MGC */
1657         rc = lustre_stop_mgc(sb);
1658         if (rc && (rc != -ENOENT)) {
1659                 if (rc != -EBUSY) {
1660                         CERROR("Can't stop MGC: %d\n", rc);
1661                         RETURN(rc);
1662                 }
1663                 /* BUSY just means that there's some other obd that
1664                    needs the mgc.  Let him clean it up. */
1665                 CDEBUG(D_MOUNT, "MGC still in use\n");
1666         }
1667         /* Drop a ref to the mounted disk */
1668         lustre_put_lsi(sb);
1669         RETURN(rc);
1670 }
1671
1672 #if 0
1673 static void lmd_print(struct lustre_mount_data *lmd)
1674 {
1675         int i;
1676
1677         PRINT_CMD(PRINT_MASK, "  mount data:\n");
1678         if (lmd_is_client(lmd))
1679                 PRINT_CMD(PRINT_MASK, "profile: %s\n", lmd->lmd_profile);
1680         PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
1681         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
1682         if (lmd->lmd_opts)
1683                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
1684         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1685                 PRINT_CMD(PRINT_MASK, "exclude %d:  OST%04x\n", i,
1686                           lmd->lmd_exclude[i]);
1687         }
1688 }
1689 #endif
1690
1691 /* Is this server on the exclusion list */
1692 int lustre_check_exclusion(struct super_block *sb, char *svname)
1693 {
1694         struct lustre_sb_info *lsi = s2lsi(sb);
1695         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1696         __u32 index;
1697         int i, rc;
1698         ENTRY;
1699
1700         rc = server_name2index(svname, &index, NULL);
1701         if (rc != LDD_F_SV_TYPE_OST)
1702                 /* Only exclude OSTs */
1703                 RETURN(0);
1704
1705         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1706                index, lmd->lmd_exclude_count, lmd->lmd_dev);
1707
1708         for(i = 0; i < lmd->lmd_exclude_count; i++) {
1709                 if (index == lmd->lmd_exclude[i]) {
1710                         CWARN("Excluding %s (on exclusion list)\n", svname);
1711                         RETURN(1);
1712                 }
1713         }
1714         RETURN(0);
1715 }
1716
1717 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1718 static int lmd_make_exclusion(struct lustre_mount_data *lmd, char *ptr)
1719 {
1720         char *s1 = ptr, *s2;
1721         __u32 index, *exclude_list;
1722         int rc = 0, devmax;
1723         ENTRY;
1724
1725         /* The shortest an ost name can be is 8 chars: -OST0000.
1726            We don't actually know the fsname at this time, so in fact
1727            a user could specify any fsname. */
1728         devmax = strlen(ptr) / 8 + 1;
1729
1730         /* temp storage until we figure out how many we have */
1731         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
1732         if (!exclude_list)
1733                 RETURN(-ENOMEM);
1734
1735         /* we enter this fn pointing at the '=' */
1736         while (*s1 && *s1 != ' ' && *s1 != ',') {
1737                 s1++;
1738                 rc = server_name2index(s1, &index, &s2);
1739                 if (rc < 0) {
1740                         CERROR("Can't parse server name '%s'\n", s1);
1741                         break;
1742                 }
1743                 if (rc == LDD_F_SV_TYPE_OST)
1744                         exclude_list[lmd->lmd_exclude_count++] = index;
1745                 else
1746                         CDEBUG(D_MOUNT, "ignoring exclude %.7s\n", s1);
1747                 s1 = s2;
1748                 /* now we are pointing at ':' (next exclude)
1749                    or ',' (end of excludes) */
1750                 if (lmd->lmd_exclude_count >= devmax)
1751                         break;
1752         }
1753         if (rc >= 0) /* non-err */
1754                 rc = 0;
1755
1756         if (lmd->lmd_exclude_count) {
1757                 /* permanent, freed in lustre_free_lsi */
1758                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
1759                           lmd->lmd_exclude_count);
1760                 if (lmd->lmd_exclude) {
1761                         memcpy(lmd->lmd_exclude, exclude_list,
1762                                sizeof(index) * lmd->lmd_exclude_count);
1763                 } else {
1764                         rc = -ENOMEM;
1765                         lmd->lmd_exclude_count = 0;
1766                 }
1767         }
1768         OBD_FREE(exclude_list, sizeof(index) * devmax);
1769         RETURN(rc);
1770 }
1771
1772 /* mount -v -t lustre uml1:uml2:/lustre-client /mnt/lustre */
1773 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1774 {
1775         char *s1, *s2, *devname = NULL;
1776         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1777         int rc = 0;
1778         ENTRY;
1779
1780         LASSERT(lmd);
1781         if (!options) {
1782                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1783                                    "/sbin/mount.lustre is installed.\n");
1784                 RETURN(-EINVAL);
1785         }
1786
1787         /* Options should be a string - try to detect old lmd data */
1788         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1789                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1790                                    "/sbin/mount.lustre.  Please install "
1791                                    "version %s\n", LUSTRE_VERSION_STRING);
1792                 RETURN(-EINVAL);
1793         }
1794         lmd->lmd_magic = LMD_MAGIC;
1795
1796         /* Set default flags here */
1797
1798         s1 = options;
1799         while (*s1) {
1800                 int clear = 0;
1801                 /* Skip whitespace and extra commas */
1802                 while (*s1 == ' ' || *s1 == ',')
1803                         s1++;
1804
1805                 /* Client options are parsed in ll_options: eg. flock,
1806                    user_xattr, acl */
1807
1808                 /* Parse non-ldiskfs options here. Rather than modifying
1809                    ldiskfs, we just zero these out here */
1810                 if (strncmp(s1, "abort_recov", 11) == 0) {
1811                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1812                         clear++;
1813                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1814                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1815                         clear++;
1816                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1817                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1818                         clear++;
1819                 /* ost exclusion list */
1820                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1821                         rc = lmd_make_exclusion(lmd, s1 + 7);
1822                         if (rc)
1823                                 goto invalid;
1824                         clear++;
1825                 }
1826                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1827                    end of the options. */
1828                 else if (strncmp(s1, "device=", 7) == 0) {
1829                         devname = s1 + 7;
1830                         /* terminate options right before device.  device
1831                            must be the last one. */
1832                         *s1 = '\0';
1833                         break;
1834                 }
1835
1836                 /* Find next opt */
1837                 s2 = strchr(s1, ',');
1838                 if (s2 == NULL) {
1839                         if (clear)
1840                                 *s1 = '\0';
1841                         break;
1842                 }
1843                 s2++;
1844                 if (clear)
1845                         memmove(s1, s2, strlen(s2) + 1);
1846                 else
1847                         s1 = s2;
1848         }
1849
1850         if (!devname) {
1851                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1852                                    "(need mount option 'device=...')\n");
1853                 goto invalid;
1854         }
1855
1856         s1 = strrchr(devname, ':');
1857         if (s1) {
1858                 lmd->lmd_flags = LMD_FLG_CLIENT;
1859                 /* Remove leading /s from fsname */
1860                 while (*++s1 == '/') ;
1861                 /* Freed in lustre_free_lsi */
1862                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
1863                 if (!lmd->lmd_profile)
1864                         RETURN(-ENOMEM);
1865                 sprintf(lmd->lmd_profile, "%s-client", s1);
1866         }
1867
1868         /* Freed in lustre_free_lsi */
1869         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1870         if (!lmd->lmd_dev)
1871                 RETURN(-ENOMEM);
1872         strcpy(lmd->lmd_dev, devname);
1873
1874         /* Save mount options */
1875         s1 = options + strlen(options) - 1;
1876         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1877                 *s1-- = 0;
1878         if (*options != 0) {
1879                 /* Freed in lustre_free_lsi */
1880                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1881                 if (!lmd->lmd_opts)
1882                         RETURN(-ENOMEM);
1883                 strcpy(lmd->lmd_opts, options);
1884         }
1885
1886         lmd->lmd_magic = LMD_MAGIC;
1887
1888         RETURN(rc);
1889
1890 invalid:
1891         CERROR("Bad mount options %s\n", options);
1892         RETURN(-EINVAL);
1893 }
1894
1895
1896 /* Common mount */
1897 int lustre_fill_super(struct super_block *sb, void *data, int silent)
1898 {
1899         struct lustre_mount_data *lmd;
1900         struct lustre_sb_info *lsi;
1901         int rc;
1902         ENTRY;
1903
1904         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1905
1906         lsi = lustre_init_lsi(sb);
1907         if (!lsi)
1908                 RETURN(-ENOMEM);
1909         lmd = lsi->lsi_lmd;
1910
1911         /* Figure out the lmd from the mount options */
1912         if (lmd_parse((char *)data, lmd)) {
1913                 lustre_put_lsi(sb);
1914                 RETURN(-EINVAL);
1915         }
1916
1917         if (lmd_is_client(lmd)) {
1918                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1919                 if (!client_fill_super) {
1920                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1921                                            "client mount! Is the 'lustre' "
1922                                            "module loaded?\n");
1923                         rc = -ENODEV;
1924                 } else {
1925                         rc = lustre_start_mgc(sb);
1926                         if (rc) {
1927                                 lustre_stop_mgc(sb);
1928                                 goto out;
1929                         }
1930                         /* Connect and start */
1931                         /* (should always be ll_fill_super) */
1932                         rc = (*client_fill_super)(sb);
1933                         /* c_f_s will call lustre_common_put_super on failure */
1934                 }
1935         } else {
1936                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1937                 lsi->lsi_flags |= LSI_SERVER;
1938                 rc = server_fill_super(sb);
1939                 /* s_f_s calls lustre_start_mgc after the mount because we need
1940                    the MGS nids which are stored on disk.  Plus, we may
1941                    need to start the MGS first. */
1942                 /* s_f_s will call server_put_super on failure */
1943         }
1944
1945 out:
1946         if (rc){
1947                 CERROR("Unable to mount %s (%d)\n",
1948                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1949         } else {
1950                 CDEBUG(D_SUPER, "mount %s complete\n", lmd->lmd_dev);
1951         }
1952         RETURN(rc);
1953 }
1954
1955
1956 /* We can't call ll_fill_super by name because it lives in a module that
1957    must be loaded after this one. */
1958 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb))
1959 {
1960         client_fill_super = cfs;
1961 }
1962
1963 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1964 {
1965         kill_super_cb = cfs;
1966 }
1967
1968 /***************** FS registration ******************/
1969
1970 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1971 struct super_block * lustre_get_sb(struct file_system_type *fs_type,
1972                                int flags, const char *devname, void * data)
1973 {
1974         /* calls back in fill super */
1975         /* we could append devname= onto options (*data) here,
1976            but 2.4 doesn't get devname.  So we do it in mount_lustre.c */
1977         return get_sb_nodev(fs_type, flags, data, lustre_fill_super);
1978 }
1979 #else
1980 int lustre_get_sb(struct file_system_type *fs_type,
1981                                int flags, const char *devname, void * data,
1982                                struct vfsmount *mnt)
1983 {
1984         /* calls back in fill super */
1985         /* we could append devname= onto options (*data) here,
1986            but 2.4 doesn't get devname.  So we do it in mount_lustre.c */
1987         return get_sb_nodev(fs_type, flags, data, lustre_fill_super, mnt);
1988 }
1989 #endif
1990
1991 void lustre_kill_super(struct super_block *sb)
1992 {
1993         struct lustre_sb_info *lsi = s2lsi(sb);
1994
1995         if (kill_super_cb && lsi &&(lsi->lsi_flags & LSI_SERVER))
1996                 (*kill_super_cb)(sb);
1997
1998         kill_anon_super(sb);
1999 }
2000
2001 struct file_system_type lustre_fs_type = {
2002         .owner        = THIS_MODULE,
2003         .name         = "lustre",
2004         .get_sb       = lustre_get_sb,
2005         .kill_sb      = lustre_kill_super,
2006         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
2007                         LL_RENAME_DOES_D_MOVE,
2008 };
2009
2010 int lustre_register_fs(void)
2011 {
2012         return register_filesystem(&lustre_fs_type);
2013 }
2014
2015 int lustre_unregister_fs(void)
2016 {
2017         return unregister_filesystem(&lustre_fs_type);
2018 }
2019
2020 EXPORT_SYMBOL(lustre_register_client_fill_super);
2021 EXPORT_SYMBOL(lustre_register_kill_super_cb);
2022 EXPORT_SYMBOL(lustre_common_put_super);
2023 EXPORT_SYMBOL(lustre_process_log);
2024 EXPORT_SYMBOL(lustre_end_log);
2025 EXPORT_SYMBOL(server_get_mount);
2026 EXPORT_SYMBOL(server_get_mount_2);
2027 EXPORT_SYMBOL(server_put_mount);
2028 EXPORT_SYMBOL(server_put_mount_2);
2029 EXPORT_SYMBOL(server_register_target);
2030 EXPORT_SYMBOL(server_name2index);
2031 EXPORT_SYMBOL(server_mti_print);
2032 EXPORT_SYMBOL(do_lcfg);
2033
2034