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