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