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