Whamcloud - gitweb
Branch b1_4_mountconf
[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/lvfs/lvfs_mount.c
5  *  Client/server mount routines
6  *
7  *  Copyright (c) 2005 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_MGMT
28 #define D_MOUNT D_SUPER|D_CONFIG|D_WARNING
29 #define PRINT_CMD LCONSOLE
30 #define PRINT_MASK D_WARNING
31
32 #include <linux/obd.h>
33 #include <linux/lvfs.h>
34 #include <linux/lustre_fsfilt.h>
35 #include <linux/obd_class.h>
36 #include <lustre/lustre_user.h>
37 #include <linux/version.h> 
38 #include <linux/lustre_log.h>
39 #include <linux/lustre_disk.h>
40                       
41 static int (*client_fill_super)(struct super_block *sb) = NULL;
42
43
44 /*********** mount lookup *********/
45
46 DECLARE_MUTEX(lustre_mount_info_lock);
47 struct list_head server_mount_info_list = LIST_HEAD_INIT(server_mount_info_list);
48
49 static struct lustre_mount_info *server_find_mount(char *name)
50 {
51         struct list_head *tmp;
52         struct lustre_mount_info *lmi;
53         
54         list_for_each(tmp, &server_mount_info_list) {
55                 lmi = list_entry(tmp, struct lustre_mount_info, lmi_list_chain);
56                 if (strcmp(name, lmi->lmi_name) == 0) 
57                         return(lmi);
58         }
59         return(NULL);
60 }
61
62 /* we must register an obd for a mount before we call the setup routine.  
63    *_setup will call lustre_get_mount to get the mnt struct
64    by obd_name, since we can't pass the pointer to setup. */
65 static int server_register_mount(char *name, struct super_block *sb,
66                           struct vfsmount *mnt)
67 {
68         struct lustre_mount_info *lmi;
69         char *name_cp;
70         ENTRY;
71
72         LASSERT(mnt);
73         LASSERT(sb);
74
75         OBD_ALLOC(lmi, sizeof(*lmi));
76         if (!lmi) 
77                 RETURN(-ENOMEM);
78         OBD_ALLOC(name_cp, strlen(name) + 1);
79         if (!name_cp) { 
80                 OBD_FREE(lmi, sizeof(*lmi));
81                 RETURN(-ENOMEM);
82         }
83         strcpy(name_cp, name);
84
85         down(&lustre_mount_info_lock);
86         
87         if (server_find_mount(name)) {
88                 up(&lustre_mount_info_lock);
89                 OBD_FREE(lmi, sizeof(*lmi));
90                 OBD_FREE(name_cp, strlen(name) + 1);
91                 CERROR("Already registered %s\n", name);
92                 RETURN(-EEXIST);
93         }
94         lmi->lmi_name = name_cp;
95         lmi->lmi_sb = sb;
96         lmi->lmi_mnt = mnt;
97         list_add(&lmi->lmi_list_chain, &server_mount_info_list);
98          
99         up(&lustre_mount_info_lock);
100
101         CDEBUG(D_MOUNT, "reg_mnt %p from %s, vfscount=%d\n", 
102                lmi->lmi_mnt, name, atomic_read(&lmi->lmi_mnt->mnt_count));
103
104         RETURN(0);
105 }
106
107 /* when an obd no longer needs a mount */
108 static int server_deregister_mount(char *name)
109 {
110         struct lustre_mount_info *lmi;
111         ENTRY;
112
113         down(&lustre_mount_info_lock);
114         lmi = server_find_mount(name);
115         if (!lmi) {
116                 up(&lustre_mount_info_lock);
117                 CERROR("%s not registered\n", name);
118                 RETURN(-ENOENT);
119         }
120         
121         CDEBUG(D_MOUNT, "dereg_mnt %p from %s, vfscount=%d\n", 
122                lmi->lmi_mnt, name, atomic_read(&lmi->lmi_mnt->mnt_count));
123         
124         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
125         list_del(&lmi->lmi_list_chain);
126         OBD_FREE(lmi, sizeof(*lmi));
127         up(&lustre_mount_info_lock);
128
129         RETURN(0);
130 }
131
132 /* Deregister anyone referencing the mnt. Everyone should have
133    put_mount in *_cleanup, but this is a catch-all in case of err... */
134 static void server_deregister_mount_all(struct vfsmount *mnt)
135 {
136         struct list_head *tmp, *n;
137         struct lustre_mount_info *lmi;
138
139         if (!mnt)
140                 return;
141
142         down(&lustre_mount_info_lock);
143         list_for_each_safe(tmp, n, &server_mount_info_list) {
144                 lmi = list_entry(tmp, struct lustre_mount_info, lmi_list_chain);
145                 if (lmi->lmi_mnt == mnt) {
146                         CERROR("Deregister failsafe %s\n", lmi->lmi_name);
147                         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
148                         list_del(&lmi->lmi_list_chain);
149                         OBD_FREE(lmi, sizeof(*lmi));
150                 }
151         }
152         up(&lustre_mount_info_lock);
153 }
154
155 /* obd's look up a registered mount using their name. This is just
156    for initial obd setup to find the mount struct.  It should not be
157    called every time you want to mntget. */
158 struct lustre_mount_info *server_get_mount(char *name)
159 {
160         struct lustre_mount_info *lmi;
161         struct lustre_sb_info *lsi;
162         ENTRY;
163
164         down(&lustre_mount_info_lock);
165
166         lmi = server_find_mount(name);
167         if (!lmi) {
168                 up(&lustre_mount_info_lock);
169                 CERROR("Can't find mount for %s\n", name);
170                 RETURN(NULL);
171         }
172         lsi = s2lsi(lmi->lmi_sb);
173         mntget(lmi->lmi_mnt);
174         atomic_inc(&lsi->lsi_mounts);
175         
176         up(&lustre_mount_info_lock);
177         
178         CDEBUG(D_MOUNT, "get_mnt %p from %s, refs=%d, vfscount=%d\n", 
179                lmi->lmi_mnt, name, atomic_read(&lsi->lsi_mounts),
180                atomic_read(&lmi->lmi_mnt->mnt_count));
181
182         RETURN(lmi);
183 }
184
185 static void unlock_mntput(struct vfsmount *mnt)
186 {
187         if (kernel_locked()) {
188                 unlock_kernel();
189                 mntput(mnt);
190                 lock_kernel();
191         } else {
192                 mntput(mnt);
193         }
194 }
195
196 static int lustre_put_lsi(struct super_block *sb);
197
198 /* to be called from obd_cleanup methods */
199 int server_put_mount(char *name, struct vfsmount *mnt)
200 {
201         struct lustre_mount_info *lmi;
202         struct lustre_sb_info *lsi;
203         ENTRY;
204
205         down(&lustre_mount_info_lock);
206         lmi = server_find_mount(name);
207         if (!lmi) {
208                 up(&lustre_mount_info_lock);
209                 CERROR("Can't find mount for %s\n", name);
210                 RETURN(-ENOENT);
211         }
212         lsi = s2lsi(lmi->lmi_sb);
213         LASSERT(lmi->lmi_mnt == mnt);
214         unlock_mntput(lmi->lmi_mnt);
215
216         CDEBUG(D_MOUNT, "put_mnt %p from %s, refs=%d, vfscount=%d\n", 
217                lmi->lmi_mnt, name, atomic_read(&lsi->lsi_mounts),
218                atomic_read(&lmi->lmi_mnt->mnt_count));
219
220         if (lustre_put_lsi(lmi->lmi_sb)) {
221                 CDEBUG(D_MOUNT, "Last put of mnt %p from %s, vfscount=%d\n", 
222                        lmi->lmi_mnt, name, 
223                        atomic_read(&lmi->lmi_mnt->mnt_count));
224                 /* last mount is the One True Mount */
225                 if (atomic_read(&lmi->lmi_mnt->mnt_count) > 1)
226                         CERROR("%s: mount busy, vfscount=%d!\n", name,
227                                atomic_read(&lmi->lmi_mnt->mnt_count));
228         }
229         up(&lustre_mount_info_lock);
230
231         /* this obd should never need the mount again */
232         server_deregister_mount(name);
233         
234         RETURN(0);
235 }
236
237
238 /******* mount helper utilities *********/
239
240 static void ldd_print(struct lustre_disk_data *ldd)
241 {
242         int i;
243
244         PRINT_CMD(PRINT_MASK, "  disk data:\n"); 
245         PRINT_CMD(PRINT_MASK, "config:  %d\n", ldd->ldd_config_ver);
246         PRINT_CMD(PRINT_MASK, "fs:      %s\n", ldd->ldd_fsname);
247         PRINT_CMD(PRINT_MASK, "server:  %s\n", ldd->ldd_svname);
248         PRINT_CMD(PRINT_MASK, "index:   %04x\n", ldd->ldd_svindex);
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         if (!ldd->ldd_mgsnid_count) 
253                 PRINT_CMD(PRINT_MASK, "no MGS nids\n");
254         else for (i = 0; i < ldd->ldd_mgsnid_count; i++) {
255                 PRINT_CMD(PRINT_MASK, "mgs nid %d:  %s\n", i, 
256                        libcfs_nid2str(ldd->ldd_mgsnid[i]));
257         }
258         if (!ldd->ldd_failnid_count)
259                 PRINT_CMD(PRINT_MASK, "no failover nids\n");
260         else for (i = 0; i < ldd->ldd_failnid_count; i++) {
261                 PRINT_CMD(PRINT_MASK, "failover nid %d:  %s\n", i,
262                           libcfs_nid2str(ldd->ldd_failnid[i]));
263         }
264 }
265
266 static int ldd_parse(struct lvfs_run_ctxt *mount_ctxt, 
267                            struct lustre_disk_data *ldd)
268 {       
269         struct lvfs_run_ctxt saved;
270         struct file *file;
271         loff_t off = 0;
272         unsigned long len;
273         int rc;
274         ENTRY;
275                
276         push_ctxt(&saved, mount_ctxt, NULL);
277         
278         file = filp_open(MOUNT_DATA_FILE, O_RDONLY, 0644);
279         if (IS_ERR(file)) {
280                 rc = PTR_ERR(file);
281                 CERROR("cannot open %s: rc = %d\n", MOUNT_DATA_FILE, rc);
282                 GOTO(out, rc);
283         }
284  
285         len = file->f_dentry->d_inode->i_size;
286         CDEBUG(D_MOUNT, "Have %s, size %lu\n", MOUNT_DATA_FILE, len);
287         if (len != sizeof(*ldd)) {
288                 CERROR("disk data size does not match: see %lu expect %u\n", 
289                        len, sizeof(*ldd));
290                 GOTO(out_close, rc = -EINVAL);
291         }
292
293         rc = lustre_fread(file, ldd, len, &off);
294         if (rc != len) {
295                 CERROR("error reading %s: read %d of %lu\n", 
296                        MOUNT_DATA_FILE, rc, len);
297                 GOTO(out_close, rc = -EINVAL);
298         }
299         rc = 0;
300
301         if (ldd->ldd_magic != LDD_MAGIC) {
302                 /* FIXME add swabbing support */
303                 CERROR("Bad magic in %s: %x!=%x\n", MOUNT_DATA_FILE, 
304                        ldd->ldd_magic, LDD_MAGIC);
305                 GOTO(out_close, rc = -EINVAL);
306         }
307         
308         if (ldd->ldd_feature_incompat & ~LDD_INCOMPAT_SUPP) {
309                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
310                        ldd->ldd_svname, 
311                        ldd->ldd_feature_incompat & ~LDD_INCOMPAT_SUPP);
312                 GOTO(out_close, rc = -EINVAL);
313         }
314         if (ldd->ldd_feature_rocompat & ~LDD_ROCOMPAT_SUPP) {
315                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
316                        ldd->ldd_svname,  
317                        ldd->ldd_feature_rocompat & ~LDD_ROCOMPAT_SUPP);
318                 /* Do something like remount filesystem read-only */
319                 GOTO(out_close, rc = -EINVAL);
320         }
321
322         ldd_print(ldd);
323
324 out_close:
325         filp_close(file, 0);
326 out:
327         pop_ctxt(&saved, mount_ctxt, NULL);
328         RETURN(rc);
329 }
330
331 static int ldd_write(struct lvfs_run_ctxt *mount_ctxt, 
332                      struct lustre_disk_data *ldd)
333 {       
334         struct lvfs_run_ctxt saved;
335         struct file *file;
336         loff_t off = 0;
337         unsigned long len = sizeof(struct lustre_disk_data);
338         int rc = 0;
339         ENTRY;
340
341         LASSERT(ldd->ldd_magic == LDD_MAGIC);
342         
343         ldd->ldd_config_ver++;  
344
345         push_ctxt(&saved, mount_ctxt, NULL);
346         
347         file = filp_open(MOUNT_DATA_FILE, O_RDWR, 0644);
348         if (IS_ERR(file)) {
349                 rc = PTR_ERR(file);
350                 CERROR("cannot open %s: rc = %d\n", MOUNT_DATA_FILE, rc);
351                 GOTO(out, rc);
352         }
353  
354         rc = lustre_fwrite(file, ldd, len, &off);
355         if (rc != len) {
356                 CERROR("error writing %s: read %d of %lu\n", 
357                        MOUNT_DATA_FILE, rc, len);
358                 GOTO(out_close, rc = -EINVAL);
359         }
360
361         rc = 0;
362         ldd_print(ldd);
363
364 out_close:
365         filp_close(file, 0);
366 out:
367         pop_ctxt(&saved, mount_ctxt, NULL);
368         RETURN(rc);
369 }
370
371
372 /**************** config llog ********************/
373
374 /* Get a config log from the MGS and process it.
375    This func is called for both clients and servers. */
376 int lustre_process_log(struct super_block *sb, char *logname, 
377                      struct config_llog_instance *cfg)
378 {
379         struct lustre_cfg *lcfg;
380         struct lustre_cfg_bufs bufs;
381         struct lustre_sb_info *lsi = s2lsi(sb);
382         struct obd_device *mgc = lsi->lsi_mgc;
383         int rc;
384         ENTRY;
385
386         LASSERT(mgc);
387         LASSERT(cfg);
388
389         /* mgc_process_config */
390         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
391         lustre_cfg_bufs_set_string(&bufs, 1, logname);
392         lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
393         lustre_cfg_bufs_set(&bufs, 3, &sb, sizeof(sb));
394         lcfg = lustre_cfg_new(LCFG_LOG_START, &bufs);
395         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
396         lustre_cfg_free(lcfg);
397
398         if (rc) 
399                 LCONSOLE_ERROR("%s: The configuration '%s' could not be read "
400                                "(%d), mount will fail.\n",
401                                mgc->obd_name, logname, rc);
402
403         class_obd_list();
404         RETURN(rc);
405 }
406
407
408 int lustre_end_log(struct super_block *sb, char *logname, 
409                        struct config_llog_instance *cfg)
410 {
411         struct lustre_cfg *lcfg;
412         struct lustre_cfg_bufs bufs;
413         struct lustre_sb_info *lsi = s2lsi(sb);
414         struct obd_device *mgc = lsi->lsi_mgc;
415         int rc;
416         ENTRY;
417
418         LASSERT(mgc);
419
420         /* mgc_process_config */
421         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
422         lustre_cfg_bufs_set_string(&bufs, 1, logname);
423         if (cfg)
424                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
425         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
426         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
427         lustre_cfg_free(lcfg);
428         RETURN(0);
429 }
430
431 /**************** obd start *******************/
432
433 static int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
434                    char *s1, char *s2, char *s3, char *s4)
435 {
436         struct lustre_cfg_bufs bufs;
437         struct lustre_cfg    * lcfg = NULL;
438         int rc;
439                
440         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
441                cmd, s1, s2, s3, s4); 
442
443         lustre_cfg_bufs_reset(&bufs, cfgname);
444         if (s1) 
445                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
446         if (s2) 
447                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
448         if (s3) 
449                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
450         if (s4) 
451                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
452
453         lcfg = lustre_cfg_new(cmd, &bufs);
454         lcfg->lcfg_nid = nid;
455         rc = class_process_config(lcfg);
456         lustre_cfg_free(lcfg);
457         return(rc);
458 }
459
460 static int lustre_start_simple(char *obdname, char *type, char *uuid, 
461                                char *s1, char *s2)
462 {
463         int rc;
464         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
465
466         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, 0, 0);
467         if (rc) {
468                 CERROR("%s attach error %d\n", obdname, rc);
469                 return(rc);
470         }
471         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, 0, 0);
472         if (rc) {
473                 CERROR("%s setup error %d\n", obdname, rc);
474                 do_lcfg(obdname, 0, LCFG_DETACH, 0, 0, 0, 0);
475         }
476         return rc;
477 }
478
479 /* Set up a MGS to serve startup logs */
480 static int server_start_mgs(struct super_block *sb)
481 {
482         struct lustre_sb_info    *lsi = s2lsi(sb);
483         struct vfsmount          *mnt = lsi->lsi_srv_mnt;
484         struct lustre_mount_info *lmi;
485         int    rc = 0;
486         ENTRY;
487         LASSERT(mnt);
488
489         /* It is impossible to have more than 1 MGS per node, since
490            MGC wouldn't know which to connect to */
491         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
492         if (lmi) {
493                 lsi = s2lsi(lmi->lmi_sb);
494                 LCONSOLE_ERROR("The MGS service was already started from "
495                                "server %s\n", lsi->lsi_ldd->ldd_svname);
496                 RETURN(-EALREADY);
497         }
498
499         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
500
501         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb, mnt);
502
503         if (!rc &&
504             ((rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME, 
505                                        LUSTRE_MGS_OBDNAME, 0, 0)))) 
506                 server_deregister_mount(LUSTRE_MGS_OBDNAME);
507         
508         if (rc)                                
509                 LCONSOLE_ERROR("Failed to start MGS '%s' (%d).  Is the 'mgs' "
510                                "module loaded?\n", LUSTRE_MGS_OBDNAME, rc);
511
512         RETURN(rc);
513 }
514
515 static int server_stop_mgs(struct super_block *sb)
516 {
517         struct obd_device *obd;
518         int rc;
519         ENTRY;
520
521         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
522
523         /* There better be only one MGS */
524         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
525         if (!obd) {
526                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
527                 RETURN(-EALREADY);
528         }
529
530         /* The MGS should always stop when we say so */
531         obd->obd_force = 1;
532         rc = class_manual_cleanup(obd);
533         RETURN(rc);
534 }
535
536 /* Set up a mgcobd to process startup logs */
537 static int lustre_start_mgc(struct super_block *sb)
538 {
539         struct lustre_handle mgc_conn = {0, };
540         struct obd_connect_data ocd = { 0 };
541         struct lustre_sb_info *lsi = s2lsi(sb);
542         struct obd_device *obd;
543         struct obd_export *exp;
544         char *uuid;
545         lnet_nid_t nid;
546         lnet_process_id_t id;
547         int recov_bk;
548         int rc = 0, i;
549         ENTRY;
550
551         LASSERT(lsi->lsi_lmd);
552         
553         obd = class_name2obd(LUSTRE_MGC_OBDNAME);
554         if (obd) {
555                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
556                 /* FIXME There's only one MGC, but users could give different
557                    MGS nids on the mount line.  So now do we add new MGS uuids
558                    or not?  If there's truly one MGS per site, the MGS uuids
559                    _should_ all be the same. Maybe check here?
560                 */
561                 
562                 /* Try all connections, but only once (again). 
563                    We don't want to block another target from starting
564                    (using its local copy of the log), but we do want to connect
565                    if at all possible. */
566                 CDEBUG(D_MOUNT, "Set MGS reconnect\n");
567                 recov_bk = 1;
568                 rc = obd_set_info(obd->obd_self_export,
569                                   strlen(KEY_INIT_RECOV_BACKUP),
570                                   KEY_INIT_RECOV_BACKUP,
571                                   sizeof(recov_bk), &recov_bk);
572                 GOTO(out, rc = 0);
573         }
574
575         if (lsi->lsi_lmd->lmd_mgsnid_count == 0) {
576                 LCONSOLE_ERROR("No NIDs for the MGS were given.\n");
577                 RETURN(-EINVAL);
578         }
579
580         CDEBUG(D_MOUNT, "Start MGC '%s'\n", LUSTRE_MGC_OBDNAME);
581
582         /* Add the first uuid for the MGS */
583         nid = lsi->lsi_lmd->lmd_mgsnid[0];
584         rc = do_lcfg(LUSTRE_MGC_OBDNAME, nid, LCFG_ADD_UUID, 
585                      libcfs_nid2str(nid), 0,0,0);
586         if (rc < 0)
587                 RETURN(rc);
588
589         /* Generate a unique uuid for each MGC - use the 1st non-loopback nid */
590         i = 0;
591         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
592                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND) 
593                         continue;
594                 break;
595         }
596         OBD_ALLOC(uuid, sizeof(struct obd_uuid));
597         sprintf(uuid, "mgc_"LPX64, id.nid);
598         /* Start the MGC */
599         rc = lustre_start_simple(LUSTRE_MGC_OBDNAME, LUSTRE_MGC_NAME, 
600                                  uuid, LUSTRE_MGS_OBDNAME, libcfs_nid2str(nid));
601         OBD_FREE(uuid, sizeof(struct obd_uuid));
602         if (rc) 
603                 RETURN(rc);
604         
605         /* Add the redundant MGS nids */
606         for (i = 1; i < lsi->lsi_lmd->lmd_mgsnid_count; i++) {
607                 nid = lsi->lsi_lmd->lmd_mgsnid[i];
608                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, nid, LCFG_ADD_UUID, 
609                              libcfs_nid2str(nid), 0, 0, 0);
610                 if (rc) {
611                         CERROR("Add uuid for %s failed %d\n", 
612                                libcfs_nid2str(nid), rc);
613                         continue;
614                 }
615                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_ADD_CONN,
616                              libcfs_nid2str(nid), 0, 0, 0);
617                 if (rc) 
618                         CERROR("Add conn for %s failed %d\n", 
619                                libcfs_nid2str(nid), rc);
620         }
621         
622         obd = class_name2obd(LUSTRE_MGC_OBDNAME);
623         if (!obd) {
624                 CERROR("Can't find mgcobd %s\n", LUSTRE_MGC_OBDNAME);
625                 RETURN(-ENOTCONN);
626         }
627
628         /* Try all connections, but only once. */
629         recov_bk = 1;
630         rc = obd_set_info(obd->obd_self_export,
631                           strlen(KEY_INIT_RECOV_BACKUP), KEY_INIT_RECOV_BACKUP,
632                           sizeof(recov_bk), &recov_bk);
633         if (rc) 
634                 /* nonfatal */
635                 CERROR("can't set %s %d\n", KEY_INIT_RECOV_BACKUP, rc);
636        
637         /* FIXME add ACL support? */
638         //ocd.ocd_connect_flags = OBD_CONNECT_ACL;
639
640         /* We connect to the MGS at setup, and don't disconnect until cleanup */
641         rc = obd_connect(&mgc_conn, obd, &(obd->obd_uuid), &ocd);
642         if (rc) {
643                 CERROR("connect failed %d\n", rc);
644                 GOTO(out, rc);
645         }
646         
647         exp = class_conn2export(&mgc_conn);
648         obd->u.cli.cl_mgc_mgsexp = exp;
649
650         /* And keep a refcount of servers/clients who started with "mount",
651            so we know when we can get rid of the mgc. */
652         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
653
654 out:
655         /* Keep the mgc info in the sb. Note that many lsi's can point
656            to the same mgc.*/
657         lsi->lsi_mgc = obd;
658         RETURN(rc);
659 }
660
661 static int lustre_stop_mgc(struct super_block *sb)
662 {
663         struct lustre_sb_info *lsi = s2lsi(sb);
664         struct obd_device *obd;
665         lnet_nid_t nid;
666         int i, rc;
667         ENTRY;
668
669         if (!lsi)
670                 RETURN(-ENOENT);
671         obd = lsi->lsi_mgc;
672         if (!obd)
673                 RETURN(-ENOENT);
674
675         lsi->lsi_mgc = NULL;
676         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
677                 /* This is not fatal, every client that stops 
678                    will call in here. */
679                 CDEBUG(D_MOUNT, "mgc still has %d references.\n", 
680                        atomic_read(&obd->u.cli.cl_mgc_refcount));
681                 RETURN(-EBUSY); 
682         }
683
684         if (obd->u.cli.cl_mgc_mgsexp)
685                 obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
686
687         rc = class_manual_cleanup(obd);
688         if (rc)
689                 RETURN(rc);
690         
691         /* class_add_uuid adds a nid even if the same uuid exists; we might
692            delete any copy here.  So they all better match. */
693         for (i = 0; i < lsi->lsi_lmd->lmd_mgsnid_count; i++) {
694                 nid = lsi->lsi_lmd->lmd_mgsnid[i];
695                 rc = do_lcfg(obd->obd_name, nid, LCFG_DEL_UUID, 
696                               libcfs_nid2str(nid), 0, 0, 0);
697                 if (rc)
698                         CERROR("del MDC UUID %s failed: rc = %d\n", 
699                                libcfs_nid2str(nid), rc);
700         }
701         /* class_import_put will get rid of the additional connections */
702
703         RETURN(0);
704 }
705           
706 /* Since there's only one mgc per node, we have to change it's fs to get
707    access to the right disk. */
708 static int server_mgc_set_fs(struct obd_device *mgc, struct super_block *sb)
709 {
710         struct lustre_sb_info *lsi = s2lsi(sb);
711         int rc;
712         ENTRY;
713
714         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
715
716         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
717         rc = obd_set_info(mgc->obd_self_export,
718                           strlen("set_fs"), "set_fs",
719                           sizeof(*sb), sb);
720         if (rc) {
721                 CERROR("can't set_fs %d\n", rc);
722         }
723
724         RETURN(rc);
725 }
726
727 static int server_mgc_clear_fs(struct obd_device *mgc)
728 {
729         int rc;
730         ENTRY;
731
732         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
733         
734         rc = obd_set_info(mgc->obd_self_export,
735                           strlen("clear_fs"), "clear_fs", 0, NULL);
736         RETURN(rc);
737 }
738
739 /* Stop MDS/OSS if nobody is using them */
740 static int server_stop_servers(struct super_block *sb)
741 {
742         struct lustre_sb_info *lsi = s2lsi(sb);
743         struct obd_device *obd;
744         int rc = 0;
745         ENTRY;
746
747         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
748         if (IS_MDT(lsi->lsi_ldd) && (obd = class_name2obd("MDS"))) {
749                 //FIXME pre-rename, should eventually be LUSTRE_MDT_NAME
750                 struct obd_type *type = class_search_type(LUSTRE_MDS_NAME);
751                 if (!type || !type->typ_refcnt) {
752                         /* nobody is using the MDT type, clean the MDS */
753                         if (lsi->lsi_flags & LSI_UMOUNT_FORCE)
754                                 obd->obd_force = 1;
755                         if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
756                                 obd->obd_fail = 1;
757                         rc = class_manual_cleanup(obd);
758                 }
759         }
760
761         /* if this was an OST, and there are no more OST's, clean up the OSS */
762         if (IS_OST(lsi->lsi_ldd) && (obd = class_name2obd("OSS"))) {
763                 struct obd_type *type = class_search_type(LUSTRE_OST_NAME);
764                 if (!type || !type->typ_refcnt) {
765                         int err;
766                         /* nobody is using the OST type, clean the OSS */
767                         if (lsi->lsi_flags & LSI_UMOUNT_FORCE)
768                                 obd->obd_force = 1;
769                         if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
770                                 obd->obd_fail = 1;
771                         err = class_manual_cleanup(obd);
772                         if (!rc) 
773                                 rc = err;
774                 }
775         }
776         RETURN(rc);
777 }
778
779 static int server_sb2mti(struct super_block *sb, struct mgs_target_info *mti)
780 {       
781         struct lustre_sb_info   *lsi = s2lsi(sb);
782         struct lustre_disk_data *ldd = lsi->lsi_ldd;
783         lnet_process_id_t        id;
784         int i = 0;
785         ENTRY;
786
787         if (!mti) 
788                 RETURN(-ENOMEM);
789         if (!(lsi->lsi_flags & LSI_SERVER))
790                 RETURN(-EINVAL);
791
792         strncpy(mti->mti_fsname, ldd->ldd_fsname,
793                 sizeof(mti->mti_fsname));
794         strncpy(mti->mti_svname, ldd->ldd_svname,
795                 sizeof(mti->mti_svname));
796         
797         mti->mti_nid_count = 0;
798         while (LNetGetId(i++, &id) != -ENOENT) {
799                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND) 
800                         continue;
801                 mti->mti_nids[mti->mti_nid_count] = id.nid;
802                 mti->mti_nid_count++;
803                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
804                         CWARN("Only using first %d nids for %s\n",
805                               mti->mti_nid_count, mti->mti_svname);
806                         break;
807                 }
808         }       
809       
810         mti->mti_failnid_count = ldd->ldd_failnid_count;
811         memcpy(mti->mti_failnids, ldd->ldd_failnid, sizeof(mti->mti_failnids));
812         mti->mti_config_ver = 0;
813         mti->mti_flags = ldd->ldd_flags;
814         mti->mti_stripe_index = ldd->ldd_svindex;
815         mti->mti_stripe_count = ldd->ldd_stripe_count;
816         mti->mti_stripe_pattern = ldd->ldd_stripe_pattern;
817         mti->mti_stripe_size = ldd->ldd_stripe_sz; 
818         mti->mti_stripe_offset = ldd->ldd_stripe_offset;  
819         RETURN(0);
820 }
821
822 /* Register an old or new target with the MGS. If needed MGS will construct
823    startup logs and assign index */
824 int server_register_target(struct super_block *sb)
825 {       
826         struct lustre_sb_info *lsi = s2lsi(sb);
827         struct obd_device *mgc = lsi->lsi_mgc;
828         struct lustre_disk_data *ldd = lsi->lsi_ldd;
829         struct mgs_target_info *mti = NULL;
830         int rc;
831         ENTRY;
832
833         LASSERT(mgc);
834
835         if (!(lsi->lsi_flags & LSI_SERVER))
836                 RETURN(-EINVAL);
837
838         OBD_ALLOC_PTR(mti);
839         rc = server_sb2mti(sb, mti);
840         if (rc) 
841                 GOTO(out, rc);
842
843         CDEBUG(D_MOUNT, "%sregistration %s, fs=%s, %s, index=%04x, flags=%#x\n",
844                mti->mti_flags & LDD_F_NEED_REGISTER ? "Initial " : "",
845                mti->mti_svname, mti->mti_fsname,
846                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
847                mti->mti_flags);
848
849         /* Register the target */
850         /* FIXME use mdc_process_config instead */
851         rc = obd_set_info(mgc->u.cli.cl_mgc_mgsexp,
852                           strlen("add_target"), "add_target",
853                           sizeof(*mti), mti);
854         if (rc) {
855                 CERROR("registration with the MGS failed (%d)\n", rc);
856                 GOTO(out, rc);
857         }
858
859         /* If this flag is set, it means the MGS wants us to change our
860            on-disk data. (So far this means just the index.) */
861         if (mti->mti_flags & LDD_F_REWRITE) {
862                 CDEBUG(D_MOUNT, "Must change on-disk index from %#x to %#x for "
863                        " %s\n",
864                        ldd->ldd_svindex, mti->mti_stripe_index, 
865                        mti->mti_svname);
866                 ldd->ldd_svindex = mti->mti_stripe_index;
867                 strncpy(ldd->ldd_svname, mti->mti_svname, 
868                         sizeof(ldd->ldd_svname));
869                 /* or ldd_make_sv_name(ldd); */
870                 ldd->ldd_flags = mti->mti_flags & ~LDD_F_REWRITE;
871                 ldd_write(&mgc->obd_lvfs_ctxt, ldd);
872                 
873                 /* FIXME write last_rcvd?, disk label? */
874         }
875
876 out:
877         if (mti)        
878                 OBD_FREE_PTR(mti);
879         RETURN(rc);
880 }
881
882 /* Start targets */
883 static int server_start_targets(struct super_block *sb, struct vfsmount *mnt)
884 {
885         struct obd_device *obd;
886         struct lustre_sb_info *lsi = s2lsi(sb);
887         struct config_llog_instance cfg;
888         int rc;
889         ENTRY;
890
891         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_ldd->ldd_svname);
892         
893         /* If we're an MDT, make sure the global MDS is running */
894         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_MDT) {
895                 /* make sure (what will be called) the MDS is started */
896                 obd = class_name2obd("MDS");
897                 if (!obd) {
898                         //FIXME pre-rename, should eventually be LUSTRE_MDS_NAME
899                         rc = lustre_start_simple("MDS", LUSTRE_MDT_NAME, 
900                                                  "MDS_uuid", 0, 0);
901                         if (rc) {
902                                 CERROR("failed to start MDS: %d\n", rc);
903                                 GOTO(out_servers, rc);
904                         }
905                 }
906         }
907
908         /* If we're an OST, make sure the global OSS is running */
909         if (lsi->lsi_ldd->ldd_flags & LDD_F_SV_TYPE_OST) {
910                 /* make sure OSS is started */
911                 obd = class_name2obd("OSS");
912                 if (!obd) {
913                         rc = lustre_start_simple("OSS", LUSTRE_OSS_NAME,
914                                                  "OSS_uuid", 0, 0);
915                         if (rc) {
916                                 CERROR("failed to start OSS: %d\n", rc);
917                                 GOTO(out_servers, rc);
918                         }
919                 }
920         }
921
922         /* Set the mgc fs to our server disk.  This allows the MGC
923            to read and write configs locally. */
924         server_mgc_set_fs(lsi->lsi_mgc, sb);
925
926         /* Register with MGS */
927         rc = server_register_target(sb);
928         if (rc && (lsi->lsi_ldd->ldd_flags & 
929                    (LDD_F_NEED_INDEX | LDD_F_NEED_REGISTER | LDD_F_UPGRADE14))){
930                 CERROR("Required refistration failed for %s: %d\n", 
931                        lsi->lsi_ldd->ldd_svname, rc);
932                 GOTO(out, rc);
933         }
934
935         if (class_name2obd(lsi->lsi_ldd->ldd_svname)) {
936                 LCONSOLE_ERROR("The target named %s is already running\n",
937                                lsi->lsi_ldd->ldd_svname);
938                 GOTO(out, rc = -EBUSY);
939         }
940
941         /* Let the target look up the mount using the target's name 
942            (we can't pass the sb or mnt through class_process_config.) */
943         rc = server_register_mount(lsi->lsi_ldd->ldd_svname, sb, mnt);
944         if (rc) 
945                 GOTO(out, rc);
946
947         /* Start targets using the llog named for the target */
948         memset(&cfg, 0, sizeof(cfg));
949         rc = lustre_process_log(sb, lsi->lsi_ldd->ldd_svname, &cfg);
950         if (rc) {
951                 CERROR("failed to start server %s: %d\n",
952                        lsi->lsi_ldd->ldd_svname, rc);
953                 GOTO(out, rc);
954         }
955
956         if (!class_name2obd(lsi->lsi_ldd->ldd_svname)) {
957                 CERROR("no server named %s was started\n",
958                        lsi->lsi_ldd->ldd_svname);
959                 rc = -ENXIO;
960         }
961         
962 out:
963         /* Release the mgc fs for others to use */
964         server_mgc_clear_fs(lsi->lsi_mgc);
965
966 out_servers:
967         RETURN(rc);
968 }
969
970 /***************** mount **************/
971
972 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
973 {
974         struct lustre_sb_info *lsi = NULL;
975         ENTRY;
976
977         OBD_ALLOC(lsi, sizeof(*lsi));
978         if (!lsi)
979                 RETURN(NULL);
980         OBD_ALLOC(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
981         if (!lsi->lsi_lmd) {
982                 OBD_FREE(lsi, sizeof(*lsi));
983                 RETURN(NULL);
984         }
985
986         s2lsi_nocast(sb) = lsi;
987         /* we take 1 extra ref for our setup */
988         atomic_set(&lsi->lsi_mounts, 1);
989         RETURN(lsi);
990 }
991
992 static int lustre_free_lsi(struct super_block *sb)
993 {
994         struct lustre_sb_info *lsi = s2lsi(sb);
995         ENTRY;
996
997         if (!lsi)
998                 RETURN(0);
999                 
1000         CDEBUG(D_MOUNT, "Freeing lsi\n");
1001         
1002         /* someone didn't call server_put_mount. */
1003         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
1004
1005         if (lsi->lsi_ldd != NULL) 
1006                 OBD_FREE(lsi->lsi_ldd, sizeof(*lsi->lsi_ldd));
1007         
1008         if (lsi->lsi_lmd != NULL) {
1009                 if (lsi->lsi_lmd->lmd_dev != NULL) 
1010                         OBD_FREE(lsi->lsi_lmd->lmd_dev, 
1011                                  strlen(lsi->lsi_lmd->lmd_dev) + 1);
1012                 if (lsi->lsi_lmd->lmd_opts != NULL) 
1013                         OBD_FREE(lsi->lsi_lmd->lmd_opts, 
1014                                  strlen(lsi->lsi_lmd->lmd_opts) + 1);
1015                 OBD_FREE(lsi->lsi_lmd, sizeof(*lsi->lsi_lmd));
1016         }
1017         
1018         LASSERT(lsi->lsi_llsbi == NULL);
1019         
1020         server_deregister_mount_all(lsi->lsi_srv_mnt);
1021         
1022         OBD_FREE(lsi, sizeof(*lsi));
1023         s2lsi_nocast(sb) = NULL;
1024         
1025         RETURN(0);
1026 }
1027            
1028 static int lustre_put_lsi(struct super_block *sb)
1029 {
1030         struct lustre_sb_info *lsi = s2lsi(sb);
1031         ENTRY;
1032
1033         LASSERT(lsi);
1034         
1035         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
1036
1037         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
1038                 lustre_free_lsi(sb);
1039                 RETURN(1);
1040         }
1041         RETURN(0);
1042 }
1043
1044 /*************** server mount ******************/
1045
1046 /* Kernel mount using mount options in MOUNT_DATA_FILE */
1047 static struct vfsmount *server_kernel_mount(struct super_block *sb)
1048 {
1049         struct lvfs_run_ctxt mount_ctxt;
1050         struct lustre_sb_info *lsi = s2lsi(sb);
1051         struct lustre_disk_data *ldd;
1052         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1053         struct vfsmount *mnt;
1054         char *options = NULL;
1055         unsigned long page, s_flags;
1056         int rc;
1057         ENTRY;
1058
1059         OBD_ALLOC(ldd, sizeof(*ldd));
1060         if (!ldd)
1061                 RETURN(ERR_PTR(-ENOMEM));
1062
1063         /* In the past, we have always used flags = 0.
1064            Note ext3/ldiskfs can't be mounted ro. */
1065         s_flags = sb->s_flags;
1066
1067         /* Pre-mount ext3 to read the MOUNT_DATA_FILE */
1068         CDEBUG(D_MOUNT, "Pre-mount ext3 %s\n", lmd->lmd_dev);
1069         mnt = do_kern_mount("ext3", s_flags, lmd->lmd_dev, 0);
1070         if (IS_ERR(mnt)) {
1071                 rc = PTR_ERR(mnt);
1072                 CERROR("premount ext3 failed (%d), trying ldiskfs\n", rc);
1073                 /* If ext3 fails (bec. of mballoc, extents), try ldiskfs */
1074                 mnt = do_kern_mount("ldiskfs", s_flags, lmd->lmd_dev, 0);
1075                 if (IS_ERR(mnt)) {
1076                         rc = PTR_ERR(mnt);
1077                         CERROR("premount ldiskfs failed: rc = %d\n", rc);
1078                         GOTO(out_free, rc);
1079                 }
1080         }
1081
1082         OBD_SET_CTXT_MAGIC(&mount_ctxt);
1083         mount_ctxt.pwdmnt = mnt;
1084         mount_ctxt.pwd = mnt->mnt_root;
1085         mount_ctxt.fs = get_ds();
1086
1087         rc = ldd_parse(&mount_ctxt, ldd); 
1088         unlock_mntput(mnt);
1089
1090         if (rc) {
1091                 CERROR("premount parse options failed: rc = %d\n", rc);
1092                 GOTO(out_free, rc);
1093         }
1094
1095         /* Done with our pre-mount, now do the real mount. */
1096
1097         /* Glom up mount options */
1098         page = __get_free_page(GFP_KERNEL);
1099         if (!page) 
1100                 GOTO(out_free, rc = -ENOMEM);
1101
1102         options = (char *)page;
1103         memset(options, 0, PAGE_SIZE);
1104         strncpy(options, ldd->ldd_mount_opts, PAGE_SIZE - 2);
1105         
1106         /* Add in any mount-line options */
1107         if (lmd->lmd_opts && (*(lmd->lmd_opts) != 0)) {
1108                 int len = PAGE_SIZE - strlen(options) - 2;
1109                 if (*options != 0) 
1110                         strcat(options, ",");
1111                 strncat(options, lmd->lmd_opts, len);
1112         }
1113
1114         /* Special permanent mount flags */
1115         if (IS_OST(ldd)) 
1116             s_flags |= MS_NOATIME | MS_NODIRATIME;
1117
1118         CDEBUG(D_MOUNT, "kern_mount: %s %s %s\n",
1119                MT_STR(ldd), lmd->lmd_dev, options);
1120         mnt = do_kern_mount(MT_STR(ldd), s_flags, lmd->lmd_dev, 
1121                             (void *)options);
1122         free_page(page);
1123         if (IS_ERR(mnt)) {
1124                 rc = PTR_ERR(mnt);
1125                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1126                 GOTO(out_free, rc);
1127         }
1128
1129         lsi->lsi_ldd = ldd;   /* freed at lsi cleanup */
1130         CDEBUG(D_SUPER, "%s: mnt = %p\n", lmd->lmd_dev, mnt);
1131         RETURN(mnt);
1132
1133 out_free:
1134         OBD_FREE(ldd, sizeof(*ldd));
1135         lsi->lsi_ldd = NULL;    
1136         RETURN(ERR_PTR(rc));
1137 }
1138                       
1139 static void server_put_super(struct super_block *sb)
1140 {
1141         struct lustre_sb_info *lsi = s2lsi(sb);
1142         struct obd_device     *obd;
1143         struct vfsmount       *mnt = lsi->lsi_srv_mnt;
1144         int rc;
1145         ENTRY;
1146
1147         CDEBUG(D_MOUNT, "server put_super %s\n", lsi->lsi_ldd->ldd_svname);
1148                                                                                        
1149         /* tell the mgc to drop the config log */
1150         lustre_end_log(sb, lsi->lsi_ldd->ldd_svname, NULL);
1151
1152         obd = class_name2obd(lsi->lsi_ldd->ldd_svname);
1153         if (obd) {
1154                 CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1155                 if (lsi->lsi_flags & LSI_UMOUNT_FORCE)
1156                         obd->obd_force = 1;
1157                 if (lsi->lsi_flags & LSI_UMOUNT_FAILOVER)
1158                         obd->obd_fail = 1;
1159                 /* We can't seem to give an error return code
1160                    to .put_super, so we better make sure we clean up!
1161                    FIXME is there a way to get around this? */
1162                 obd->obd_force = 1;
1163                 class_manual_cleanup(obd);
1164         } else {
1165                 CERROR("no obd %s\n", lsi->lsi_ldd->ldd_svname);
1166                 server_deregister_mount(lsi->lsi_ldd->ldd_svname);
1167         }
1168
1169         server_stop_servers(sb);
1170
1171         /* If they wanted the mgs to stop separately from the mdt, they
1172            should have put it on a different device. */ 
1173         if (IS_MGS(lsi->lsi_ldd)) {
1174                 /* stop the mgc before the mgs so the connection gets cleaned
1175                    up */
1176                 lustre_stop_mgc(sb);
1177                 server_stop_mgs(sb);
1178         }
1179
1180         /* clean the mgc and sb */
1181         rc = lustre_common_put_super(sb);
1182         // FIXME how do I return a failure? 
1183
1184         /* drop the One True Mount */
1185         unlock_mntput(mnt);
1186
1187         CDEBUG(D_MOUNT, "umount done\n");
1188         EXIT;
1189 }
1190
1191 static void server_umount_begin(struct super_block *sb)
1192 {
1193         struct lustre_sb_info *lsi = s2lsi(sb);
1194         ENTRY;
1195
1196         CDEBUG(D_MOUNT, "umount -f\n");
1197         /* umount = normal
1198            umount -f = failover
1199            no third way to do LSI_UMOUNT_FORCE */
1200         lsi->lsi_flags |= LSI_UMOUNT_FAILOVER;
1201         EXIT;
1202 }
1203
1204 static int server_statfs (struct super_block *sb, struct kstatfs *buf)
1205 {
1206         struct vfsmount *mnt = s2lsi(sb)->lsi_srv_mnt;
1207         ENTRY;
1208
1209         if (mnt && mnt->mnt_sb && mnt->mnt_sb->s_op->statfs) {
1210                 int rc = mnt->mnt_sb->s_op->statfs(mnt->mnt_sb, buf);
1211                 if (!rc) {
1212                         buf->f_type = sb->s_magic;
1213                         RETURN(0);
1214                 }
1215         }
1216         
1217         /* just return 0 */
1218         buf->f_type = sb->s_magic;
1219         buf->f_bsize = sb->s_blocksize;
1220         buf->f_blocks = 1;
1221         buf->f_bfree = 0;
1222         buf->f_bavail = 0;
1223         buf->f_files = 1;
1224         buf->f_ffree = 0;
1225         buf->f_namelen = NAME_MAX;
1226         RETURN(0);
1227 }
1228
1229 static struct super_operations server_ops =
1230 {
1231         .put_super      = server_put_super,
1232         .umount_begin   = server_umount_begin, /* umount -f */
1233         .statfs         = server_statfs,
1234 };
1235
1236 #define log2(n) ffz(~(n))
1237 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1238
1239 static int server_fill_super_common(struct super_block *sb)
1240 {
1241         struct inode *root = 0;
1242         ENTRY;
1243                                                                                  
1244         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1245                                                                                  
1246         sb->s_blocksize = 4096;
1247         sb->s_blocksize_bits = log2(sb->s_blocksize);
1248         sb->s_magic = LUSTRE_SUPER_MAGIC;
1249         sb->s_maxbytes = 0; //PAGE_CACHE_MAXBYTES;
1250         sb->s_flags |= MS_RDONLY;
1251         sb->s_op = &server_ops;
1252  
1253         root = new_inode(sb);
1254         if (!root) {
1255                 CERROR("Can't make root inode\n");
1256                 RETURN(-EIO);
1257         }
1258                                                                                  
1259         /* returns -EIO for every operation */
1260         /* make_bad_inode(root); -- badness - can't umount */
1261         /* apparently we need to be a directory for the mount to finish */
1262         root->i_mode = S_IFDIR;
1263                                                                                  
1264         sb->s_root = d_alloc_root(root);
1265         if (!sb->s_root) {
1266                 CERROR("Can't make root dentry\n");
1267                 iput(root);
1268                 RETURN(-EIO);
1269         }
1270                                                                                  
1271         RETURN(0);
1272 }
1273      
1274 static int server_fill_super(struct super_block *sb)
1275 {
1276         struct lustre_sb_info *lsi = s2lsi(sb);
1277         struct vfsmount *mnt;
1278         int mgs_service = 0, i = 0, rc;
1279         ENTRY;
1280
1281         /* the One True Mount */
1282         mnt = server_kernel_mount(sb);
1283         if (IS_ERR(mnt)) {
1284                 rc = PTR_ERR(mnt);
1285                 CERROR("Unable to mount device %s: %d\n", 
1286                       lsi->lsi_lmd->lmd_dev, rc);
1287                 GOTO(out, rc);
1288         }
1289         lsi->lsi_srv_mnt = mnt;
1290
1291         LASSERT(lsi->lsi_ldd);
1292         CDEBUG(D_MOUNT, "Found service %s for fs '%s' on device %s\n",
1293                lsi->lsi_ldd->ldd_svname, lsi->lsi_ldd->ldd_fsname, 
1294                lsi->lsi_lmd->lmd_dev);
1295
1296         /* append on-disk MGS nids to mount-line MGS nids */
1297         for (i = 0; (i < lsi->lsi_ldd->ldd_mgsnid_count) && 
1298               (lsi->lsi_lmd->lmd_mgsnid_count < MTI_NIDS_MAX); i++) {
1299                 lsi->lsi_lmd->lmd_mgsnid[lsi->lsi_lmd->lmd_mgsnid_count++] = 
1300                         lsi->lsi_ldd->ldd_mgsnid[i];
1301         }
1302
1303         /* start MGS before MGC */
1304         if (IS_MGS(lsi->lsi_ldd)) {
1305                 rc = server_start_mgs(sb);
1306                 if (rc) {
1307                         CERROR("ignoring Failed MGS start!!\n");
1308                         //GOTO(out_mnt, rc);
1309                 } else {
1310                         /* add local nids (including LO) to MGS nids */
1311                         lnet_process_id_t id;
1312                         int j = lsi->lsi_lmd->lmd_mgsnid_count;
1313                         i = 0;
1314                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
1315                                 if (j >= MTI_NIDS_MAX) 
1316                                         break;
1317                                 lsi->lsi_lmd->lmd_mgsnid[j++] = id.nid;
1318                         }     
1319                         lsi->lsi_lmd->lmd_mgsnid_count = j;
1320
1321                         mgs_service++;
1322                 }
1323         }
1324
1325         rc = lustre_start_mgc(sb);
1326         if (rc) 
1327                 GOTO(out_mnt, rc);
1328
1329         /* Set up all obd devices for service */
1330         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) && 
1331                 (IS_OST(lsi->lsi_ldd) || IS_MDT(lsi->lsi_ldd))) {
1332                 rc = server_start_targets(sb, mnt);
1333                 if (rc < 0) {
1334                         CERROR("Unable to start targets: %d\n", rc);
1335                         GOTO(out_mnt, rc);
1336                 }
1337         /* FIXME overmount client here,
1338            or can we just start a client log and client_fill_super on this sb? 
1339            We need to make sure server_put_super gets called too - ll_put_super
1340            calls lustre_common_put_super; check there for LSI_SERVER flag, 
1341            call s_p_s if so. 
1342            Probably should start client from new thread so we can return.
1343            Client will not finish until all servers are connected.
1344            Note - MGMT-only server does NOT get a client, since there is no
1345            lustre fs associated - the MGMT is for all lustre fs's */
1346         }
1347
1348         rc = server_fill_super_common(sb);
1349         if (rc) 
1350                 GOTO(out_mnt, rc);
1351
1352         RETURN(0);
1353
1354 out_mnt:
1355         server_put_super(sb);
1356 out:
1357         RETURN(rc);
1358 }
1359
1360
1361 /*************** mount common betweeen server and client ***************/
1362
1363 /* Common umount */
1364 int lustre_common_put_super(struct super_block *sb)
1365 {
1366         int rc;
1367         ENTRY;
1368
1369         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
1370         
1371         rc = lustre_stop_mgc(sb);
1372         if (rc && (rc != -ENOENT)) {
1373                 if (rc != -EBUSY) {
1374                         CERROR("Can't stop MGC: %d\n", rc);
1375                         RETURN(rc);
1376                 }
1377                 /* BUSY just means that there's some other obd that
1378                    needs the mgc.  Let him clean it up. */
1379                 CDEBUG(D_MOUNT, "MGC busy, will stop later\n");
1380         }
1381         lustre_put_lsi(sb);
1382         RETURN(rc);
1383 }      
1384
1385 static void lmd_print(struct lustre_mount_data *lmd)
1386 {
1387         int i;
1388
1389         PRINT_CMD(PRINT_MASK, "  mount data:\n"); 
1390         if (!lmd->lmd_mgsnid_count) 
1391                 PRINT_CMD(PRINT_MASK, "no MGS nids\n");
1392         else for (i = 0; i < lmd->lmd_mgsnid_count; i++) {
1393                 PRINT_CMD(PRINT_MASK, "nid %d:  %s\n", i, 
1394                        libcfs_nid2str(lmd->lmd_mgsnid[i]));
1395         }
1396         if (lmd_is_client(lmd)) 
1397                 PRINT_CMD(PRINT_MASK, "fsname:  %s\n", lmd->lmd_dev);
1398         else
1399                 PRINT_CMD(PRINT_MASK, "device:  %s\n", lmd->lmd_dev);
1400         PRINT_CMD(PRINT_MASK, "flags:   %x\n", lmd->lmd_flags);
1401         if (lmd->lmd_opts)
1402                 PRINT_CMD(PRINT_MASK, "options: %s\n", lmd->lmd_opts);
1403 }
1404
1405 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1406 {
1407         char *s1, *s2, *devname = NULL;
1408         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1409         ENTRY;
1410
1411         LASSERT(lmd);
1412         if (!options) {
1413                 LCONSOLE_ERROR("Missing mount data: check that " 
1414                                "/sbin/mount.lustre is installed.\n");
1415                 RETURN(-EINVAL);          
1416         }
1417         
1418         /* Options should be a string - try to detect old lmd data */
1419         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) { 
1420                 LCONSOLE_ERROR("You're using an old version of "        
1421                                "/sbin/mount.lustre.  Please install version "   
1422                                "1.%d\n", LMD_MAGIC & 0xFF);     
1423                 RETURN(-EINVAL);
1424         }
1425         lmd->lmd_magic = LMD_MAGIC;
1426
1427         /* default flags */
1428         lmd->lmd_flags |= LMD_FLG_RECOVER;
1429
1430         s1 = options;
1431         while(*s1) {
1432                 while (*s1 == ' ' || *s1 == ',')
1433                         s1++;
1434                 /* Client options are parsed in ll_options: eg. flock, 
1435                    user_xattr, acl */
1436                 
1437                 if (strncmp(s1, "recov", 5) == 0) 
1438                         /* FIXME do something with the RECOVER flag - see lconf */
1439                         lmd->lmd_flags |= LMD_FLG_RECOVER;
1440                 else if (strncmp(s1, "norecov", 7) == 0)
1441                         lmd->lmd_flags &= ~LMD_FLG_RECOVER;
1442                 else if (strncmp(s1, "nosvc", 5) == 0)
1443                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1444
1445                 else if (strncmp(s1, "exclude=", 8) == 0) {
1446                         CERROR("Exclude: %s\n", s1);
1447                         /* FIXME implement */
1448                         /* store exlusion list in lmd_exclude, mdt & client
1449                          must check */
1450                 }
1451
1452                 /* Linux 2.4 doesn't pass the device, so we stuck it at the 
1453                    end of the options. */
1454                 else if (strncmp(s1, "device=", 7) == 0) {
1455                         devname = s1 + 7;
1456                         /* terminate options right before device.  device
1457                            must be the last one. */
1458                         *s1 = 0;
1459                 }
1460                 s2 = strstr(s1, ",");
1461                 if (s2 == NULL) 
1462                         break;
1463                 s1 = s2 + 1;
1464         }
1465
1466         if (!devname) {
1467                 LCONSOLE_ERROR("Can't find the device name "
1468                                "(need mount option 'device=...')\n");
1469                 goto invalid;
1470         }
1471
1472         if (strchr(devname, ',')) {
1473                 LCONSOLE_ERROR("Device name must be the final option\n");
1474                 goto invalid;
1475         }
1476
1477         s1 = devname;
1478         /* Get MGS nids if client mount:  uml1@tcp:uml2@tcp:/fsname-client */
1479         while ((s2 = strchr(s1, ':'))) {
1480                 lnet_nid_t nid;
1481                 *s2 = 0;
1482                 lmd->lmd_flags = LMD_FLG_CLIENT;
1483                 nid = libcfs_str2nid(s1);
1484                 if (nid == LNET_NID_ANY) {
1485                         LCONSOLE_ERROR("Can't parse NID '%s'\n", s1);
1486                         goto invalid;
1487                 }
1488                 if (lmd->lmd_mgsnid_count >= MTI_NIDS_MAX) {
1489                         LCONSOLE_ERROR("Too many NIDs: '%s'\n", s1);
1490                         goto invalid;
1491                 }
1492                 lmd->lmd_mgsnid[lmd->lmd_mgsnid_count++] = nid;
1493                 s1 = s2 + 1;
1494         }
1495
1496         if (lmd_is_client(lmd)) {
1497                 /* Remove leading /s from fsname */
1498                 while (*++s1 == '/')
1499                         ;
1500         }
1501
1502         if (*s1 == 0) {
1503                 LCONSOLE_ERROR("No filesytem specified\n");
1504                 goto invalid;
1505         }
1506
1507         /* freed in lustre_free_lsi */
1508         OBD_ALLOC(lmd->lmd_dev, strlen(s1) + 1);
1509         if (!lmd->lmd_dev) 
1510                 RETURN(-ENOMEM);
1511         strcpy(lmd->lmd_dev, s1);
1512         
1513         /* save mount options */
1514         s1 = options + strlen(options) - 1;
1515         while (s1 >= options && (*s1 == ',' || *s1 == ' ')) 
1516                 *s1-- = 0;
1517         if (*options != 0) {
1518                 /* freed in lustre_free_lsi */
1519                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1520                 if (!lmd->lmd_opts) 
1521                         RETURN(-ENOMEM);
1522                 strcpy(lmd->lmd_opts, options);
1523         }
1524
1525         lmd->lmd_magic = LMD_MAGIC;
1526
1527         lmd_print(lmd);
1528         RETURN(0);
1529
1530 invalid:
1531         CERROR("Bad mount options %s\n", options);
1532         RETURN(-EINVAL);          
1533 }
1534
1535
1536 /* Common mount */
1537 int lustre_fill_super(struct super_block *sb, void *data, int silent)
1538 {
1539         struct lustre_mount_data *lmd;
1540         struct lustre_sb_info *lsi;
1541         int rc;
1542         ENTRY;
1543  
1544         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1545         
1546         lsi = lustre_init_lsi(sb);
1547         if (!lsi) 
1548                 RETURN(-ENOMEM);
1549         lmd = lsi->lsi_lmd;
1550
1551         /* Figure out the lmd from the mount options */
1552         if (lmd_parse((char *)data, lmd)) {
1553                 lustre_put_lsi(sb);
1554                 RETURN(-EINVAL);
1555         }
1556
1557         if (lmd_is_client(lmd)) {
1558                 CDEBUG(D_MOUNT, "Mounting client for fs %s\n", lmd->lmd_dev);
1559                 if (!client_fill_super) {
1560                         LCONSOLE_ERROR("Nothing registered for client mount!"
1561                                " Is llite module loaded?\n");
1562                         rc = -ENOSYS;
1563                 } else {
1564                         rc = lustre_start_mgc(sb);
1565                         if (rc) 
1566                                 goto out;
1567                         /* Connect and start */
1568                         /* (should always be ll_fill_super) */
1569                         rc = (*client_fill_super)(sb);
1570                         if (rc) 
1571                                 lustre_common_put_super(sb);
1572                 }
1573         } else {
1574                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1575                 lsi->lsi_flags |= LSI_SERVER;
1576                 rc = server_fill_super(sb);
1577                 /* s_f_s calls lustre_start_mgc after the mount because we need
1578                    the MGS nids which are stored on disk.  Plus, we may
1579                    need to start the MGS first. */
1580                 /* s_f_s will call server_put_super on failure */
1581         }
1582                                                                                 
1583 out:
1584         if (rc){
1585                 CERROR("Unable to mount %s\n", 
1586                        s2lsi(sb) ? lmd->lmd_dev : "");
1587         } else {
1588                 CDEBUG(D_MOUNT, "Successfully mounted %s\n", lmd->lmd_dev);
1589         }
1590         RETURN(rc);
1591
1592                                                                                
1593
1594 /* We can't call ll_fill_super by name because it lives in a module that
1595    must be loaded after this one. */
1596 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb))
1597 {
1598         client_fill_super = cfs;
1599 }
1600
1601 /***************** FS registration ******************/
1602
1603 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1604 /* 2.5 and later */
1605 struct super_block * lustre_get_sb(struct file_system_type *fs_type,
1606                                int flags, const char *devname, void * data)
1607 {
1608         /* calls back in fill super */
1609         /* we could append devname= onto options (*data) here, 
1610            but 2.4 doesn't get devname.  So we do it in mount_lustre.c */
1611         return get_sb_nodev(fs_type, flags, data, lustre_fill_super);
1612 }
1613
1614 struct file_system_type lustre_fs_type = {
1615         .owner        = THIS_MODULE,
1616         .name         = "lustre",
1617         .get_sb       = lustre_get_sb,
1618         .kill_sb      = kill_anon_super,
1619         .fs_flags     = FS_BINARY_MOUNTDATA,
1620 };
1621
1622 #else
1623 /* 2.4 */
1624 static struct super_block *lustre_read_super(struct super_block *sb,
1625                                              void *data, int silent)
1626 {
1627         int rc;
1628         ENTRY;
1629
1630         rc = lustre_fill_super(sb, data, silent);
1631         if (rc)
1632                 RETURN(NULL);
1633         RETURN(sb);
1634 }
1635
1636 static struct file_system_type lustre_fs_type = {
1637         .owner          = THIS_MODULE,
1638         .name           = "lustre",
1639         .fs_flags       = FS_NFSEXP_FSID,
1640         .read_super     = lustre_read_super,
1641 };
1642 #endif
1643
1644 int lustre_register_fs(void)
1645 {
1646         return register_filesystem(&lustre_fs_type);
1647 }
1648
1649 int lustre_unregister_fs(void)
1650 {
1651         return unregister_filesystem(&lustre_fs_type);
1652 }
1653
1654 EXPORT_SYMBOL(lustre_register_client_fill_super);
1655 EXPORT_SYMBOL(lustre_common_put_super);
1656 EXPORT_SYMBOL(lustre_process_log);
1657 EXPORT_SYMBOL(lustre_end_log);
1658 EXPORT_SYMBOL(server_get_mount);
1659 EXPORT_SYMBOL(server_put_mount);
1660 EXPORT_SYMBOL(server_register_target);
1661
1662