Whamcloud - gitweb
89572b5134af712fd6e22620c13589249b8ffb09
[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 struct list_head server_mount_info_list = LIST_HEAD_INIT(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 ocd = { 0 };
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                                         strlen(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                                 strlen(KEY_INIT_RECOV_BACKUP),
720                                 KEY_INIT_RECOV_BACKUP,
721                                 sizeof(recov_bk), &recov_bk, NULL);
722         if (rc)
723                 /* nonfatal */
724                 CERROR("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         rc = obd_connect(NULL, &mgc_conn, obd, &(obd->obd_uuid), &ocd);
727         if (rc) {
728                 CERROR("connect failed %d\n", rc);
729                 GOTO(out, rc);
730         }
731
732         exp = class_conn2export(&mgc_conn);
733         obd->u.cli.cl_mgc_mgsexp = exp;
734
735 out:
736         /* Keep the mgc info in the sb. Note that many lsi's can point
737            to the same mgc.*/
738         lsi->lsi_mgc = obd;
739 out_free:
740         mutex_up(&mgc_start_lock);
741
742         if (mgcname)
743                 OBD_FREE(mgcname, len);
744         if (niduuid)
745                 OBD_FREE(niduuid, len + 2);
746         RETURN(rc);
747 }
748
749 static int lustre_stop_mgc(struct super_block *sb)
750 {
751         struct lustre_sb_info *lsi = s2lsi(sb);
752         struct obd_device *obd;
753         char *niduuid = 0, *ptr = 0;
754         int i, rc = 0, len = 0;
755         ENTRY;
756
757         if (!lsi)
758                 RETURN(-ENOENT);
759         obd = lsi->lsi_mgc;
760         if (!obd)
761                 RETURN(-ENOENT);
762
763         lsi->lsi_mgc = NULL;
764         mutex_down(&mgc_start_lock);
765         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
766                 /* This is not fatal, every client that stops
767                    will call in here. */
768                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
769                        atomic_read(&obd->u.cli.cl_mgc_refcount));
770                 GOTO(out, rc = -EBUSY);
771         }
772
773         /* MGC must always stop */
774         obd->obd_force = 1;
775         /* client_disconnect_export uses the no_recov flag to decide whether it
776            should disconnect or just invalidate.  (The MGC has no
777            recoverable data in any case.) */
778         obd->obd_no_recov = 1;
779
780         if (obd->u.cli.cl_mgc_mgsexp)
781                 obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
782
783         /* Save the obdname for cleaning the nid uuids, which are
784            obdname_XX */
785         len = strlen(obd->obd_name) + 6;
786         OBD_ALLOC(niduuid, len);
787         if (niduuid) {
788                 strcpy(niduuid, obd->obd_name);
789                 ptr = niduuid + strlen(niduuid);
790         }
791
792         rc = class_manual_cleanup(obd);
793         if (rc)
794                 GOTO(out, rc);
795
796         /* Clean the nid uuids */
797         if (!niduuid)
798                 RETURN(-ENOMEM);
799         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
800                 sprintf(ptr, "_%x", i);
801                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
802                              niduuid, 0, 0, 0);
803                 if (rc)
804                         CERROR("del MDC UUID %s failed: rc = %d\n",
805                                niduuid, rc);
806         }
807         OBD_FREE(niduuid, len);
808         /* class_import_put will get rid of the additional connections */
809
810 out:
811         mutex_up(&mgc_start_lock);
812         RETURN(rc);
813 }
814
815 /* Since there's only one mgc per node, we have to change it's fs to get
816    access to the right disk. */
817 static int server_mgc_set_fs(struct obd_device *mgc, struct super_block *sb)
818 {
819         struct lustre_sb_info *lsi = s2lsi(sb);
820         int rc;
821         ENTRY;
822
823         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
824
825         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
826         rc = obd_set_info_async(mgc->obd_self_export,
827                                 strlen("set_fs"), "set_fs",
828                                 sizeof(*sb), sb, NULL);
829         if (rc) {
830                 CERROR("can't set_fs %d\n", rc);
831         }
832
833         RETURN(rc);
834 }
835
836 static int server_mgc_clear_fs(struct obd_device *mgc)
837 {
838         int rc;
839         ENTRY;
840
841         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
842
843         rc = obd_set_info_async(mgc->obd_self_export,
844                                 strlen("clear_fs"), "clear_fs",
845                                 0, NULL, NULL);
846         RETURN(rc);
847 }
848
849 DECLARE_MUTEX(server_start_lock);
850
851 /* Stop MDS/OSS if nobody is using them */
852 static int server_stop_servers(int lddflags, int lsiflags)
853 {
854         struct obd_device *obd = NULL;
855         struct obd_type *type = NULL;
856         int rc = 0;
857         ENTRY;
858
859         mutex_down(&server_start_lock);
860
861         /* Either an MDT or an OST or neither  */
862         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
863         if ((lddflags & LDD_F_SV_TYPE_MDT) &&
864             (obd = class_name2obd(LUSTRE_MDS_OBDNAME))) {
865                 /*FIXME pre-rename, should eventually be LUSTRE_MDT_NAME*/
866                 type = class_search_type(LUSTRE_MDS_NAME);
867         }
868         /* if this was an OST, and there are no more OST's, clean up the OSS */
869         if ((lddflags & LDD_F_SV_TYPE_OST) &&
870             (obd = class_name2obd(LUSTRE_OSS_OBDNAME))) {
871                 type = class_search_type(LUSTRE_OST_NAME);
872         }
873
874         if (obd && (!type || !type->typ_refcnt)) {
875                 int err;
876                 obd->obd_force = 1;
877                 /* obd_fail doesn't mean much on a server obd */
878                 err = class_manual_cleanup(obd);
879                 if (!rc)
880                         rc = err;
881         }
882
883         mutex_up(&server_start_lock);
884
885         RETURN(rc);
886 }
887
888 int server_mti_print(char *title, struct mgs_target_info *mti)
889 {
890         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
891         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
892         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
893         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
894         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
895                   mti->mti_config_ver, mti->mti_flags);
896         return(0);
897 }
898
899 static
900 int mti_set_sec_opts(struct mgs_target_info *mti, struct lustre_mount_data *lmd)
901 {
902         char *s1, *s2;
903
904         if (lmd->lmd_sec_mdt == NULL && lmd->lmd_sec_cli == NULL) {
905                 /* just let on-disk params do its work. but we have an
906                  * assumption that any changes of on-disk data by tune2fs
907                  * should lead to server rewrite log.
908                  */
909                 return 0;
910         }
911
912         /* filter out existing sec options */
913         s1 = mti->mti_params;
914         while (*s1) {
915                 int clear;
916
917                 while (*s1 == ' ')
918                         s1++;
919
920                 if (strncmp(s1, PARAM_SEC_RPC_MDT,
921                             sizeof(PARAM_SEC_RPC_MDT) - 1) == 0 ||
922                     strncmp(s1, PARAM_SEC_RPC_CLI,
923                             sizeof(PARAM_SEC_RPC_CLI) - 1) == 0)
924                         clear = 1;
925                 else
926                         clear = 0;
927
928                 s2 = strchr(s1, ' ');
929                 if (s2 == NULL) {
930                         if (clear)
931                                 *s1 = '\0';
932                         break;
933                 }
934                 s2++;
935                 if (clear)
936                         memmove(s1, s2, strlen(s2) + 1);
937                 else
938                         s1 = s2;
939         }
940
941         /* append sec options from lmd */
942         /* FIXME add flag LDD_F_UPDATE after mountconf start supporting
943          * log updating.
944          */
945         if (lmd->lmd_sec_mdt) {
946                 if (strlen(mti->mti_params) + strlen(lmd->lmd_sec_mdt) +
947                     sizeof(PARAM_SEC_RPC_MDT) + 1 >= sizeof(mti->mti_params)) {
948                         CERROR("security params too big for mti\n");
949                         return -ENOMEM;
950                 }
951                 strcat(mti->mti_params, " "PARAM_SEC_RPC_MDT);
952                 strcat(mti->mti_params, lmd->lmd_sec_mdt);
953                 //mti->mti_flags |= LDD_F_UPDATE;
954         }
955         if (lmd->lmd_sec_cli) {
956                 if (strlen(mti->mti_params) + strlen(lmd->lmd_sec_cli) +
957                     sizeof(PARAM_SEC_RPC_CLI) + 2 > sizeof(mti->mti_params)) {
958                         CERROR("security params too big for mti\n");
959                         return -ENOMEM;
960                 }
961                 strcat(mti->mti_params, " "PARAM_SEC_RPC_CLI);
962                 strcat(mti->mti_params, lmd->lmd_sec_cli);
963                 //mti->mti_flags |= LDD_F_UPDATE;
964         }
965
966         return 0;
967 }
968
969 static int server_sb2mti(struct super_block *sb, struct mgs_target_info *mti)
970 {
971         struct lustre_sb_info    *lsi = s2lsi(sb);
972         struct lustre_disk_data  *ldd = lsi->lsi_ldd;
973         struct lustre_mount_data *lmd = lsi->lsi_lmd;
974         lnet_process_id_t         id;
975         int i = 0;
976         ENTRY;
977
978         if (!(lsi->lsi_flags & LSI_SERVER))
979                 RETURN(-EINVAL);
980
981         strncpy(mti->mti_fsname, ldd->ldd_fsname,
982                 sizeof(mti->mti_fsname));
983         strncpy(mti->mti_svname, ldd->ldd_svname,
984                 sizeof(mti->mti_svname));
985
986         mti->mti_nid_count = 0;
987         while (LNetGetId(i++, &id) != -ENOENT) {
988                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
989                         continue;
990                 mti->mti_nids[mti->mti_nid_count] = id.nid;
991                 mti->mti_nid_count++;
992                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
993                         CWARN("Only using first %d nids for %s\n",
994                               mti->mti_nid_count, mti->mti_svname);
995                         break;
996                 }
997         }
998
999         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1000         mti->mti_config_ver = 0;
1001         mti->mti_flags = ldd->ldd_flags;
1002         mti->mti_stripe_index = ldd->ldd_svindex;
1003         memcpy(mti->mti_uuid, ldd->ldd_uuid, sizeof(mti->mti_uuid));
1004         if (strlen(ldd->ldd_params) > sizeof(mti->mti_params)) {
1005                 CERROR("params too big for mti\n");
1006                 RETURN(-ENOMEM);
1007         }
1008         memcpy(mti->mti_params, ldd->ldd_params, sizeof(mti->mti_params));
1009
1010         RETURN(mti_set_sec_opts(mti, lmd));
1011 }
1012
1013 /* Register an old or new target with the MGS. If needed MGS will construct
1014    startup logs and assign index */
1015 int server_register_target(struct super_block *sb)
1016 {
1017         struct lustre_sb_info *lsi = s2lsi(sb);
1018         struct obd_device *mgc = lsi->lsi_mgc;
1019         struct lustre_disk_data *ldd = lsi->lsi_ldd;
1020         struct mgs_target_info *mti = NULL;
1021         int rc;
1022         ENTRY;
1023
1024         LASSERT(mgc);
1025
1026         if (!(lsi->lsi_flags & LSI_SERVER))
1027                 RETURN(-EINVAL);
1028
1029         OBD_ALLOC_PTR(mti);
1030         if (!mti)
1031                 RETURN(-ENOMEM);
1032         rc = server_sb2mti(sb, mti);
1033         if (rc)
1034                 GOTO(out, rc);
1035
1036         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1037                mti->mti_svname, mti->mti_fsname,
1038                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1039                mti->mti_flags);
1040
1041         /* Register the target */
1042         /* FIXME use mgc_process_config instead */
1043         rc = obd_set_info_async(mgc->u.cli.cl_mgc_mgsexp,
1044                                 strlen("register_target"), "register_target",
1045                                 sizeof(*mti), mti, NULL);
1046         if (rc) {
1047                 CERROR("registration with the MGS failed (%d)\n", rc);
1048                 GOTO(out, rc);
1049         }
1050
1051         /* Always update our flags */
1052         ldd->ldd_flags = mti->mti_flags & ~LDD_F_REWRITE_LDD;
1053
1054         /* If this flag is set, it means the MGS wants us to change our
1055            on-disk data. (So far this means just the index.) */
1056         if (mti->mti_flags & LDD_F_REWRITE_LDD) {
1057                 char *label;
1058                 int err;
1059                 CDEBUG(D_MOUNT, "Changing on-disk index from %#x to %#x "
1060                        "for %s\n", ldd->ldd_svindex, mti->mti_stripe_index,
1061                        mti->mti_svname);
1062                 ldd->ldd_svindex = mti->mti_stripe_index;
1063                 strncpy(ldd->ldd_svname, mti->mti_svname,
1064                         sizeof(ldd->ldd_svname));
1065                 /* or ldd_make_sv_name(ldd); */
1066                 ldd_write(&mgc->obd_lvfs_ctxt, ldd);
1067                 err = fsfilt_set_label(mgc, lsi->lsi_srv_mnt->mnt_sb,
1068                                        mti->mti_svname);
1069                 if (err)
1070                         CERROR("Label set error %d\n", err);
1071                 label = fsfilt_get_label(mgc, lsi->lsi_srv_mnt->mnt_sb);
1072                 if (label)
1073                         CDEBUG(D_MOUNT, "Disk label changed to %s\n", label);
1074
1075                 /* Flush the new ldd to disk */
1076                 fsfilt_sync(mgc, lsi->lsi_srv_mnt->mnt_sb);
1077         }
1078
1079 out:
1080         if (mti)
1081                 OBD_FREE_PTR(mti);
1082         RETURN(rc);
1083 }
1084
1085 /* Start targets */
1086 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
1087 {
1088         struct obd_device *obd;
1089         struct lustre_sb_info *lsi = s2lsi(sb);
1090         struct config_llog_instance cfg;
1091         int rc;
1092         ENTRY;
1093
1094         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_ldd->ldd_svname);
1095
1096 #if 0
1097         /* If we're an MDT, make sure the global MDS is running */
1098         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_MDT) {
1099                 /* make sure the MDS is started */
1100                 mutex_down(&server_start_lock);
1101                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1102                 if (!obd) {
1103                         rc = lustre_start_simple(LUSTRE_MDS_OBDNAME,
1104                     /* FIXME pre-rename, should eventually be LUSTRE_MDS_NAME */
1105                                                  LUSTRE_MDT_NAME,
1106                                                  LUSTRE_MDS_OBDNAME"_uuid",
1107                                                  0, 0);
1108                         if (rc) {
1109                                 mutex_up(&server_start_lock);
1110                                 CERROR("failed to start MDS: %d\n", rc);
1111                                 RETURN(rc);
1112                         }
1113                 }
1114                 mutex_up(&server_start_lock);
1115         }
1116 #endif
1117
1118         /* If we're an OST, make sure the global OSS is running */
1119         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
1120                 /* make sure OSS is started */
1121                 mutex_down(&server_start_lock);
1122                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1123                 if (!obd) {
1124                         rc = lustre_start_simple(LUSTRE_OSS_OBDNAME,
1125                                                  LUSTRE_OSS_NAME,
1126                                                  LUSTRE_OSS_OBDNAME"_uuid",
1127                                                  0, 0);
1128                         if (rc) {
1129                                 mutex_up(&server_start_lock);
1130                                 CERROR("failed to start OSS: %d\n", rc);
1131                                 RETURN(rc);
1132                         }
1133                 }
1134                 mutex_up(&server_start_lock);
1135         }
1136
1137         /* Set the mgc fs to our server disk.  This allows the MGC
1138            to read and write configs locally. */
1139         rc = server_mgc_set_fs(lsi->lsi_mgc, sb);
1140         if (rc)
1141                 RETURN(rc);
1142
1143         /* Register with MGS */
1144         rc = server_register_target(sb);
1145         if (rc && (lsi->lsi_ldd->ldd_flags &
1146                    (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_UPGRADE14))){
1147                 CERROR("Required registration failed for %s: %d\n",
1148                        lsi->lsi_ldd->ldd_svname, rc);
1149                 if (rc == -EIO) {
1150                         LCONSOLE_ERROR_MSG(0x15f, "Communication error with "
1151                                            "the MGS.  Is the MGS running?\n");
1152                 }
1153                 GOTO(out_mgc, rc);
1154         }
1155         if (rc == -EINVAL) {
1156                 LCONSOLE_ERROR_MSG(0x160, "The MGS is refusing to allow this "
1157                                    "server (%s) to start. Please see messages"
1158                                    " on the MGS node.\n",
1159                                    lsi->lsi_ldd->ldd_svname);
1160                 GOTO(out_mgc, rc);
1161         }
1162
1163         /* Let the target look up the mount using the target's name
1164            (we can't pass the sb or mnt through class_process_config.) */
1165         rc = server_register_mount(lsi->lsi_ldd->ldd_svname, sb, mnt);
1166         if (rc)
1167                 GOTO(out_mgc, rc);
1168
1169         /* Start targets using the llog named for the target */
1170         memset(&cfg, 0, sizeof(cfg));
1171         rc = lustre_process_log(sb, lsi->lsi_ldd->ldd_svname, &cfg);
1172         if (rc) {
1173                 CERROR("failed to start server %s: %d\n",
1174                        lsi->lsi_ldd->ldd_svname, rc);
1175                 GOTO(out_mgc, rc);
1176         }
1177
1178 out_mgc:
1179         /* Release the mgc fs for others to use */
1180         server_mgc_clear_fs(lsi->lsi_mgc);
1181
1182         if (!rc) {
1183                 obd = class_name2obd(lsi->lsi_ldd->ldd_svname);
1184                 if (!obd) {
1185                         CERROR("no server named %s was started\n",
1186                                lsi->lsi_ldd->ldd_svname);
1187                         RETURN(-ENXIO);
1188                 }
1189
1190                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_ABORT_RECOV) &&
1191                     (OBP(obd, iocontrol))) {
1192                         obd_iocontrol(OBD_IOC_ABORT_RECOVERY,
1193                                       obd->obd_self_export, 0, NULL, NULL);
1194                 }
1195
1196                 /* log has been fully processed */
1197                 obd_notify(obd, NULL, OBD_NOTIFY_CONFIG, (void *)CONFIG_LOG);
1198         }
1199
1200         RETURN(rc);
1201 }
1202
1203 /***************** lustre superblock **************/
1204
1205 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
1206 {
1207         struct lustre_sb_info *lsi = NULL;
1208         ENTRY;
1209
1210         OBD_ALLOC(lsi, sizeof(*lsi));
1211         if (!lsi)
1212                 RETURN(NULL);
1213         OBD_ALLOC(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1214         if (!lsi->lsi_lmd) {
1215                 OBD_FREE(lsi, sizeof(*lsi));
1216                 RETURN(NULL);
1217         }
1218
1219         lsi->lsi_lmd->lmd_exclude_count = 0;
1220         s2lsi_nocast(sb) = lsi;
1221         /* we take 1 extra ref for our setup */
1222         atomic_set(&lsi->lsi_mounts, 1);
1223
1224         /* Default umount style */
1225         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
1226
1227         lsi->lsi_lmd->lmd_nllu = NOBODY_UID;
1228         lsi->lsi_lmd->lmd_nllg = NOBODY_GID;
1229         RETURN(lsi);
1230 }
1231
1232 static int lustre_free_lsi(struct super_block *sb)
1233 {
1234         struct lustre_sb_info *lsi = s2lsi(sb);
1235         ENTRY;
1236
1237         if (!lsi)
1238                 RETURN(0);
1239
1240         CDEBUG(D_MOUNT, "Freeing lsi\n");
1241
1242         /* someone didn't call server_put_mount. */
1243         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
1244
1245         if (lsi->lsi_ldd != NULL)
1246                 OBD_FREE(lsi->lsi_ldd, sizeof(*lsi->lsi_ldd));
1247
1248         if (lsi->lsi_lmd != NULL) {
1249                 if (lsi->lsi_lmd->lmd_dev != NULL)
1250                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
1251                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1252                 if (lsi->lsi_lmd->lmd_profile != NULL)
1253                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
1254                                  strlen(lsi->lsi_lmd->lmd_profile) + 1);
1255                 if (lsi->lsi_lmd->lmd_sec_mdt != NULL)
1256                         OBD_FREE(lsi->lsi_lmd->lmd_sec_mdt,
1257                                  strlen(lsi->lsi_lmd->lmd_sec_mdt) + 1);
1258                 if (lsi->lsi_lmd->lmd_sec_cli != NULL)
1259                         OBD_FREE(lsi->lsi_lmd->lmd_sec_cli,
1260                                  strlen(lsi->lsi_lmd->lmd_sec_cli) + 1);
1261                 if (lsi->lsi_lmd->lmd_opts != NULL)
1262                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
1263                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1264                 if (lsi->lsi_lmd->lmd_exclude_count)
1265                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
1266                                  sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
1267                                  lsi->lsi_lmd->lmd_exclude_count);
1268                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1269         }
1270
1271         LASSERT(lsi->lsi_llsbi == NULL);
1272         OBD_FREE(lsi, sizeof(*lsi));
1273         s2lsi_nocast(sb) = NULL;
1274
1275         RETURN(0);
1276 }
1277
1278 /* The lsi has one reference for every server that is using the disk -
1279    e.g. MDT, MGS, and potentially MGC */
1280 static int lustre_put_lsi(struct super_block *sb)
1281 {
1282         struct lustre_sb_info *lsi = s2lsi(sb);
1283         ENTRY;
1284
1285         LASSERT(lsi);
1286
1287         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
1288
1289         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
1290                 lustre_free_lsi(sb);
1291                 RETURN(1);
1292         }
1293         RETURN(0);
1294 }
1295
1296 /*************** server mount ******************/
1297
1298 /* Kernel mount using mount options in MOUNT_DATA_FILE */
1299 static struct vfsmount *server_kernel_mount(struct super_block *sb)
1300 {
1301         struct lvfs_run_ctxt mount_ctxt;
1302         struct lustre_sb_info *lsi = s2lsi(sb);
1303         struct lustre_disk_data *ldd;
1304         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1305         struct vfsmount *mnt;
1306         char *options = NULL;
1307         unsigned long page, s_flags;
1308         int rc;
1309         ENTRY;
1310
1311         OBD_ALLOC(ldd, sizeof(*ldd));
1312         if (!ldd)
1313                 RETURN(ERR_PTR(-ENOMEM));
1314
1315         /* In the past, we have always used flags = 0.
1316            Note ext3/ldiskfs can't be mounted ro. */
1317         s_flags = sb->s_flags;
1318
1319         /* Pre-mount ldiskfs to read the MOUNT_DATA_FILE */
1320         CDEBUG(D_MOUNT, "Pre-mount ldiskfs %s\n", lmd->lmd_dev);
1321         mnt = ll_kern_mount("ldiskfs", s_flags, lmd->lmd_dev, 0);
1322         if (IS_ERR(mnt)) {
1323                 rc = PTR_ERR(mnt);
1324                 CERROR("premount %s:%#lx ldiskfs failed: %d "
1325                         "Is the ldiskfs module available?\n",
1326                         lmd->lmd_dev, s_flags, rc );
1327                 GOTO(out_free, rc);
1328         }
1329
1330         OBD_SET_CTXT_MAGIC(&mount_ctxt);
1331         mount_ctxt.pwdmnt = mnt;
1332         mount_ctxt.pwd = mnt->mnt_root;
1333         mount_ctxt.fs = get_ds();
1334
1335         rc = ldd_parse(&mount_ctxt, ldd);
1336         unlock_mntput(mnt);
1337
1338         if (rc) {
1339                 CERROR("premount parse options failed: rc = %d\n", rc);
1340                 GOTO(out_free, rc);
1341         }
1342
1343         /* Done with our pre-mount, now do the real mount. */
1344
1345         /* Glom up mount options */
1346         page = __get_free_page(GFP_KERNEL);
1347         if (!page)
1348                 GOTO(out_free, rc = -ENOMEM);
1349
1350         options = (char *)page;
1351         memset(options, 0, CFS_PAGE_SIZE);
1352         strncpy(options, ldd->ldd_mount_opts, CFS_PAGE_SIZE - 2);
1353
1354         /* Add in any mount-line options */
1355         if (lmd->lmd_opts && (*(lmd->lmd_opts) != 0)) {
1356                 int len = CFS_PAGE_SIZE - strlen(options) - 2;
1357                 if (*options != 0)
1358                         strcat(options, ",");
1359                 strncat(options, lmd->lmd_opts, len);
1360         }
1361
1362         /* Special permanent mount flags */
1363         if (IS_OST(ldd))
1364             s_flags |= MS_NOATIME | MS_NODIRATIME;
1365
1366         CDEBUG(D_MOUNT, "kern_mount: %s %s %s\n",
1367                MT_STR(ldd), lmd->lmd_dev, options);
1368         mnt = ll_kern_mount(MT_STR(ldd), s_flags, lmd->lmd_dev,
1369                             (void *)options);
1370         free_page(page);
1371         if (IS_ERR(mnt)) {
1372                 rc = PTR_ERR(mnt);
1373                 CERROR("ll_kern_mount failed: rc = %d\n", rc);
1374                 GOTO(out_free, rc);
1375         }
1376
1377         lsi->lsi_ldd = ldd;   /* freed at lsi cleanup */
1378         CDEBUG(D_SUPER, "%s: mnt = %p\n", lmd->lmd_dev, mnt);
1379         RETURN(mnt);
1380
1381 out_free:
1382         OBD_FREE(ldd, sizeof(*ldd));
1383         lsi->lsi_ldd = NULL;
1384         RETURN(ERR_PTR(rc));
1385 }
1386
1387 static void server_wait_finished(struct vfsmount *mnt)
1388 {
1389         wait_queue_head_t   waitq;
1390         struct l_wait_info  lwi;
1391         int                 retries = 120;
1392
1393         init_waitqueue_head(&waitq);
1394
1395         while ((atomic_read(&mnt->mnt_count) > 1) && (retries > 0)) {
1396                 LCONSOLE_WARN("Mount still busy with %d refs, waiting for "
1397                               "%d secs...\n",
1398                               atomic_read(&mnt->mnt_count), retries);
1399
1400                 /* Wait for a bit */
1401                 retries -= 5;
1402                 lwi = LWI_TIMEOUT(5 * HZ, NULL, NULL);
1403                 l_wait_event(waitq, 0, &lwi);
1404         }
1405         if (atomic_read(&mnt->mnt_count) > 1) {
1406                 CERROR("Mount %p is still busy (%d refs), giving up.\n",
1407                        mnt, atomic_read(&mnt->mnt_count));
1408         }
1409 }
1410
1411 static void server_put_super(struct super_block *sb)
1412 {
1413         struct lustre_sb_info *lsi = s2lsi(sb);
1414         struct obd_device     *obd;
1415         struct vfsmount       *mnt = lsi->lsi_srv_mnt;
1416         char *tmpname, *extraname = NULL;
1417         int tmpname_sz;
1418         int lddflags = lsi->lsi_ldd->ldd_flags;
1419         int lsiflags = lsi->lsi_flags;
1420         int rc;
1421         ENTRY;
1422
1423         LASSERT(lsiflags & LSI_SERVER);
1424
1425         tmpname_sz = strlen(lsi->lsi_ldd->ldd_svname) + 1;
1426         OBD_ALLOC(tmpname, tmpname_sz);
1427         memcpy(tmpname, lsi->lsi_ldd->ldd_svname, tmpname_sz);
1428         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1429
1430         /* Stop the target */
1431         if (IS_MDT(lsi->lsi_ldd) || IS_OST(lsi->lsi_ldd)) {
1432                 struct lustre_profile *lprof = NULL;
1433
1434                 /* tell the mgc to drop the config log */
1435                 lustre_end_log(sb, lsi->lsi_ldd->ldd_svname, NULL);
1436
1437                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1438                    If there are any setup/cleanup errors, save the lov
1439                    name for safety cleanup later. */
1440                 lprof = class_get_profile(lsi->lsi_ldd->ldd_svname);
1441                 if (lprof && lprof->lp_dt) {
1442                         OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1443                         strcpy(extraname, lprof->lp_dt);
1444                 }
1445
1446                 obd = class_name2obd(lsi->lsi_ldd->ldd_svname);
1447                 if (obd) {
1448                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1449                         if (lsi->lsi_flags & LSI_UMOUNT_FORCE)
1450                                 obd->obd_force = 1;
1451                         if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
1452                                 obd->obd_fail = 1;
1453                         /* We can't seem to give an error return code
1454                            to .put_super, so we better make sure we clean up! */
1455                         obd->obd_force = 1;
1456                         class_manual_cleanup(obd);
1457                 } else {
1458                         CERROR("no obd %s\n", lsi->lsi_ldd->ldd_svname);
1459                         server_deregister_mount(lsi->lsi_ldd->ldd_svname);
1460                 }
1461         }
1462
1463         /* If they wanted the mgs to stop separately from the mdt, they
1464            should have put it on a different device. */
1465         if (IS_MGS(lsi->lsi_ldd)) {
1466                 /* stop the mgc before the mgs so the connection gets cleaned
1467                    up */
1468                 lustre_stop_mgc(sb);
1469                 server_stop_mgs(sb);
1470         }
1471
1472         /* Clean the mgc and sb */
1473         rc = lustre_common_put_super(sb);
1474         /* FIXME how can I report a failure to umount? */
1475
1476         /* Wait for the targets to really clean up - can't exit (and let the
1477            sb get destroyed) while the mount is still in use */
1478         server_wait_finished(mnt);
1479
1480         /* drop the One True Mount */
1481         unlock_mntput(mnt);
1482
1483         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1484            until the target is really gone so that our type refcount check
1485            is right. */
1486         server_stop_servers(lddflags, lsiflags);
1487
1488         /* In case of startup or cleanup err, stop related obds */
1489         if (extraname) {
1490                 obd = class_name2obd(extraname);
1491                 if (obd) {
1492                         CWARN("Cleaning orphaned obd %s\n", extraname);
1493                         obd->obd_force = 1;
1494                         class_manual_cleanup(obd);
1495                 }
1496                 OBD_FREE(extraname, strlen(extraname) + 1);
1497         }
1498
1499         LCONSOLE_WARN("server umount %s complete\n", tmpname);
1500         OBD_FREE(tmpname, tmpname_sz);
1501         EXIT;
1502 }
1503
1504 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1505 static void server_umount_begin(struct vfsmount *vfsmnt, int flags)
1506 {
1507         struct super_block *sb = vfsmnt->mnt_sb;
1508 #else
1509 static void server_umount_begin(struct super_block *sb)
1510 {
1511 #endif
1512         struct lustre_sb_info *lsi = s2lsi(sb);
1513         ENTRY;
1514
1515 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1516         if (!(flags & MNT_FORCE)) {
1517                 EXIT;
1518                 return;
1519         }
1520 #endif
1521
1522         CDEBUG(D_MOUNT, "umount -f\n");
1523         /* umount = failover
1524            umount -f = force
1525            no third way to do non-force, non-failover */
1526         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1527         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
1528         EXIT;
1529 }
1530
1531 #ifndef HAVE_STATFS_DENTRY_PARAM
1532 static int server_statfs (struct super_block *sb, struct kstatfs *buf)
1533 {
1534 #else
1535 static int server_statfs (struct dentry *dentry, struct kstatfs *buf)
1536 {
1537         struct super_block *sb = dentry->d_sb;
1538 #endif
1539         struct vfsmount *mnt = s2lsi(sb)->lsi_srv_mnt;
1540         ENTRY;
1541
1542         if (mnt && mnt->mnt_sb && mnt->mnt_sb->s_op->statfs) {
1543 #ifdef HAVE_STATFS_DENTRY_PARAM
1544                 int rc = mnt->mnt_sb->s_op->statfs(mnt->mnt_root, buf);
1545 #else
1546                 int rc = mnt->mnt_sb->s_op->statfs(mnt->mnt_sb, buf);
1547 #endif
1548                 if (!rc) {
1549                         buf->f_type = sb->s_magic;
1550                         RETURN(0);
1551                 }
1552         }
1553
1554         /* just return 0 */
1555         buf->f_type = sb->s_magic;
1556         buf->f_bsize = sb->s_blocksize;
1557         buf->f_blocks = 1;
1558         buf->f_bfree = 0;
1559         buf->f_bavail = 0;
1560         buf->f_files = 1;
1561         buf->f_ffree = 0;
1562         buf->f_namelen = NAME_MAX;
1563         RETURN(0);
1564 }
1565
1566 static struct super_operations server_ops =
1567 {
1568         .put_super      = server_put_super,
1569         .umount_begin   = server_umount_begin, /* umount -f */
1570         .statfs         = server_statfs,
1571 };
1572
1573 #define log2(n) ffz(~(n))
1574 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1575
1576 static int server_fill_super_common(struct super_block *sb)
1577 {
1578         struct inode *root = 0;
1579         ENTRY;
1580
1581         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1582
1583         sb->s_blocksize = 4096;
1584         sb->s_blocksize_bits = log2(sb->s_blocksize);
1585         sb->s_magic = LUSTRE_SUPER_MAGIC;
1586         sb->s_maxbytes = 0; //PAGE_CACHE_MAXBYTES;
1587         sb->s_flags |= MS_RDONLY;
1588         sb->s_op = &server_ops;
1589
1590         root = new_inode(sb);
1591         if (!root) {
1592                 CERROR("Can't make root inode\n");
1593                 RETURN(-EIO);
1594         }
1595
1596         /* returns -EIO for every operation */
1597         /* make_bad_inode(root); -- badness - can't umount */
1598         /* apparently we need to be a directory for the mount to finish */
1599         root->i_mode = S_IFDIR;
1600
1601         sb->s_root = d_alloc_root(root);
1602         if (!sb->s_root) {
1603                 CERROR("Can't make root dentry\n");
1604                 iput(root);
1605                 RETURN(-EIO);
1606         }
1607
1608         RETURN(0);
1609 }
1610
1611 static int server_fill_super(struct super_block *sb)
1612 {
1613         struct lustre_sb_info *lsi = s2lsi(sb);
1614         struct vfsmount *mnt;
1615         int rc;
1616         ENTRY;
1617
1618         /* the One True Mount */
1619         mnt = server_kernel_mount(sb);
1620         if (IS_ERR(mnt)) {
1621                 rc = PTR_ERR(mnt);
1622                 CERROR("Unable to mount device %s: %d\n",
1623                       lsi->lsi_lmd->lmd_dev, rc);
1624                 lustre_put_lsi(sb);
1625                 GOTO(out, rc);
1626         }
1627         lsi->lsi_srv_mnt = mnt;
1628
1629         LASSERT(lsi->lsi_ldd);
1630         CDEBUG(D_MOUNT, "Found service %s for fs '%s' on device %s\n",
1631                lsi->lsi_ldd->ldd_svname, lsi->lsi_ldd->ldd_fsname,
1632                lsi->lsi_lmd->lmd_dev);
1633
1634         if (class_name2obd(lsi->lsi_ldd->ldd_svname)) {
1635                 LCONSOLE_ERROR_MSG(0x161, "The target named %s is already "
1636                                    "running. Double-mount may have compromised"
1637                                    " the disk journal.\n",
1638                                    lsi->lsi_ldd->ldd_svname);
1639                 unlock_mntput(mnt);
1640                 lustre_put_lsi(sb);
1641                 GOTO(out, rc = -EALREADY);
1642         }
1643
1644         /* start MGS before MGC */
1645         if (IS_MGS(lsi->lsi_ldd)) {
1646                 rc = server_start_mgs(sb);
1647                 if (rc)
1648                         GOTO(out_mnt, rc);
1649         }
1650
1651         rc = lustre_start_mgc(sb);
1652         if (rc)
1653                 GOTO(out_mnt, rc);
1654
1655         /* Set up all obd devices for service */
1656         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1657                 (IS_OST(lsi->lsi_ldd) || IS_MDT(lsi->lsi_ldd))) {
1658                 rc = server_start_targets(sb, mnt);
1659                 if (rc < 0) {
1660                         CERROR("Unable to start targets: %d\n", rc);
1661                         GOTO(out_mnt, rc);
1662                 }
1663         /* FIXME overmount client here,
1664            or can we just start a client log and client_fill_super on this sb?
1665            We need to make sure server_put_super gets called too - ll_put_super
1666            calls lustre_common_put_super; check there for LSI_SERVER flag,
1667            call s_p_s if so.
1668            Probably should start client from new thread so we can return.
1669            Client will not finish until all servers are connected.
1670            Note - MGS-only server does NOT get a client, since there is no
1671            lustre fs associated - the MGS is for all lustre fs's */
1672         }
1673
1674         rc = server_fill_super_common(sb);
1675         if (rc)
1676                 GOTO(out_mnt, rc);
1677
1678         LCONSOLE_WARN("Server %s on device %s has started\n",
1679                       lsi->lsi_ldd->ldd_svname, lsi->lsi_lmd->lmd_dev);
1680
1681         RETURN(0);
1682
1683 out_mnt:
1684         server_put_super(sb);
1685 out:
1686         RETURN(rc);
1687 }
1688
1689 /* Get the index from the obd name.
1690    rc = server type, or
1691    rc < 0  on error
1692    if endptr isn't NULL it is set to end of name */
1693 int server_name2index(char *svname, __u32 *idx, char **endptr)
1694 {
1695         unsigned long index;
1696         int rc;
1697         char *dash = strchr(svname, '-');
1698         if (!dash)
1699                 return(-EINVAL);
1700
1701         if (strncmp(dash + 1, "MDT", 3) == 0)
1702                 rc = LDD_F_SV_TYPE_MDT;
1703         else if (strncmp(dash + 1, "OST", 3) == 0)
1704                 rc = LDD_F_SV_TYPE_OST;
1705         else
1706                 return(-EINVAL);
1707
1708         index = simple_strtoul(dash + 4, endptr, 16);
1709         *idx = index;
1710         return rc;
1711 }
1712
1713 /*************** mount common betweeen server and client ***************/
1714
1715 /* Common umount */
1716 int lustre_common_put_super(struct super_block *sb)
1717 {
1718         int rc;
1719         ENTRY;
1720
1721         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
1722
1723         /* Drop a ref to the MGC */
1724         rc = lustre_stop_mgc(sb);
1725         if (rc && (rc != -ENOENT)) {
1726                 if (rc != -EBUSY) {
1727                         CERROR("Can't stop MGC: %d\n", rc);
1728                         RETURN(rc);
1729                 }
1730                 /* BUSY just means that there's some other obd that
1731                    needs the mgc.  Let him clean it up. */
1732                 CDEBUG(D_MOUNT, "MGC still in use\n");
1733         }
1734         /* Drop a ref to the mounted disk */
1735         lustre_put_lsi(sb);
1736         RETURN(rc);
1737 }
1738
1739 #if 0
1740 static void lmd_print(struct lustre_mount_data *lmd)
1741 {
1742         int i;
1743
1744         PRINT_CMD(PRINT_MASK, "  mount data:\n");
1745         if (lmd_is_client(lmd))
1746                 PRINT_CMD(PRINT_MASK, "profile: %s\n", lmd->lmd_profile);
1747         PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
1748         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
1749         if (lmd->lmd_sec_mdt)
1750                 PRINT_CMD(PRINT_MASK, "sec_mdt: %s\n", lmd->lmd_sec_mdt);
1751         if (lmd->lmd_sec_cli)
1752                 PRINT_CMD(PRINT_MASK, "sec_cli: %s\n", lmd->lmd_sec_cli);
1753         if (lmd->lmd_opts)
1754                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
1755         for (i = 0; i < lmd->lmd_exclude_count; i++) {
1756                 PRINT_CMD(PRINT_MASK, "exclude %d:  OST%04x\n", i,
1757                           lmd->lmd_exclude[i]);
1758         }
1759 }
1760 #endif
1761
1762 /* Is this server on the exclusion list */
1763 int lustre_check_exclusion(struct super_block *sb, char *svname)
1764 {
1765         struct lustre_sb_info *lsi = s2lsi(sb);
1766         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1767         __u32 index;
1768         int i, rc;
1769         ENTRY;
1770
1771         rc = server_name2index(svname, &index, NULL);
1772         if (rc != LDD_F_SV_TYPE_OST)
1773                 /* Only exclude OSTs */
1774                 RETURN(0);
1775
1776         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1777                index, lmd->lmd_exclude_count, lmd->lmd_dev);
1778
1779         for(i = 0; i < lmd->lmd_exclude_count; i++) {
1780                 if (index == lmd->lmd_exclude[i]) {
1781                         CWARN("Excluding %s (on exclusion list)\n", svname);
1782                         RETURN(1);
1783                 }
1784         }
1785         RETURN(0);
1786 }
1787
1788 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1789 static int lmd_make_exclusion(struct lustre_mount_data *lmd, char *ptr)
1790 {
1791         char *s1 = ptr, *s2;
1792         __u32 index, *exclude_list;
1793         int rc = 0, devmax;
1794         ENTRY;
1795
1796         /* The shortest an ost name can be is 8 chars: -OST0000.
1797            We don't actually know the fsname at this time, so in fact
1798            a user could specify any fsname. */
1799         devmax = strlen(ptr) / 8 + 1;
1800
1801         /* temp storage until we figure out how many we have */
1802         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
1803         if (!exclude_list)
1804                 RETURN(-ENOMEM);
1805
1806         /* we enter this fn pointing at the '=' */
1807         while (*s1 && *s1 != ' ' && *s1 != ',') {
1808                 s1++;
1809                 rc = server_name2index(s1, &index, &s2);
1810                 if (rc < 0) {
1811                         CERROR("Can't parse server name '%s'\n", s1);
1812                         break;
1813                 }
1814                 if (rc == LDD_F_SV_TYPE_OST)
1815                         exclude_list[lmd->lmd_exclude_count++] = index;
1816                 else
1817                         CDEBUG(D_MOUNT, "ignoring exclude %.7s\n", s1);
1818                 s1 = s2;
1819                 /* now we are pointing at ':' (next exclude)
1820                    or ',' (end of excludes) */
1821                 if (lmd->lmd_exclude_count >= devmax)
1822                         break;
1823         }
1824         if (rc >= 0) /* non-err */
1825                 rc = 0;
1826
1827         if (lmd->lmd_exclude_count) {
1828                 /* permanent, freed in lustre_free_lsi */
1829                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
1830                           lmd->lmd_exclude_count);
1831                 if (lmd->lmd_exclude) {
1832                         memcpy(lmd->lmd_exclude, exclude_list,
1833                                sizeof(index) * lmd->lmd_exclude_count);
1834                 } else {
1835                         rc = -ENOMEM;
1836                         lmd->lmd_exclude_count = 0;
1837                 }
1838         }
1839         OBD_FREE(exclude_list, sizeof(index) * devmax);
1840         RETURN(rc);
1841 }
1842
1843 static
1844 int lmd_set_sec_opts(char **set, char *opts, int length)
1845 {
1846         if (*set)
1847                 OBD_FREE(*set, strlen(*set) + 1);
1848
1849         OBD_ALLOC(*set, length + 1);
1850         if (*set == NULL)
1851                 return -ENOMEM;
1852
1853         memcpy(*set, opts, length);
1854         (*set)[length] = '\0';
1855
1856         return 0;
1857 }
1858
1859 static
1860 int lmd_parse_sec_opts(struct lustre_mount_data *lmd, char *ptr)
1861 {
1862         char  *tail;
1863         char **set = NULL;
1864         int    length;
1865
1866         /* check peer name */
1867         if (strncmp(ptr, "sec_mdt=", 8) == 0) {
1868                 set = &lmd->lmd_sec_mdt;
1869                 ptr += 8;
1870         } else if (strncmp(ptr, "sec_cli=", 8) == 0) {
1871                 set = &lmd->lmd_sec_cli;
1872                 ptr += 8;
1873         } else if (strncmp(ptr, "sec=", 4) == 0) {
1874                 /* leave 'set' be null */
1875                 ptr += 4;
1876         } else {
1877                 CERROR("invalid security options: %s\n", ptr);
1878                 return -EINVAL;
1879         }
1880
1881         tail = strchr(ptr, ',');
1882         if (tail == NULL)
1883                 length = strlen(ptr);
1884         else
1885                 length = tail - ptr;
1886
1887         if (set) {
1888                 if (lmd_set_sec_opts(set, ptr, length))
1889                         return -EINVAL;
1890         } else {
1891                 if (lmd->lmd_sec_mdt == NULL &&
1892                     lmd_set_sec_opts(&lmd->lmd_sec_mdt, ptr, length))
1893                         return -EINVAL;
1894
1895                 if (lmd->lmd_sec_cli == NULL &&
1896                     lmd_set_sec_opts(&lmd->lmd_sec_cli, ptr, length))
1897                         return -EINVAL;
1898         }
1899
1900         return 0;
1901 }
1902
1903 /* mount -v -t lustre uml1:uml2:/lustre-client /mnt/lustre */
1904 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1905 {
1906         char *s1, *s2, *devname = NULL;
1907         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1908         int rc = 0;
1909         ENTRY;
1910
1911         LASSERT(lmd);
1912         if (!options) {
1913                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1914                                    "/sbin/mount.lustre is installed.\n");
1915                 RETURN(-EINVAL);
1916         }
1917
1918         /* Options should be a string - try to detect old lmd data */
1919         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1920                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1921                                    "/sbin/mount.lustre.  Please install "
1922                                    "version %s\n", LUSTRE_VERSION_STRING);
1923                 RETURN(-EINVAL);
1924         }
1925         lmd->lmd_magic = LMD_MAGIC;
1926
1927         /* Set default flags here */
1928
1929         s1 = options;
1930         while (*s1) {
1931                 int clear = 0;
1932                 /* Skip whitespace and extra commas */
1933                 while (*s1 == ' ' || *s1 == ',')
1934                         s1++;
1935
1936                 /* Client options are parsed in ll_options: eg. flock,
1937                    user_xattr, acl */
1938
1939                 /* Parse non-ldiskfs options here. Rather than modifying
1940                    ldiskfs, we just zero these out here */
1941                 if (strncmp(s1, "abort_recov", 11) == 0) {
1942                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1943                         clear++;
1944                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1945                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1946                         clear++;
1947                 /* ost exclusion list */
1948                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1949                         rc = lmd_make_exclusion(lmd, s1 + 7);
1950                         if (rc)
1951                                 goto invalid;
1952                         clear++;
1953                 } else if (strncmp(s1, "nllu=", 5) == 0) {
1954                         lmd->lmd_nllu = simple_strtoul(s1 + 5, NULL, 10);
1955                         clear++;
1956                 } else if (strncmp(s1, "nllg=", 5) == 0) {
1957                         lmd->lmd_nllg = simple_strtoul(s1 + 5, NULL, 10);
1958                         clear++;
1959                 } else if (strncmp(s1, "sec", 3) == 0) {
1960                         rc = lmd_parse_sec_opts(lmd, s1);
1961                         if (rc)
1962                                 goto invalid;
1963                         clear++;
1964                 }
1965                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1966                    end of the options. */
1967                 else if (strncmp(s1, "device=", 7) == 0) {
1968                         devname = s1 + 7;
1969                         /* terminate options right before device.  device
1970                            must be the last one. */
1971                         *s1 = '\0';
1972                         break;
1973                 }
1974
1975                 /* Find next opt */
1976                 s2 = strchr(s1, ',');
1977                 if (s2 == NULL) {
1978                         if (clear)
1979                                 *s1 = '\0';
1980                         break;
1981                 }
1982                 s2++;
1983                 if (clear)
1984                         memmove(s1, s2, strlen(s2) + 1);
1985                 else
1986                         s1 = s2;
1987         }
1988
1989         if (!devname) {
1990                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1991                                    "(need mount option 'device=...')\n");
1992                 goto invalid;
1993         }
1994
1995         s1 = strrchr(devname, ':');
1996         if (s1) {
1997                 lmd->lmd_flags = LMD_FLG_CLIENT;
1998                 /* Remove leading /s from fsname */
1999                 while (*++s1 == '/') ;
2000                 /* Freed in lustre_free_lsi */
2001                 OBD_ALLOC(lmd->lmd_profile, strlen(s1) + 8);
2002                 if (!lmd->lmd_profile)
2003                         RETURN(-ENOMEM);
2004                 sprintf(lmd->lmd_profile, "%s-client", s1);
2005         }
2006
2007         /* Freed in lustre_free_lsi */
2008         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
2009         if (!lmd->lmd_dev)
2010                 RETURN(-ENOMEM);
2011         strcpy(lmd->lmd_dev, devname);
2012
2013         /* Save mount options */
2014         s1 = options + strlen(options) - 1;
2015         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
2016                 *s1-- = 0;
2017         if (*options != 0) {
2018                 /* Freed in lustre_free_lsi */
2019                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
2020                 if (!lmd->lmd_opts)
2021                         RETURN(-ENOMEM);
2022                 strcpy(lmd->lmd_opts, options);
2023         }
2024
2025         lmd->lmd_magic = LMD_MAGIC;
2026
2027         RETURN(rc);
2028
2029 invalid:
2030         CERROR("Bad mount options %s\n", options);
2031         RETURN(-EINVAL);
2032 }
2033
2034
2035 /* Common mount */
2036 int lustre_fill_super(struct super_block *sb, void *data, int silent)
2037 {
2038         struct lustre_mount_data *lmd;
2039         struct lustre_sb_info *lsi;
2040         int rc;
2041         ENTRY;
2042
2043         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2044
2045         lsi = lustre_init_lsi(sb);
2046         if (!lsi)
2047                 RETURN(-ENOMEM);
2048         lmd = lsi->lsi_lmd;
2049
2050         /* Figure out the lmd from the mount options */
2051         if (lmd_parse((char *)data, lmd)) {
2052                 lustre_put_lsi(sb);
2053                 RETURN(-EINVAL);
2054         }
2055
2056         if (lmd_is_client(lmd)) {
2057                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
2058                 if (!client_fill_super) {
2059                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
2060                                            "client mount! Is the 'lustre' "
2061                                            "module loaded?\n");
2062                         rc = -ENODEV;
2063                 } else {
2064                         rc = lustre_start_mgc(sb);
2065                         if (rc) {
2066                                 lustre_stop_mgc(sb);
2067                                 goto out;
2068                         }
2069                         /* Connect and start */
2070                         /* (should always be ll_fill_super) */
2071                         rc = (*client_fill_super)(sb);
2072                         /* c_f_s will call lustre_common_put_super on failure */
2073                 }
2074         } else {
2075                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2076                 lsi->lsi_flags |= LSI_SERVER;
2077                 rc = server_fill_super(sb);
2078                 /* s_f_s calls lustre_start_mgc after the mount because we need
2079                    the MGS nids which are stored on disk.  Plus, we may
2080                    need to start the MGS first. */
2081                 /* s_f_s will call server_put_super on failure */
2082         }
2083
2084 out:
2085         if (rc){
2086                 CERROR("Unable to mount %s (%d)\n",
2087                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
2088         } else {
2089                 CDEBUG(D_SUPER, "mount %s complete\n", lmd->lmd_dev);
2090         }
2091         RETURN(rc);
2092 }
2093
2094
2095 /* We can't call ll_fill_super by name because it lives in a module that
2096    must be loaded after this one. */
2097 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb))
2098 {
2099         client_fill_super = cfs;
2100 }
2101
2102 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
2103 {
2104         kill_super_cb = cfs;
2105 }
2106
2107 /***************** FS registration ******************/
2108
2109 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2110 struct super_block * lustre_get_sb(struct file_system_type *fs_type,
2111                                int flags, const char *devname, void * data)
2112 {
2113         /* calls back in fill super */
2114         /* we could append devname= onto options (*data) here,
2115            but 2.4 doesn't get devname.  So we do it in mount_lustre.c */
2116         return get_sb_nodev(fs_type, flags, data, lustre_fill_super);
2117 }
2118 #else
2119 int lustre_get_sb(struct file_system_type *fs_type,
2120                                int flags, const char *devname, void * data,
2121                                struct vfsmount *mnt)
2122 {
2123         /* calls back in fill super */
2124         /* we could append devname= onto options (*data) here,
2125            but 2.4 doesn't get devname.  So we do it in mount_lustre.c */
2126         return get_sb_nodev(fs_type, flags, data, lustre_fill_super, mnt);
2127 }
2128 #endif
2129
2130 void lustre_kill_super(struct super_block *sb)
2131 {
2132         struct lustre_sb_info *lsi = s2lsi(sb);
2133
2134         if (kill_super_cb && lsi &&(lsi->lsi_flags & LSI_SERVER))
2135                 (*kill_super_cb)(sb);
2136
2137         kill_anon_super(sb);
2138 }
2139
2140 struct file_system_type lustre_fs_type = {
2141         .owner        = THIS_MODULE,
2142         .name         = "lustre",
2143         .get_sb       = lustre_get_sb,
2144         .kill_sb      = lustre_kill_super,
2145         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV,
2146 };
2147
2148 int lustre_register_fs(void)
2149 {
2150         return register_filesystem(&lustre_fs_type);
2151 }
2152
2153 int lustre_unregister_fs(void)
2154 {
2155         return unregister_filesystem(&lustre_fs_type);
2156 }
2157
2158 EXPORT_SYMBOL(lustre_register_client_fill_super);
2159 EXPORT_SYMBOL(lustre_register_kill_super_cb);
2160 EXPORT_SYMBOL(lustre_common_put_super);
2161 EXPORT_SYMBOL(lustre_process_log);
2162 EXPORT_SYMBOL(lustre_end_log);
2163 EXPORT_SYMBOL(server_get_mount);
2164 EXPORT_SYMBOL(server_get_mount_2);
2165 EXPORT_SYMBOL(server_put_mount);
2166 EXPORT_SYMBOL(server_put_mount_2);
2167 EXPORT_SYMBOL(server_register_target);
2168 EXPORT_SYMBOL(server_name2index);
2169 EXPORT_SYMBOL(server_mti_print);
2170 EXPORT_SYMBOL(do_lcfg);
2171
2172