Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[fs/lustre-release.git] / lustre / obdclass / obd_mount.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_mount.c
33  *
34  * Client mount routines
35  *
36  * Author: Nathan Rutman <nathan@clusterfs.com>
37  */
38
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
42 #define PRINT_CMD CDEBUG
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre/lustre_user.h>
47 #include <linux/version.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <lustre_param.h>
51
52 static int (*client_fill_super)(struct super_block *sb,
53                                 struct vfsmount *mnt);
54
55 static void (*kill_super_cb)(struct super_block *sb);
56
57 /**************** config llog ********************/
58
59 /** Get a config log from the MGS and process it.
60  * This func is called for both clients and servers.
61  * Continue to process new statements appended to the logs
62  * (whenever the config lock is revoked) until lustre_end_log
63  * is called.
64  * @param sb The superblock is used by the MGC to write to the local copy of
65  *   the config log
66  * @param logname The name of the llog to replicate from the MGS
67  * @param cfg Since the same mgc may be used to follow multiple config logs
68  *   (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
69  *   this log, and is added to the mgc's list of logs to follow.
70  */
71 int lustre_process_log(struct super_block *sb, char *logname,
72                      struct config_llog_instance *cfg)
73 {
74         struct lustre_cfg *lcfg;
75         struct lustre_cfg_bufs *bufs;
76         struct lustre_sb_info *lsi = s2lsi(sb);
77         struct obd_device *mgc = lsi->lsi_mgc;
78         int rc;
79         ENTRY;
80
81         LASSERT(mgc);
82         LASSERT(cfg);
83
84         OBD_ALLOC_PTR(bufs);
85         if (bufs == NULL)
86                 RETURN(-ENOMEM);
87
88         /* mgc_process_config */
89         lustre_cfg_bufs_reset(bufs, mgc->obd_name);
90         lustre_cfg_bufs_set_string(bufs, 1, logname);
91         lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
92         lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
93         lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
94         if (lcfg == NULL)
95                 GOTO(out, rc = -ENOMEM);
96         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
97         lustre_cfg_free(lcfg);
98 out:
99         OBD_FREE_PTR(bufs);
100
101         if (rc == -EINVAL)
102                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
103                                    "failed from the MGS (%d).  Make sure this "
104                                    "client and the MGS are running compatible "
105                                    "versions of Lustre.\n",
106                                    mgc->obd_name, logname, rc);
107         else if (rc != 0)
108                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
109                                    "failed (%d). This may be the result of "
110                                    "communication errors between this node and "
111                                    "the MGS, a bad configuration, or other "
112                                    "errors. See the syslog for more "
113                                    "information.\n", mgc->obd_name, logname,
114                                    rc);
115
116         /* class_obd_list(); */
117         RETURN(rc);
118 }
119 EXPORT_SYMBOL(lustre_process_log);
120
121 /* Stop watching this config log for updates */
122 int lustre_end_log(struct super_block *sb, char *logname,
123                        struct config_llog_instance *cfg)
124 {
125         struct lustre_cfg *lcfg;
126         struct lustre_cfg_bufs bufs;
127         struct lustre_sb_info *lsi = s2lsi(sb);
128         struct obd_device *mgc = lsi->lsi_mgc;
129         int rc;
130         ENTRY;
131
132         if (!mgc)
133                 RETURN(-ENOENT);
134
135         /* mgc_process_config */
136         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
137         lustre_cfg_bufs_set_string(&bufs, 1, logname);
138         if (cfg)
139                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
140         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
141         if (lcfg == NULL)
142                 RETURN(-ENOMEM);
143         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
144         lustre_cfg_free(lcfg);
145         RETURN(rc);
146 }
147 EXPORT_SYMBOL(lustre_end_log);
148
149 /**************** obd start *******************/
150
151 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
152  * lctl (and do for echo cli/srv.
153  */
154 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
155             char *s1, char *s2, char *s3, char *s4)
156 {
157         struct lustre_cfg_bufs bufs;
158         struct lustre_cfg    * lcfg = NULL;
159         int rc;
160
161         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
162                cmd, s1, s2, s3, s4);
163
164         lustre_cfg_bufs_reset(&bufs, cfgname);
165         if (s1)
166                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
167         if (s2)
168                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
169         if (s3)
170                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
171         if (s4)
172                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
173
174         lcfg = lustre_cfg_new(cmd, &bufs);
175         if (lcfg == NULL)
176                 return -ENOMEM;
177         lcfg->lcfg_nid = nid;
178         rc = class_process_config(lcfg);
179         lustre_cfg_free(lcfg);
180         return(rc);
181 }
182
183 /** Call class_attach and class_setup.  These methods in turn call
184  * obd type-specific methods.
185  */
186 int lustre_start_simple(char *obdname, char *type, char *uuid,
187                         char *s1, char *s2, char *s3, char *s4)
188 {
189         int rc;
190         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
191
192         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
193         if (rc) {
194                 CERROR("%s attach error %d\n", obdname, rc);
195                 return rc;
196         }
197         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
198         if (rc) {
199                 CERROR("%s setup error %d\n", obdname, rc);
200                 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
201         }
202         return rc;
203 }
204
205 static DEFINE_MUTEX(mgc_start_lock);
206
207 /** Set up a mgc obd to process startup logs
208  *
209  * \param sb [in] super block of the mgc obd
210  *
211  * \retval 0 success, otherwise error code
212  */
213 int lustre_start_mgc(struct super_block *sb)
214 {
215         struct obd_connect_data *data = NULL;
216         struct lustre_sb_info *lsi = s2lsi(sb);
217         struct obd_device *obd;
218         struct obd_export *exp;
219         struct obd_uuid *uuid;
220         class_uuid_t uuidc;
221         lnet_nid_t nid;
222         char nidstr[LNET_NIDSTR_SIZE];
223         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
224         char *ptr;
225         int rc = 0, i = 0, j;
226         size_t len;
227         ENTRY;
228
229         LASSERT(lsi->lsi_lmd);
230
231         /* Find the first non-lo MGS nid for our MGC name */
232         if (IS_SERVER(lsi)) {
233                 /* mount -o mgsnode=nid */
234                 ptr = lsi->lsi_lmd->lmd_mgs;
235                 if (lsi->lsi_lmd->lmd_mgs &&
236                     (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
237                         i++;
238                 } else if (IS_MGS(lsi)) {
239                         struct lnet_process_id id;
240
241                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
242                                 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
243                                         continue;
244                                 nid = id.nid;
245                                 i++;
246                                 break;
247                         }
248                 }
249         } else { /* client */
250                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
251                 ptr = lsi->lsi_lmd->lmd_dev;
252                 if (class_parse_nid(ptr, &nid, &ptr) == 0)
253                         i++;
254         }
255         if (i == 0) {
256                 CERROR("No valid MGS nids found.\n");
257                 RETURN(-EINVAL);
258         }
259
260         mutex_lock(&mgc_start_lock);
261
262         libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
263         len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
264         OBD_ALLOC(mgcname, len);
265         OBD_ALLOC(niduuid, len + 2);
266         if (mgcname == NULL || niduuid == NULL)
267                 GOTO(out_free, rc = -ENOMEM);
268         snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
269
270         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
271
272         OBD_ALLOC_PTR(data);
273         if (data == NULL)
274                 GOTO(out_free, rc = -ENOMEM);
275
276         obd = class_name2obd(mgcname);
277         if (obd && !obd->obd_stopping) {
278                 int recov_bk;
279
280                 rc = obd_set_info_async(NULL, obd->obd_self_export,
281                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
282                                         strlen(mgssec), mgssec, NULL);
283                 if (rc)
284                         GOTO(out_free, rc);
285
286                 /* Re-using an existing MGC */
287                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
288
289                 /* IR compatibility check, only for clients */
290                 if (lmd_is_client(lsi->lsi_lmd)) {
291                         int has_ir;
292                         int vallen = sizeof(*data);
293                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
294
295                         rc = obd_get_info(NULL, obd->obd_self_export,
296                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
297                                           &vallen, data);
298                         LASSERT(rc == 0);
299                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
300                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
301                                 /* LMD_FLG_NOIR is for test purpose only */
302                                 LCONSOLE_WARN(
303                                     "Trying to mount a client with IR setting "
304                                     "not compatible with current mgc. "
305                                     "Force to use current mgc setting that is "
306                                     "IR %s.\n",
307                                     has_ir ? "enabled" : "disabled");
308                                 if (has_ir)
309                                         *flags &= ~LMD_FLG_NOIR;
310                                 else
311                                         *flags |= LMD_FLG_NOIR;
312                         }
313                 }
314
315                 recov_bk = 0;
316                 /* If we are restarting the MGS, don't try to keep the MGC's
317                    old connection, or registration will fail. */
318                 if (IS_MGS(lsi)) {
319                         CDEBUG(D_MOUNT, "New MGS with live MGC\n");
320                         recov_bk = 1;
321                 }
322
323                 /* Try all connections, but only once (again).
324                    We don't want to block another target from starting
325                    (using its local copy of the log), but we do want to connect
326                    if at all possible. */
327                 recov_bk++;
328                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
329                 rc = obd_set_info_async(NULL, obd->obd_self_export,
330                                         sizeof(KEY_INIT_RECOV_BACKUP),
331                                         KEY_INIT_RECOV_BACKUP,
332                                         sizeof(recov_bk), &recov_bk, NULL);
333                 GOTO(out, rc = 0);
334         }
335
336         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
337
338         /* Add the primary nids for the MGS */
339         i = 0;
340         snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
341         if (IS_SERVER(lsi)) {
342                 ptr = lsi->lsi_lmd->lmd_mgs;
343                 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
344                 if (IS_MGS(lsi)) {
345                         /* Use local nids (including LO) */
346                         struct lnet_process_id id;
347
348                         while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
349                                 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
350                                              niduuid, NULL, NULL, NULL);
351                         }
352                 } else {
353                         /* Use mgsnode= nids */
354                         /* mount -o mgsnode=nid */
355                         if (lsi->lsi_lmd->lmd_mgs) {
356                                 ptr = lsi->lsi_lmd->lmd_mgs;
357                         } else if (class_find_param(ptr, PARAM_MGSNODE,
358                                                     &ptr) != 0) {
359                                 CERROR("No MGS nids given.\n");
360                                 GOTO(out_free, rc = -EINVAL);
361                         }
362                         /*
363                          * Add primary MGS nid(s).
364                          * Multiple nids on one MGS node are separated
365                          * by commas.
366                          */
367                         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
368                                 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
369                                              niduuid, NULL, NULL, NULL);
370                                 if (rc == 0)
371                                         ++i;
372                                 /* Stop at the first failover nid */
373                                 if (*ptr == ':')
374                                         break;
375                         }
376                 }
377         } else { /* client */
378                 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
379                 ptr = lsi->lsi_lmd->lmd_dev;
380                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
381                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
382                                      niduuid, NULL, NULL, NULL);
383                         if (rc == 0)
384                                 ++i;
385                         /* Stop at the first failover nid */
386                         if (*ptr == ':')
387                                 break;
388                 }
389         }
390         if (i == 0) {
391                 CERROR("No valid MGS nids found.\n");
392                 GOTO(out_free, rc = -EINVAL);
393         }
394         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
395
396         /* Random uuid for MGC allows easier reconnects */
397         OBD_ALLOC_PTR(uuid);
398         if (uuid == NULL)
399                 GOTO(out_free, rc = -ENOMEM);
400
401         ll_generate_random_uuid(uuidc);
402         class_uuid_unparse(uuidc, uuid);
403
404         /* Start the MGC */
405         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
406                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
407                                  niduuid, NULL, NULL);
408         OBD_FREE_PTR(uuid);
409         if (rc)
410                 GOTO(out_free, rc);
411
412         /* Add any failover MGS nids */
413         i = 1;
414         while (ptr && ((*ptr == ':' ||
415                class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
416                 /* New failover node */
417                 sprintf(niduuid, "%s_%x", mgcname, i);
418                 j = 0;
419                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
420                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
421                                      niduuid, NULL, NULL, NULL);
422                         if (rc == 0)
423                                 ++j;
424                         if (*ptr == ':')
425                                 break;
426                 }
427                 if (j > 0) {
428                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
429                                      niduuid, NULL, NULL, NULL);
430                         if (rc == 0)
431                                 ++i;
432                 } else {
433                         /* at ":/fsname" */
434                         break;
435                 }
436         }
437         lsi->lsi_lmd->lmd_mgs_failnodes = i;
438
439         obd = class_name2obd(mgcname);
440         if (!obd) {
441                 CERROR("Can't find mgcobd %s\n", mgcname);
442                 GOTO(out_free, rc = -ENOTCONN);
443         }
444
445         rc = obd_set_info_async(NULL, obd->obd_self_export,
446                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
447                                 strlen(mgssec), mgssec, NULL);
448         if (rc)
449                 GOTO(out_free, rc);
450
451         /* Keep a refcount of servers/clients who started with "mount",
452            so we know when we can get rid of the mgc. */
453         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
454
455         /* We connect to the MGS at setup, and don't disconnect until cleanup */
456         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
457                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
458                                   OBD_CONNECT_LVB_TYPE |
459                                   OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER;
460
461 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
462         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
463 #endif
464
465         if (lmd_is_client(lsi->lsi_lmd) &&
466             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
467                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
468         data->ocd_version = LUSTRE_VERSION_CODE;
469         rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
470         if (rc) {
471                 CERROR("connect failed %d\n", rc);
472                 GOTO(out, rc);
473         }
474
475         obd->u.cli.cl_mgc_mgsexp = exp;
476
477 out:
478         /* Keep the mgc info in the sb. Note that many lsi's can point
479            to the same mgc.*/
480         lsi->lsi_mgc = obd;
481 out_free:
482         mutex_unlock(&mgc_start_lock);
483
484         if (data)
485                 OBD_FREE_PTR(data);
486         if (mgcname)
487                 OBD_FREE(mgcname, len);
488         if (niduuid)
489                 OBD_FREE(niduuid, len + 2);
490         RETURN(rc);
491 }
492
493 static int lustre_stop_mgc(struct super_block *sb)
494 {
495         struct lustre_sb_info *lsi = s2lsi(sb);
496         struct obd_device *obd;
497         char *niduuid = NULL, *ptr = NULL;
498         int i, rc = 0, len = 0;
499         ENTRY;
500
501         if (!lsi)
502                 RETURN(-ENOENT);
503         obd = lsi->lsi_mgc;
504         if (!obd)
505                 RETURN(-ENOENT);
506         lsi->lsi_mgc = NULL;
507
508         mutex_lock(&mgc_start_lock);
509         LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
510         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
511                 /* This is not fatal, every client that stops
512                    will call in here. */
513                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
514                        atomic_read(&obd->u.cli.cl_mgc_refcount));
515                 GOTO(out, rc = -EBUSY);
516         }
517
518         /* The MGC has no recoverable data in any case.
519          * force shotdown set in umount_begin */
520         obd->obd_no_recov = 1;
521
522         if (obd->u.cli.cl_mgc_mgsexp) {
523                 /* An error is not fatal, if we are unable to send the
524                    disconnect mgs ping evictor cleans up the export */
525                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
526                 if (rc)
527                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
528         }
529
530         /* Save the obdname for cleaning the nid uuids, which are
531            obdname_XX */
532         len = strlen(obd->obd_name) + 6;
533         OBD_ALLOC(niduuid, len);
534         if (niduuid) {
535                 strcpy(niduuid, obd->obd_name);
536                 ptr = niduuid + strlen(niduuid);
537         }
538
539         rc = class_manual_cleanup(obd);
540         if (rc)
541                 GOTO(out, rc);
542
543         /* Clean the nid uuids */
544         if (!niduuid)
545                 GOTO(out, rc = -ENOMEM);
546
547         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
548                 sprintf(ptr, "_%x", i);
549                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
550                              niduuid, NULL, NULL, NULL);
551                 if (rc)
552                         CERROR("del MDC UUID %s failed: rc = %d\n",
553                                niduuid, rc);
554         }
555 out:
556         if (niduuid)
557                 OBD_FREE(niduuid, len);
558
559         /* class_import_put will get rid of the additional connections */
560         mutex_unlock(&mgc_start_lock);
561         RETURN(rc);
562 }
563
564 /***************** lustre superblock **************/
565
566 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
567 {
568         struct lustre_sb_info *lsi;
569         ENTRY;
570
571         OBD_ALLOC_PTR(lsi);
572         if (!lsi)
573                 RETURN(NULL);
574         OBD_ALLOC_PTR(lsi->lsi_lmd);
575         if (!lsi->lsi_lmd) {
576                 OBD_FREE_PTR(lsi);
577                 RETURN(NULL);
578         }
579
580         lsi->lsi_lmd->lmd_exclude_count = 0;
581         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
582         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
583         s2lsi_nocast(sb) = lsi;
584         /* we take 1 extra ref for our setup */
585         atomic_set(&lsi->lsi_mounts, 1);
586
587         /* Default umount style */
588         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
589         INIT_LIST_HEAD(&lsi->lsi_lwp_list);
590         spin_lock_init(&lsi->lsi_lwp_lock);
591
592         RETURN(lsi);
593 }
594
595 static int lustre_free_lsi(struct super_block *sb)
596 {
597         struct lustre_sb_info *lsi = s2lsi(sb);
598         ENTRY;
599
600         LASSERT(lsi != NULL);
601         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
602
603         /* someone didn't call server_put_mount. */
604         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
605
606         if (lsi->lsi_lmd != NULL) {
607                 if (lsi->lsi_lmd->lmd_dev != NULL)
608                         OBD_FREE(lsi->lsi_lmd->lmd_dev,
609                                 strlen(lsi->lsi_lmd->lmd_dev) + 1);
610                 if (lsi->lsi_lmd->lmd_profile != NULL)
611                         OBD_FREE(lsi->lsi_lmd->lmd_profile,
612                                 strlen(lsi->lsi_lmd->lmd_profile) + 1);
613                 if (lsi->lsi_lmd->lmd_fileset != NULL)
614                         OBD_FREE(lsi->lsi_lmd->lmd_fileset,
615                                 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
616                 if (lsi->lsi_lmd->lmd_mgssec != NULL)
617                         OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
618                                 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
619                 if (lsi->lsi_lmd->lmd_opts != NULL)
620                         OBD_FREE(lsi->lsi_lmd->lmd_opts,
621                                 strlen(lsi->lsi_lmd->lmd_opts) + 1);
622                 if (lsi->lsi_lmd->lmd_exclude_count)
623                         OBD_FREE(lsi->lsi_lmd->lmd_exclude,
624                                 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
625                                 lsi->lsi_lmd->lmd_exclude_count);
626                 if (lsi->lsi_lmd->lmd_mgs != NULL)
627                         OBD_FREE(lsi->lsi_lmd->lmd_mgs,
628                                  strlen(lsi->lsi_lmd->lmd_mgs) + 1);
629                 if (lsi->lsi_lmd->lmd_osd_type != NULL)
630                         OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
631                                  strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
632                 if (lsi->lsi_lmd->lmd_params != NULL)
633                         OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
634                 if (lsi->lsi_lmd->lmd_nidnet != NULL)
635                         OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
636                                 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
637
638                 OBD_FREE_PTR(lsi->lsi_lmd);
639         }
640
641         LASSERT(lsi->lsi_llsbi == NULL);
642         OBD_FREE_PTR(lsi);
643         s2lsi_nocast(sb) = NULL;
644
645         RETURN(0);
646 }
647
648 /* The lsi has one reference for every server that is using the disk -
649    e.g. MDT, MGS, and potentially MGC */
650 int lustre_put_lsi(struct super_block *sb)
651 {
652         struct lustre_sb_info *lsi = s2lsi(sb);
653         ENTRY;
654
655         LASSERT(lsi != NULL);
656
657         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
658         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
659                 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
660                         lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
661                         lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
662                         lsi->lsi_dt_dev = NULL;
663                         obd_disconnect(lsi->lsi_osd_exp);
664                         /* wait till OSD is gone */
665                         obd_zombie_barrier();
666                 }
667                 lustre_free_lsi(sb);
668                 RETURN(1);
669         }
670         RETURN(0);
671 }
672
673 /*** SERVER NAME ***
674  * <FSNAME><SEPARATOR><TYPE><INDEX>
675  * FSNAME is between 1 and 8 characters (inclusive).
676  *      Excluded characters are '/' and ':'
677  * SEPARATOR is either ':' or '-'
678  * TYPE: "OST", "MDT", etc.
679  * INDEX: Hex representation of the index
680  */
681
682 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
683  * @param [in] svname server name including type and index
684  * @param [out] fsname Buffer to copy filesystem name prefix into.
685  *  Must have at least 'strlen(fsname) + 1' chars.
686  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
687  * rc < 0  on error
688  */
689 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
690 {
691         const char *dash;
692
693         dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
694         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
695                 ;
696         if (dash == svname)
697                 return -EINVAL;
698
699         if (fsname != NULL) {
700                 strncpy(fsname, svname, dash - svname);
701                 fsname[dash - svname] = '\0';
702         }
703
704         if (endptr != NULL)
705                 *endptr = dash;
706
707         return 0;
708 }
709 EXPORT_SYMBOL(server_name2fsname);
710
711 /**
712  * Get service name (svname) from string
713  * rc < 0 on error
714  * if endptr isn't NULL it is set to end of fsname *
715  */
716 int server_name2svname(const char *label, char *svname, const char **endptr,
717                        size_t svsize)
718 {
719         int rc;
720         const char *dash;
721
722         /* We use server_name2fsname() just for parsing */
723         rc = server_name2fsname(label, NULL, &dash);
724         if (rc != 0)
725                 return rc;
726
727         if (endptr != NULL)
728                 *endptr = dash;
729
730         if (strlcpy(svname, dash + 1, svsize) >= svsize)
731                 return -E2BIG;
732
733         return 0;
734 }
735 EXPORT_SYMBOL(server_name2svname);
736
737 /**
738  * check server name is OST.
739  **/
740 int server_name_is_ost(const char *svname)
741 {
742         const char *dash;
743         int rc;
744
745         /* We use server_name2fsname() just for parsing */
746         rc = server_name2fsname(svname, NULL, &dash);
747         if (rc != 0)
748                 return rc;
749
750         dash++;
751
752         if (strncmp(dash, "OST", 3) == 0)
753                 return 1;
754         return 0;
755 }
756 EXPORT_SYMBOL(server_name_is_ost);
757
758 /**
759  * Get the index from the target name MDTXXXX/OSTXXXX
760  * rc = server type, or rc < 0  on error
761  **/
762 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
763 {
764         const char *dash = tgtname;
765         unsigned long index;
766         int rc;
767
768         if (strncmp(dash, "MDT", 3) == 0)
769                 rc = LDD_F_SV_TYPE_MDT;
770         else if (strncmp(dash, "OST", 3) == 0)
771                 rc = LDD_F_SV_TYPE_OST;
772         else
773                 return -EINVAL;
774
775         dash += 3;
776
777         if (strncmp(dash, "all", 3) == 0) {
778                 if (endptr != NULL)
779                         *endptr = dash + 3;
780                 return rc | LDD_F_SV_ALL;
781         }
782
783         index = simple_strtoul(dash, (char **)endptr, 16);
784         if (idx != NULL)
785                 *idx = index;
786         return rc;
787 }
788 EXPORT_SYMBOL(target_name2index);
789
790 /* Get the index from the obd name.
791    rc = server type, or
792    rc < 0  on error
793    if endptr isn't NULL it is set to end of name */
794 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
795 {
796         const char *dash;
797         int rc;
798
799         /* We use server_name2fsname() just for parsing */
800         rc = server_name2fsname(svname, NULL, &dash);
801         if (rc != 0)
802                 return rc;
803
804         dash++;
805         rc = target_name2index(dash, idx, endptr);
806         if (rc < 0)
807                 return rc;
808
809         /* Account for -mdc after index that is possible when specifying mdt */
810         if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
811                                       sizeof(LUSTRE_MDC_NAME)-1) == 0)
812                 *endptr += sizeof(LUSTRE_MDC_NAME);
813
814         return rc;
815 }
816 EXPORT_SYMBOL(server_name2index);
817
818 /*************** mount common betweeen server and client ***************/
819
820 /* Common umount */
821 int lustre_common_put_super(struct super_block *sb)
822 {
823         int rc;
824         ENTRY;
825
826         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
827
828         /* Drop a ref to the MGC */
829         rc = lustre_stop_mgc(sb);
830         if (rc && (rc != -ENOENT)) {
831                 if (rc != -EBUSY) {
832                         CERROR("Can't stop MGC: %d\n", rc);
833                         RETURN(rc);
834                 }
835                 /* BUSY just means that there's some other obd that
836                    needs the mgc.  Let him clean it up. */
837                 CDEBUG(D_MOUNT, "MGC still in use\n");
838         }
839         /* Drop a ref to the mounted disk */
840         lustre_put_lsi(sb);
841
842         RETURN(rc);
843 }
844 EXPORT_SYMBOL(lustre_common_put_super);
845
846 static void lmd_print(struct lustre_mount_data *lmd)
847 {
848         int i;
849
850         PRINT_CMD(D_MOUNT, "  mount data:\n");
851         if (lmd_is_client(lmd))
852                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
853         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
854         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
855
856         if (lmd->lmd_opts)
857                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
858
859         if (lmd->lmd_recovery_time_soft)
860                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
861                           lmd->lmd_recovery_time_soft);
862
863         if (lmd->lmd_recovery_time_hard)
864                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
865                           lmd->lmd_recovery_time_hard);
866
867         for (i = 0; i < lmd->lmd_exclude_count; i++) {
868                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
869                           lmd->lmd_exclude[i]);
870         }
871 }
872
873 /* Is this server on the exclusion list */
874 int lustre_check_exclusion(struct super_block *sb, char *svname)
875 {
876         struct lustre_sb_info *lsi = s2lsi(sb);
877         struct lustre_mount_data *lmd = lsi->lsi_lmd;
878         __u32 index;
879         int i, rc;
880         ENTRY;
881
882         rc = server_name2index(svname, &index, NULL);
883         if (rc != LDD_F_SV_TYPE_OST)
884                 /* Only exclude OSTs */
885                 RETURN(0);
886
887         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
888                index, lmd->lmd_exclude_count, lmd->lmd_dev);
889
890         for(i = 0; i < lmd->lmd_exclude_count; i++) {
891                 if (index == lmd->lmd_exclude[i]) {
892                         CWARN("Excluding %s (on exclusion list)\n", svname);
893                         RETURN(1);
894                 }
895         }
896         RETURN(0);
897 }
898
899 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
900 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
901 {
902         const char *s1 = ptr, *s2;
903         __u32 *exclude_list;
904         __u32 index = 0;
905         int rc = 0, devmax;
906         ENTRY;
907
908         /* The shortest an ost name can be is 8 chars: -OST0000.
909            We don't actually know the fsname at this time, so in fact
910            a user could specify any fsname. */
911         devmax = strlen(ptr) / 8 + 1;
912
913         /* temp storage until we figure out how many we have */
914         OBD_ALLOC(exclude_list, sizeof(index) * devmax);
915         if (!exclude_list)
916                 RETURN(-ENOMEM);
917
918         /* we enter this fn pointing at the '=' */
919         while (*s1 && *s1 != ' ' && *s1 != ',') {
920                 s1++;
921                 rc = server_name2index(s1, &index, &s2);
922                 if (rc < 0) {
923                         CERROR("Can't parse server name '%s': rc = %d\n",
924                                s1, rc);
925                         break;
926                 }
927                 if (rc == LDD_F_SV_TYPE_OST)
928                         exclude_list[lmd->lmd_exclude_count++] = index;
929                 else
930                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
931                                (uint)(s2-s1), s1, rc);
932                 s1 = s2;
933                 /* now we are pointing at ':' (next exclude)
934                    or ',' (end of excludes) */
935                 if (lmd->lmd_exclude_count >= devmax)
936                         break;
937         }
938         if (rc >= 0) /* non-err */
939                 rc = 0;
940
941         if (lmd->lmd_exclude_count) {
942                 /* permanent, freed in lustre_free_lsi */
943                 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
944                           lmd->lmd_exclude_count);
945                 if (lmd->lmd_exclude) {
946                         memcpy(lmd->lmd_exclude, exclude_list,
947                                sizeof(index) * lmd->lmd_exclude_count);
948                 } else {
949                         rc = -ENOMEM;
950                         lmd->lmd_exclude_count = 0;
951                 }
952         }
953         OBD_FREE(exclude_list, sizeof(index) * devmax);
954         RETURN(rc);
955 }
956
957 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
958 {
959         char   *tail;
960         int     length;
961
962         if (lmd->lmd_mgssec != NULL) {
963                 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
964                 lmd->lmd_mgssec = NULL;
965         }
966
967         tail = strchr(ptr, ',');
968         if (tail == NULL)
969                 length = strlen(ptr);
970         else
971                 length = tail - ptr;
972
973         OBD_ALLOC(lmd->lmd_mgssec, length + 1);
974         if (lmd->lmd_mgssec == NULL)
975                 return -ENOMEM;
976
977         memcpy(lmd->lmd_mgssec, ptr, length);
978         lmd->lmd_mgssec[length] = '\0';
979         return 0;
980 }
981
982 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
983 {
984         char   *tail;
985         int     length;
986
987         if (lmd->lmd_nidnet != NULL) {
988                 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
989                 lmd->lmd_nidnet = NULL;
990         }
991
992         tail = strchr(ptr, ',');
993         if (tail == NULL)
994                 length = strlen(ptr);
995         else
996                 length = tail - ptr;
997
998         OBD_ALLOC(lmd->lmd_nidnet, length + 1);
999         if (lmd->lmd_nidnet == NULL)
1000                 return -ENOMEM;
1001
1002         memcpy(lmd->lmd_nidnet, ptr, length);
1003         lmd->lmd_nidnet[length] = '\0';
1004         return 0;
1005 }
1006
1007 static int lmd_parse_string(char **handle, char *ptr)
1008 {
1009         char   *tail;
1010         int     length;
1011
1012         if ((handle == NULL) || (ptr == NULL))
1013                 return -EINVAL;
1014
1015         if (*handle != NULL) {
1016                 OBD_FREE(*handle, strlen(*handle) + 1);
1017                 *handle = NULL;
1018         }
1019
1020         tail = strchr(ptr, ',');
1021         if (tail == NULL)
1022                 length = strlen(ptr);
1023         else
1024                 length = tail - ptr;
1025
1026         OBD_ALLOC(*handle, length + 1);
1027         if (*handle == NULL)
1028                 return -ENOMEM;
1029
1030         memcpy(*handle, ptr, length);
1031         (*handle)[length] = '\0';
1032
1033         return 0;
1034 }
1035
1036 /* Collect multiple values for mgsnid specifiers */
1037 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1038 {
1039         lnet_nid_t nid;
1040         char *tail = *ptr;
1041         char *mgsnid;
1042         int   length;
1043         int   oldlen = 0;
1044
1045         /* Find end of nidlist */
1046         while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1047         length = tail - *ptr;
1048         if (length == 0) {
1049                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1050                 return -EINVAL;
1051         }
1052
1053         if (lmd->lmd_mgs != NULL)
1054                 oldlen = strlen(lmd->lmd_mgs) + 1;
1055
1056         OBD_ALLOC(mgsnid, oldlen + length + 1);
1057         if (mgsnid == NULL)
1058                 return -ENOMEM;
1059
1060         if (lmd->lmd_mgs != NULL) {
1061                 /* Multiple mgsnid= are taken to mean failover locations */
1062                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1063                 mgsnid[oldlen - 1] = ':';
1064                 OBD_FREE(lmd->lmd_mgs, oldlen);
1065         }
1066         memcpy(mgsnid + oldlen, *ptr, length);
1067         mgsnid[oldlen + length] = '\0';
1068         lmd->lmd_mgs = mgsnid;
1069         *ptr = tail;
1070
1071         return 0;
1072 }
1073
1074 /**
1075  * Find the first delimiter (comma or colon) from the specified \a buf and
1076  * make \a *endh point to the string starting with the delimiter. The commas
1077  * in expression list [...] will be skipped.
1078  *
1079  * \param[in] buf       a delimiter-separated string
1080  * \param[in] endh      a pointer to a pointer that will point to the string
1081  *                      starting with the delimiter
1082  *
1083  * \retval 0            if delimiter is found
1084  * \retval 1            if delimiter is not found
1085  */
1086 static int lmd_find_delimiter(char *buf, char **endh)
1087 {
1088         char *c = buf;
1089         int   skip = 0;
1090
1091         if (buf == NULL)
1092                 return 1;
1093
1094         while (*c != '\0') {
1095                 if (*c == '[')
1096                         skip++;
1097                 else if (*c == ']')
1098                         skip--;
1099
1100                 if ((*c == ',' || *c == ':') && skip == 0) {
1101                         if (endh != NULL)
1102                                 *endh = c;
1103                         return 0;
1104                 }
1105
1106                 c++;
1107         }
1108
1109         return 1;
1110 }
1111
1112 /**
1113  * Find the first valid string delimited by comma or colon from the specified
1114  * \a buf and parse it to see whether it's a valid nid list. If yes, \a *endh
1115  * will point to the next string starting with the delimiter.
1116  *
1117  * \param[in] buf       a delimiter-separated string
1118  * \param[in] endh      a pointer to a pointer that will point to the string
1119  *                      starting with the delimiter
1120  *
1121  * \retval 0            if the string is a valid nid list
1122  * \retval 1            if the string is not a valid nid list
1123  */
1124 static int lmd_parse_nidlist(char *buf, char **endh)
1125 {
1126         struct list_head nidlist;
1127         char            *endp = buf;
1128         char             tmp;
1129         int              rc = 0;
1130
1131         if (buf == NULL)
1132                 return 1;
1133         while (*buf == ',' || *buf == ':')
1134                 buf++;
1135         if (*buf == ' ' || *buf == '/' || *buf == '\0')
1136                 return 1;
1137
1138         if (lmd_find_delimiter(buf, &endp) != 0)
1139                 endp = buf + strlen(buf);
1140
1141         tmp = *endp;
1142         *endp = '\0';
1143
1144         INIT_LIST_HEAD(&nidlist);
1145         if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1146                 rc = 1;
1147         cfs_free_nidlist(&nidlist);
1148
1149         *endp = tmp;
1150         if (rc != 0)
1151                 return rc;
1152         if (endh != NULL)
1153                 *endh = endp;
1154         return 0;
1155 }
1156
1157 /** Parse mount line options
1158  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1159  * dev is passed as device=uml1:/lustre by mount.lustre
1160  */
1161 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1162 {
1163         char *s1, *s2, *devname = NULL;
1164         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1165         int rc = 0;
1166         ENTRY;
1167
1168         LASSERT(lmd);
1169         if (!options) {
1170                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1171                                    "/sbin/mount.lustre is installed.\n");
1172                 RETURN(-EINVAL);
1173         }
1174
1175         /* Options should be a string - try to detect old lmd data */
1176         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1177                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1178                                    "/sbin/mount.lustre.  Please install "
1179                                    "version %s\n", LUSTRE_VERSION_STRING);
1180                 RETURN(-EINVAL);
1181         }
1182         lmd->lmd_magic = LMD_MAGIC;
1183
1184         OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1185         if (lmd->lmd_params == NULL)
1186                 RETURN(-ENOMEM);
1187         lmd->lmd_params[0] = '\0';
1188
1189         /* Set default flags here */
1190
1191         s1 = options;
1192         while (*s1) {
1193                 int clear = 0;
1194                 int time_min = OBD_RECOVERY_TIME_MIN;
1195                 char *s3;
1196
1197                 /* Skip whitespace and extra commas */
1198                 while (*s1 == ' ' || *s1 == ',')
1199                         s1++;
1200                 s3 = s1;
1201
1202                 /* Client options are parsed in ll_options: eg. flock,
1203                    user_xattr, acl */
1204
1205                 /* Parse non-ldiskfs options here. Rather than modifying
1206                    ldiskfs, we just zero these out here */
1207                 if (strncmp(s1, "abort_recov", 11) == 0) {
1208                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1209                         clear++;
1210                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1211                         lmd->lmd_recovery_time_soft =
1212                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1213                                       time_min);
1214                         clear++;
1215                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1216                         lmd->lmd_recovery_time_hard =
1217                                 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1218                                       time_min);
1219                         clear++;
1220                 } else if (strncmp(s1, "noir", 4) == 0) {
1221                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1222                         clear++;
1223                 } else if (strncmp(s1, "nosvc", 5) == 0) {
1224                         lmd->lmd_flags |= LMD_FLG_NOSVC;
1225                         clear++;
1226                 } else if (strncmp(s1, "nomgs", 5) == 0) {
1227                         lmd->lmd_flags |= LMD_FLG_NOMGS;
1228                         clear++;
1229                 } else if (strncmp(s1, "noscrub", 7) == 0) {
1230                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1231                         clear++;
1232                 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1233                         lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1234                         clear++;
1235                 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1236                         lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1237                         clear++;
1238                 } else if (strncmp(s1, PARAM_MGSNODE,
1239                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
1240                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1241                         /* Assume the next mount opt is the first
1242                            invalid nid we get to. */
1243                         rc = lmd_parse_mgs(lmd, &s2);
1244                         if (rc)
1245                                 goto invalid;
1246                         s3 = s2;
1247                         clear++;
1248                 } else if (strncmp(s1, "writeconf", 9) == 0) {
1249                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
1250                         clear++;
1251                 } else if (strncmp(s1, "update", 6) == 0) {
1252                         lmd->lmd_flags |= LMD_FLG_UPDATE;
1253                         clear++;
1254                 } else if (strncmp(s1, "virgin", 6) == 0) {
1255                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
1256                         clear++;
1257                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1258                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1259                         clear++;
1260                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1261                         rc = lmd_parse_mgssec(lmd, s1 + 7);
1262                         if (rc)
1263                                 goto invalid;
1264                         clear++;
1265                         /* ost exclusion list */
1266                 } else if (strncmp(s1, "exclude=", 8) == 0) {
1267                         rc = lmd_make_exclusion(lmd, s1 + 7);
1268                         if (rc)
1269                                 goto invalid;
1270                         clear++;
1271                 } else if (strncmp(s1, "mgs", 3) == 0) {
1272                         /* We are an MGS */
1273                         lmd->lmd_flags |= LMD_FLG_MGS;
1274                         clear++;
1275                 } else if (strncmp(s1, "svname=", 7) == 0) {
1276                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1277                         if (rc)
1278                                 goto invalid;
1279                         clear++;
1280                 } else if (strncmp(s1, "param=", 6) == 0) {
1281                         size_t length, params_length;
1282                         char  *tail = s1;
1283                         if (lmd_find_delimiter(s1 + 6, &tail) != 0)
1284                                 length = strlen(s1);
1285                         else {
1286                                 char *param_str = tail + 1;
1287                                 int   supplementary = 1;
1288                                 while (lmd_parse_nidlist(param_str,
1289                                                          &param_str) == 0) {
1290                                         supplementary = 0;
1291                                 }
1292                                 length = param_str - s1 - supplementary;
1293                         }
1294                         length -= 6;
1295                         params_length = strlen(lmd->lmd_params);
1296                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1297                                 RETURN(-E2BIG);
1298                         strncat(lmd->lmd_params, s1 + 6, length);
1299                         lmd->lmd_params[params_length + length] = '\0';
1300                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1301                         s3 = s1 + 6 + length;
1302                         clear++;
1303                 } else if (strncmp(s1, "osd=", 4) == 0) {
1304                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1305                         if (rc)
1306                                 goto invalid;
1307                         clear++;
1308                 }
1309                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1310                    end of the options. */
1311                 else if (strncmp(s1, "device=", 7) == 0) {
1312                         devname = s1 + 7;
1313                         /* terminate options right before device.  device
1314                            must be the last one. */
1315                         *s1 = '\0';
1316                         break;
1317                 } else if (strncmp(s1, "network=", 8) == 0) {
1318                         rc = lmd_parse_network(lmd, s1 + 8);
1319                         if (rc)
1320                                 goto invalid;
1321                         clear++;
1322                 }
1323
1324                 /* Find next opt */
1325                 s2 = strchr(s3, ',');
1326                 if (s2 == NULL) {
1327                         if (clear)
1328                                 *s1 = '\0';
1329                         break;
1330                 }
1331                 s2++;
1332                 if (clear)
1333                         memmove(s1, s2, strlen(s2) + 1);
1334                 else
1335                         s1 = s2;
1336         }
1337
1338         if (!devname) {
1339                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1340                                    "(need mount option 'device=...')\n");
1341                 goto invalid;
1342         }
1343
1344         s1 = strstr(devname, ":/");
1345         if (s1) {
1346                 ++s1;
1347                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1348                 /* Remove leading /s from fsname */
1349                 while (*++s1 == '/')
1350                         ;
1351                 s2 = s1;
1352                 while (*s2 != '/' && *s2 != '\0')
1353                         s2++;
1354                 /* Freed in lustre_free_lsi */
1355                 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1356                 if (!lmd->lmd_profile)
1357                         RETURN(-ENOMEM);
1358
1359                 strncat(lmd->lmd_profile, s1, s2 - s1);
1360                 strncat(lmd->lmd_profile, "-client", 7);
1361
1362                 s1 = s2;
1363                 s2 = s1 + strlen(s1) - 1;
1364                 /* Remove padding /s from fileset */
1365                 while (*s2 == '/')
1366                         s2--;
1367                 if (s2 > s1) {
1368                         OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1369                         if (lmd->lmd_fileset == NULL) {
1370                                 OBD_FREE(lmd->lmd_profile,
1371                                          strlen(lmd->lmd_profile) + 1);
1372                                 RETURN(-ENOMEM);
1373                         }
1374                         strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1375                 }
1376         } else {
1377                 /* server mount */
1378                 if (lmd->lmd_nidnet != NULL) {
1379                         /* 'network=' mount option forbidden for server */
1380                         OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1381                         lmd->lmd_nidnet = NULL;
1382                         rc = -EINVAL;
1383                         CERROR("%s: option 'network=' not allowed for Lustre "
1384                                "servers: rc = %d\n", devname, rc);
1385                         RETURN(rc);
1386                 }
1387         }
1388
1389         /* Freed in lustre_free_lsi */
1390         OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1391         if (!lmd->lmd_dev)
1392                 RETURN(-ENOMEM);
1393         strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1394
1395         /* Save mount options */
1396         s1 = options + strlen(options) - 1;
1397         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1398                 *s1-- = 0;
1399         if (*options != 0) {
1400                 /* Freed in lustre_free_lsi */
1401                 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1402                 if (!lmd->lmd_opts)
1403                         RETURN(-ENOMEM);
1404                 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1405         }
1406
1407         lmd_print(lmd);
1408         lmd->lmd_magic = LMD_MAGIC;
1409
1410         RETURN(rc);
1411
1412 invalid:
1413         CERROR("Bad mount options %s\n", options);
1414         RETURN(-EINVAL);
1415 }
1416
1417 struct lustre_mount_data2 {
1418         void *lmd2_data;
1419         struct vfsmount *lmd2_mnt;
1420 };
1421
1422 /** This is the entry point for the mount call into Lustre.
1423  * This is called when a server or client is mounted,
1424  * and this is where we start setting things up.
1425  * @param data Mount options (e.g. -o flock,abort_recov)
1426  */
1427 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1428 {
1429         struct lustre_mount_data *lmd;
1430         struct lustre_mount_data2 *lmd2 = data;
1431         struct lustre_sb_info *lsi;
1432         int rc;
1433         ENTRY;
1434
1435         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1436
1437         lsi = lustre_init_lsi(sb);
1438         if (!lsi)
1439                 RETURN(-ENOMEM);
1440         lmd = lsi->lsi_lmd;
1441
1442         /*
1443          * Disable lockdep during mount, because mount locking patterns are
1444          * `special'.
1445          */
1446         lockdep_off();
1447
1448         /*
1449          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1450          */
1451         obd_zombie_barrier();
1452
1453         /* Figure out the lmd from the mount options */
1454         if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1455                 lustre_put_lsi(sb);
1456                 GOTO(out, rc = -EINVAL);
1457         }
1458
1459         if (lmd_is_client(lmd)) {
1460                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1461                 if (client_fill_super == NULL)
1462                         request_module("lustre");
1463                 if (client_fill_super == NULL) {
1464                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1465                                            "client mount! Is the 'lustre' "
1466                                            "module loaded?\n");
1467                         lustre_put_lsi(sb);
1468                         rc = -ENODEV;
1469                 } else {
1470                         rc = lustre_start_mgc(sb);
1471                         if (rc) {
1472                                 lustre_common_put_super(sb);
1473                                 GOTO(out, rc);
1474                         }
1475                         /* Connect and start */
1476                         /* (should always be ll_fill_super) */
1477                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1478                         /* c_f_s will call lustre_common_put_super on failure */
1479                 }
1480         } else {
1481 #ifdef HAVE_SERVER_SUPPORT
1482                 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1483                 rc = server_fill_super(sb);
1484                 /* s_f_s calls lustre_start_mgc after the mount because we need
1485                    the MGS nids which are stored on disk.  Plus, we may
1486                    need to start the MGS first. */
1487                 /* s_f_s will call server_put_super on failure */
1488 #else
1489                 CERROR("This is client-side-only module, "
1490                        "cannot handle server mount.\n");
1491                 rc = -EINVAL;
1492 #endif
1493         }
1494
1495         /* If error happens in fill_super() call, @lsi will be killed there.
1496          * This is why we do not put it here. */
1497         GOTO(out, rc);
1498 out:
1499         if (rc) {
1500                 CERROR("Unable to mount %s (%d)\n",
1501                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1502         } else {
1503                 CDEBUG(D_SUPER, "Mount %s complete\n",
1504                        lmd->lmd_dev);
1505         }
1506         lockdep_on();
1507         return rc;
1508 }
1509
1510
1511 /* We can't call ll_fill_super by name because it lives in a module that
1512    must be loaded after this one. */
1513 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1514                                                   struct vfsmount *mnt))
1515 {
1516         client_fill_super = cfs;
1517 }
1518 EXPORT_SYMBOL(lustre_register_client_fill_super);
1519
1520 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1521 {
1522         kill_super_cb = cfs;
1523 }
1524 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1525
1526 /***************** FS registration ******************/
1527 #ifdef HAVE_FSTYPE_MOUNT
1528 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1529                                    const char *devname, void *data)
1530 {
1531         struct lustre_mount_data2 lmd2 = {
1532                 .lmd2_data = data,
1533         };
1534
1535         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1536 }
1537 #else
1538 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1539                          const char *devname, void *data, struct vfsmount *mnt)
1540 {
1541         struct lustre_mount_data2 lmd2 = {
1542                 .lmd2_data = data,
1543                 .lmd2_mnt = mnt,
1544         };
1545
1546         return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1547 }
1548 #endif
1549
1550 static void lustre_kill_super(struct super_block *sb)
1551 {
1552         struct lustre_sb_info *lsi = s2lsi(sb);
1553
1554         if (kill_super_cb && lsi && !IS_SERVER(lsi))
1555                 (*kill_super_cb)(sb);
1556
1557         kill_anon_super(sb);
1558 }
1559
1560 /** Register the "lustre" fs type
1561  */
1562 static struct file_system_type lustre_fs_type = {
1563         .owner        = THIS_MODULE,
1564         .name         = "lustre",
1565 #ifdef HAVE_FSTYPE_MOUNT
1566         .mount        = lustre_mount,
1567 #else
1568         .get_sb       = lustre_get_sb,
1569 #endif
1570         .kill_sb      = lustre_kill_super,
1571         .fs_flags     = FS_REQUIRES_DEV | FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1572 };
1573 MODULE_ALIAS_FS("lustre");
1574
1575 int lustre_register_fs(void)
1576 {
1577         return register_filesystem(&lustre_fs_type);
1578 }
1579
1580 int lustre_unregister_fs(void)
1581 {
1582         return unregister_filesystem(&lustre_fs_type);
1583 }