GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, 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 #define DEBUG_SUBSYSTEM S_CLASS
40 #define D_MOUNT (D_SUPER | D_CONFIG/*|D_WARNING */)
41 #define PRINT_CMD CDEBUG
42
43 #include "../include/obd.h"
44 #include "../include/lustre_compat.h"
45 #include "../include/obd_class.h"
46 #include "../include/lustre/lustre_user.h"
47 #include "../include/lustre_log.h"
48 #include "../include/lustre_disk.h"
49 #include "../include/lustre_param.h"
50
51 static int (*client_fill_super)(struct super_block *sb,
52                                 struct vfsmount *mnt);
53
54 static void (*kill_super_cb)(struct super_block *sb);
55
56 /**************** config llog ********************/
57
58 /** Get a config log from the MGS and process it.
59  * This func is called for both clients and servers.
60  * Continue to process new statements appended to the logs
61  * (whenever the config lock is revoked) until lustre_end_log
62  * is called.
63  * @param sb The superblock is used by the MGC to write to the local copy of
64  *   the config log
65  * @param logname The name of the llog to replicate from the MGS
66  * @param cfg Since the same mgc may be used to follow multiple config logs
67  *   (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
68  *   this log, and is added to the mgc's list of logs to follow.
69  */
70 int lustre_process_log(struct super_block *sb, char *logname,
71                        struct config_llog_instance *cfg)
72 {
73         struct lustre_cfg *lcfg;
74         struct lustre_cfg_bufs *bufs;
75         struct lustre_sb_info *lsi = s2lsi(sb);
76         struct obd_device *mgc = lsi->lsi_mgc;
77         int rc;
78
79         LASSERT(mgc);
80         LASSERT(cfg);
81
82         bufs = kzalloc(sizeof(*bufs), GFP_NOFS);
83         if (!bufs)
84                 return -ENOMEM;
85
86         /* mgc_process_config */
87         lustre_cfg_bufs_reset(bufs, mgc->obd_name);
88         lustre_cfg_bufs_set_string(bufs, 1, logname);
89         lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
90         lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
91         lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
92         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
93         lustre_cfg_free(lcfg);
94
95         kfree(bufs);
96
97         if (rc == -EINVAL)
98                 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s' failed from the MGS (%d).  Make sure this client and the MGS are running compatible versions of Lustre.\n",
99                                    mgc->obd_name, logname, rc);
100
101         else if (rc)
102                 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' failed (%d). This may be the result of communication errors between this node and the MGS, a bad configuration, or other errors. See the syslog for more information.\n",
103                                    mgc->obd_name, logname,
104                                    rc);
105
106         /* class_obd_list(); */
107         return rc;
108 }
109 EXPORT_SYMBOL(lustre_process_log);
110
111 /* Stop watching this config log for updates */
112 int lustre_end_log(struct super_block *sb, char *logname,
113                    struct config_llog_instance *cfg)
114 {
115         struct lustre_cfg *lcfg;
116         struct lustre_cfg_bufs bufs;
117         struct lustre_sb_info *lsi = s2lsi(sb);
118         struct obd_device *mgc = lsi->lsi_mgc;
119         int rc;
120
121         if (!mgc)
122                 return -ENOENT;
123
124         /* mgc_process_config */
125         lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
126         lustre_cfg_bufs_set_string(&bufs, 1, logname);
127         if (cfg)
128                 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
129         lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
130         rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
131         lustre_cfg_free(lcfg);
132         return rc;
133 }
134 EXPORT_SYMBOL(lustre_end_log);
135
136 /**************** obd start *******************/
137
138 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
139  * lctl (and do for echo cli/srv.
140  */
141 static int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
142                    char *s1, char *s2, char *s3, char *s4)
143 {
144         struct lustre_cfg_bufs bufs;
145         struct lustre_cfg     *lcfg = NULL;
146         int rc;
147
148         CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
149                cmd, s1, s2, s3, s4);
150
151         lustre_cfg_bufs_reset(&bufs, cfgname);
152         if (s1)
153                 lustre_cfg_bufs_set_string(&bufs, 1, s1);
154         if (s2)
155                 lustre_cfg_bufs_set_string(&bufs, 2, s2);
156         if (s3)
157                 lustre_cfg_bufs_set_string(&bufs, 3, s3);
158         if (s4)
159                 lustre_cfg_bufs_set_string(&bufs, 4, s4);
160
161         lcfg = lustre_cfg_new(cmd, &bufs);
162         lcfg->lcfg_nid = nid;
163         rc = class_process_config(lcfg);
164         lustre_cfg_free(lcfg);
165         return rc;
166 }
167
168 /** Call class_attach and class_setup.  These methods in turn call
169  * obd type-specific methods.
170  */
171 static int lustre_start_simple(char *obdname, char *type, char *uuid,
172                                char *s1, char *s2, char *s3, char *s4)
173 {
174         int rc;
175
176         CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
177
178         rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
179         if (rc) {
180                 CERROR("%s attach error %d\n", obdname, rc);
181                 return rc;
182         }
183         rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
184         if (rc) {
185                 CERROR("%s setup error %d\n", obdname, rc);
186                 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
187         }
188         return rc;
189 }
190
191 static DEFINE_MUTEX(mgc_start_lock);
192
193 /** Set up a mgc obd to process startup logs
194  *
195  * \param sb [in] super block of the mgc obd
196  *
197  * \retval 0 success, otherwise error code
198  */
199 int lustre_start_mgc(struct super_block *sb)
200 {
201         struct obd_connect_data *data = NULL;
202         struct lustre_sb_info *lsi = s2lsi(sb);
203         struct obd_device *obd;
204         struct obd_export *exp;
205         struct obd_uuid *uuid;
206         class_uuid_t uuidc;
207         lnet_nid_t nid;
208         char nidstr[LNET_NIDSTR_SIZE];
209         char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
210         char *ptr;
211         int rc = 0, i = 0, j;
212
213         LASSERT(lsi->lsi_lmd);
214
215         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
216         ptr = lsi->lsi_lmd->lmd_dev;
217         if (class_parse_nid(ptr, &nid, &ptr) == 0)
218                 i++;
219         if (i == 0) {
220                 CERROR("No valid MGS nids found.\n");
221                 return -EINVAL;
222         }
223
224         mutex_lock(&mgc_start_lock);
225
226         libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
227         mgcname = kasprintf(GFP_NOFS,
228                             "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
229         niduuid = kasprintf(GFP_NOFS, "%s_%x", mgcname, i);
230         if (!mgcname || !niduuid) {
231                 rc = -ENOMEM;
232                 goto out_free;
233         }
234
235         mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
236
237         data = kzalloc(sizeof(*data), GFP_NOFS);
238         if (!data) {
239                 rc = -ENOMEM;
240                 goto out_free;
241         }
242
243         obd = class_name2obd(mgcname);
244         if (obd && !obd->obd_stopping) {
245                 int recov_bk;
246
247                 rc = obd_set_info_async(NULL, obd->obd_self_export,
248                                         strlen(KEY_MGSSEC), KEY_MGSSEC,
249                                         strlen(mgssec), mgssec, NULL);
250                 if (rc)
251                         goto out_free;
252
253                 /* Re-using an existing MGC */
254                 atomic_inc(&obd->u.cli.cl_mgc_refcount);
255
256                 /* IR compatibility check, only for clients */
257                 if (lmd_is_client(lsi->lsi_lmd)) {
258                         int has_ir;
259                         int vallen = sizeof(*data);
260                         __u32 *flags = &lsi->lsi_lmd->lmd_flags;
261
262                         rc = obd_get_info(NULL, obd->obd_self_export,
263                                           strlen(KEY_CONN_DATA), KEY_CONN_DATA,
264                                           &vallen, data, NULL);
265                         LASSERT(rc == 0);
266                         has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
267                         if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
268                                 /* LMD_FLG_NOIR is for test purpose only */
269                                 LCONSOLE_WARN(
270                                         "Trying to mount a client with IR setting not compatible with current mgc. Force to use current mgc setting that is IR %s.\n",
271                                         has_ir ? "enabled" : "disabled");
272                                 if (has_ir)
273                                         *flags &= ~LMD_FLG_NOIR;
274                                 else
275                                         *flags |= LMD_FLG_NOIR;
276                         }
277                 }
278
279                 recov_bk = 0;
280
281                 /* Try all connections, but only once (again).
282                  * We don't want to block another target from starting
283                  * (using its local copy of the log), but we do want to connect
284                  * if at all possible.
285                  */
286                 recov_bk++;
287                 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,
288                        recov_bk);
289                 rc = obd_set_info_async(NULL, obd->obd_self_export,
290                                         sizeof(KEY_INIT_RECOV_BACKUP),
291                                         KEY_INIT_RECOV_BACKUP,
292                                         sizeof(recov_bk), &recov_bk, NULL);
293                 rc = 0;
294                 goto out;
295         }
296
297         CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
298
299         /* Add the primary nids for the MGS */
300         i = 0;
301         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
302         ptr = lsi->lsi_lmd->lmd_dev;
303         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
304                 rc = do_lcfg(mgcname, nid,
305                              LCFG_ADD_UUID, niduuid, NULL, NULL, NULL);
306                 if (!rc)
307                         i++;
308                 /* Stop at the first failover nid */
309                 if (*ptr == ':')
310                         break;
311         }
312         if (i == 0) {
313                 CERROR("No valid MGS nids found.\n");
314                 rc = -EINVAL;
315                 goto out_free;
316         }
317         lsi->lsi_lmd->lmd_mgs_failnodes = 1;
318
319         /* Random uuid for MGC allows easier reconnects */
320         uuid = kzalloc(sizeof(*uuid), GFP_NOFS);
321         if (!uuid) {
322                 rc = -ENOMEM;
323                 goto out_free;
324         }
325
326         ll_generate_random_uuid(uuidc);
327         class_uuid_unparse(uuidc, uuid);
328
329         /* Start the MGC */
330         rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
331                                  (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
332                                  niduuid, NULL, NULL);
333         kfree(uuid);
334         if (rc)
335                 goto out_free;
336
337         /* Add any failover MGS nids */
338         i = 1;
339         while (ptr && ((*ptr == ':' ||
340                         class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
341                 /* New failover node */
342                 sprintf(niduuid, "%s_%x", mgcname, i);
343                 j = 0;
344                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
345                         rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID, niduuid,
346                                      NULL, NULL, NULL);
347                         if (!rc)
348                                 ++j;
349                         if (*ptr == ':')
350                                 break;
351                 }
352                 if (j > 0) {
353                         rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
354                                      niduuid, NULL, NULL, NULL);
355                         if (!rc)
356                                 i++;
357                 } else {
358                         /* at ":/fsname" */
359                         break;
360                 }
361         }
362         lsi->lsi_lmd->lmd_mgs_failnodes = i;
363
364         obd = class_name2obd(mgcname);
365         if (!obd) {
366                 CERROR("Can't find mgcobd %s\n", mgcname);
367                 rc = -ENOTCONN;
368                 goto out_free;
369         }
370
371         rc = obd_set_info_async(NULL, obd->obd_self_export,
372                                 strlen(KEY_MGSSEC), KEY_MGSSEC,
373                                 strlen(mgssec), mgssec, NULL);
374         if (rc)
375                 goto out_free;
376
377         /* Keep a refcount of servers/clients who started with "mount",
378          * so we know when we can get rid of the mgc.
379          */
380         atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
381
382         /* We connect to the MGS at setup, and don't disconnect until cleanup */
383         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
384                                   OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
385                                   OBD_CONNECT_LVB_TYPE;
386
387 #if OBD_OCD_VERSION(3, 0, 53, 0) > LUSTRE_VERSION_CODE
388         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
389 #endif
390
391         if (lmd_is_client(lsi->lsi_lmd) &&
392             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
393                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
394         data->ocd_version = LUSTRE_VERSION_CODE;
395         rc = obd_connect(NULL, &exp, obd, &obd->obd_uuid, data, NULL);
396         if (rc) {
397                 CERROR("connect failed %d\n", rc);
398                 goto out;
399         }
400
401         obd->u.cli.cl_mgc_mgsexp = exp;
402
403 out:
404         /* Keep the mgc info in the sb. Note that many lsi's can point
405          * to the same mgc.
406          */
407         lsi->lsi_mgc = obd;
408 out_free:
409         mutex_unlock(&mgc_start_lock);
410
411         kfree(data);
412         kfree(mgcname);
413         kfree(niduuid);
414         return rc;
415 }
416
417 static int lustre_stop_mgc(struct super_block *sb)
418 {
419         struct lustre_sb_info *lsi = s2lsi(sb);
420         struct obd_device *obd;
421         char *niduuid = NULL, *ptr = NULL;
422         int i, rc = 0, len = 0;
423
424         if (!lsi)
425                 return -ENOENT;
426         obd = lsi->lsi_mgc;
427         if (!obd)
428                 return -ENOENT;
429         lsi->lsi_mgc = NULL;
430
431         mutex_lock(&mgc_start_lock);
432         LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
433         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
434                 /* This is not fatal, every client that stops
435                  * will call in here.
436                  */
437                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
438                        atomic_read(&obd->u.cli.cl_mgc_refcount));
439                 rc = -EBUSY;
440                 goto out;
441         }
442
443         /* The MGC has no recoverable data in any case.
444          * force shutdown set in umount_begin
445          */
446         obd->obd_no_recov = 1;
447
448         if (obd->u.cli.cl_mgc_mgsexp) {
449                 /* An error is not fatal, if we are unable to send the
450                  * disconnect mgs ping evictor cleans up the export
451                  */
452                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
453                 if (rc)
454                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
455         }
456
457         /* Save the obdname for cleaning the nid uuids, which are obdname_XX */
458         len = strlen(obd->obd_name) + 6;
459         niduuid = kzalloc(len, GFP_NOFS);
460         if (niduuid) {
461                 strcpy(niduuid, obd->obd_name);
462                 ptr = niduuid + strlen(niduuid);
463         }
464
465         rc = class_manual_cleanup(obd);
466         if (rc)
467                 goto out;
468
469         /* Clean the nid uuids */
470         if (!niduuid) {
471                 rc = -ENOMEM;
472                 goto out;
473         }
474
475         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
476                 sprintf(ptr, "_%x", i);
477                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
478                              niduuid, NULL, NULL, NULL);
479                 if (rc)
480                         CERROR("del MDC UUID %s failed: rc = %d\n",
481                                niduuid, rc);
482         }
483 out:
484         kfree(niduuid);
485
486         /* class_import_put will get rid of the additional connections */
487         mutex_unlock(&mgc_start_lock);
488         return rc;
489 }
490
491 /***************** lustre superblock **************/
492
493 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
494 {
495         struct lustre_sb_info *lsi;
496
497         lsi = kzalloc(sizeof(*lsi), GFP_NOFS);
498         if (!lsi)
499                 return NULL;
500         lsi->lsi_lmd = kzalloc(sizeof(*lsi->lsi_lmd), GFP_NOFS);
501         if (!lsi->lsi_lmd) {
502                 kfree(lsi);
503                 return NULL;
504         }
505
506         lsi->lsi_lmd->lmd_exclude_count = 0;
507         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
508         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
509         s2lsi_nocast(sb) = lsi;
510         /* we take 1 extra ref for our setup */
511         atomic_set(&lsi->lsi_mounts, 1);
512
513         /* Default umount style */
514         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
515
516         return lsi;
517 }
518
519 static int lustre_free_lsi(struct super_block *sb)
520 {
521         struct lustre_sb_info *lsi = s2lsi(sb);
522
523         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
524
525         /* someone didn't call server_put_mount. */
526         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
527
528         if (lsi->lsi_lmd) {
529                 kfree(lsi->lsi_lmd->lmd_dev);
530                 kfree(lsi->lsi_lmd->lmd_profile);
531                 kfree(lsi->lsi_lmd->lmd_mgssec);
532                 kfree(lsi->lsi_lmd->lmd_opts);
533                 if (lsi->lsi_lmd->lmd_exclude_count)
534                         kfree(lsi->lsi_lmd->lmd_exclude);
535                 kfree(lsi->lsi_lmd->lmd_mgs);
536                 kfree(lsi->lsi_lmd->lmd_osd_type);
537                 kfree(lsi->lsi_lmd->lmd_params);
538
539                 kfree(lsi->lsi_lmd);
540         }
541
542         LASSERT(!lsi->lsi_llsbi);
543         kfree(lsi);
544         s2lsi_nocast(sb) = NULL;
545
546         return 0;
547 }
548
549 /* The lsi has one reference for every server that is using the disk -
550  * e.g. MDT, MGS, and potentially MGC
551  */
552 static int lustre_put_lsi(struct super_block *sb)
553 {
554         struct lustre_sb_info *lsi = s2lsi(sb);
555
556         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
557         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
558                 lustre_free_lsi(sb);
559                 return 1;
560         }
561         return 0;
562 }
563
564 /*** SERVER NAME ***
565  * <FSNAME><SEPARATOR><TYPE><INDEX>
566  * FSNAME is between 1 and 8 characters (inclusive).
567  *      Excluded characters are '/' and ':'
568  * SEPARATOR is either ':' or '-'
569  * TYPE: "OST", "MDT", etc.
570  * INDEX: Hex representation of the index
571  */
572
573 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
574  * @param [in] svname server name including type and index
575  * @param [out] fsname Buffer to copy filesystem name prefix into.
576  *  Must have at least 'strlen(fsname) + 1' chars.
577  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
578  * rc < 0  on error
579  */
580 static int server_name2fsname(const char *svname, char *fsname,
581                               const char **endptr)
582 {
583         const char *dash;
584
585         dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
586         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
587                 ;
588         if (dash == svname)
589                 return -EINVAL;
590
591         if (fsname) {
592                 strncpy(fsname, svname, dash - svname);
593                 fsname[dash - svname] = '\0';
594         }
595
596         if (endptr)
597                 *endptr = dash;
598
599         return 0;
600 }
601
602 /* Get the index from the obd name.
603  *  rc = server type, or
604  * rc < 0  on error
605  * if endptr isn't NULL it is set to end of name
606  */
607 static int server_name2index(const char *svname, __u32 *idx,
608                              const char **endptr)
609 {
610         unsigned long index;
611         int rc;
612         const char *dash;
613
614         /* We use server_name2fsname() just for parsing */
615         rc = server_name2fsname(svname, NULL, &dash);
616         if (rc != 0)
617                 return rc;
618
619         dash++;
620
621         if (strncmp(dash, "MDT", 3) == 0)
622                 rc = LDD_F_SV_TYPE_MDT;
623         else if (strncmp(dash, "OST", 3) == 0)
624                 rc = LDD_F_SV_TYPE_OST;
625         else
626                 return -EINVAL;
627
628         dash += 3;
629
630         if (strncmp(dash, "all", 3) == 0) {
631                 if (endptr)
632                         *endptr = dash + 3;
633                 return rc | LDD_F_SV_ALL;
634         }
635
636         index = simple_strtoul(dash, (char **)endptr, 16);
637         if (idx)
638                 *idx = index;
639
640         /* Account for -mdc after index that is possible when specifying mdt */
641         if (endptr && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
642                               sizeof(LUSTRE_MDC_NAME) - 1) == 0)
643                 *endptr += sizeof(LUSTRE_MDC_NAME);
644
645         return rc;
646 }
647
648 /*************** mount common between server and client ***************/
649
650 /* Common umount */
651 int lustre_common_put_super(struct super_block *sb)
652 {
653         int rc;
654
655         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
656
657         /* Drop a ref to the MGC */
658         rc = lustre_stop_mgc(sb);
659         if (rc && (rc != -ENOENT)) {
660                 if (rc != -EBUSY) {
661                         CERROR("Can't stop MGC: %d\n", rc);
662                         return rc;
663                 }
664                 /* BUSY just means that there's some other obd that
665                  * needs the mgc.  Let him clean it up.
666                  */
667                 CDEBUG(D_MOUNT, "MGC still in use\n");
668         }
669         /* Drop a ref to the mounted disk */
670         lustre_put_lsi(sb);
671         return rc;
672 }
673 EXPORT_SYMBOL(lustre_common_put_super);
674
675 static void lmd_print(struct lustre_mount_data *lmd)
676 {
677         int i;
678
679         PRINT_CMD(D_MOUNT, "  mount data:\n");
680         if (lmd_is_client(lmd))
681                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
682         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
683         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
684
685         if (lmd->lmd_opts)
686                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
687
688         if (lmd->lmd_recovery_time_soft)
689                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
690                           lmd->lmd_recovery_time_soft);
691
692         if (lmd->lmd_recovery_time_hard)
693                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
694                           lmd->lmd_recovery_time_hard);
695
696         for (i = 0; i < lmd->lmd_exclude_count; i++) {
697                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
698                           lmd->lmd_exclude[i]);
699         }
700 }
701
702 /* Is this server on the exclusion list */
703 int lustre_check_exclusion(struct super_block *sb, char *svname)
704 {
705         struct lustre_sb_info *lsi = s2lsi(sb);
706         struct lustre_mount_data *lmd = lsi->lsi_lmd;
707         __u32 index;
708         int i, rc;
709
710         rc = server_name2index(svname, &index, NULL);
711         if (rc != LDD_F_SV_TYPE_OST)
712                 /* Only exclude OSTs */
713                 return 0;
714
715         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
716                index, lmd->lmd_exclude_count, lmd->lmd_dev);
717
718         for (i = 0; i < lmd->lmd_exclude_count; i++) {
719                 if (index == lmd->lmd_exclude[i]) {
720                         CWARN("Excluding %s (on exclusion list)\n", svname);
721                         return 1;
722                 }
723         }
724         return 0;
725 }
726
727 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
728 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
729 {
730         const char *s1 = ptr, *s2;
731         __u32 index = 0, *exclude_list;
732         int rc = 0, devmax;
733
734         /* The shortest an ost name can be is 8 chars: -OST0000.
735          * We don't actually know the fsname at this time, so in fact
736          * a user could specify any fsname.
737          */
738         devmax = strlen(ptr) / 8 + 1;
739
740         /* temp storage until we figure out how many we have */
741         exclude_list = kcalloc(devmax, sizeof(index), GFP_NOFS);
742         if (!exclude_list)
743                 return -ENOMEM;
744
745         /* we enter this fn pointing at the '=' */
746         while (*s1 && *s1 != ' ' && *s1 != ',') {
747                 s1++;
748                 rc = server_name2index(s1, &index, &s2);
749                 if (rc < 0) {
750                         CERROR("Can't parse server name '%s': rc = %d\n",
751                                s1, rc);
752                         break;
753                 }
754                 if (rc == LDD_F_SV_TYPE_OST)
755                         exclude_list[lmd->lmd_exclude_count++] = index;
756                 else
757                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
758                                (uint)(s2 - s1), s1, rc);
759                 s1 = s2;
760                 /* now we are pointing at ':' (next exclude)
761                  * or ',' (end of excludes)
762                  */
763                 if (lmd->lmd_exclude_count >= devmax)
764                         break;
765         }
766         if (rc >= 0) /* non-err */
767                 rc = 0;
768
769         if (lmd->lmd_exclude_count) {
770                 /* permanent, freed in lustre_free_lsi */
771                 lmd->lmd_exclude = kcalloc(lmd->lmd_exclude_count,
772                                            sizeof(index), GFP_NOFS);
773                 if (lmd->lmd_exclude) {
774                         memcpy(lmd->lmd_exclude, exclude_list,
775                                sizeof(index) * lmd->lmd_exclude_count);
776                 } else {
777                         rc = -ENOMEM;
778                         lmd->lmd_exclude_count = 0;
779                 }
780         }
781         kfree(exclude_list);
782         return rc;
783 }
784
785 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
786 {
787         char   *tail;
788         int     length;
789
790         kfree(lmd->lmd_mgssec);
791         lmd->lmd_mgssec = NULL;
792
793         tail = strchr(ptr, ',');
794         if (!tail)
795                 length = strlen(ptr);
796         else
797                 length = tail - ptr;
798
799         lmd->lmd_mgssec = kzalloc(length + 1, GFP_NOFS);
800         if (!lmd->lmd_mgssec)
801                 return -ENOMEM;
802
803         memcpy(lmd->lmd_mgssec, ptr, length);
804         lmd->lmd_mgssec[length] = '\0';
805         return 0;
806 }
807
808 static int lmd_parse_string(char **handle, char *ptr)
809 {
810         char   *tail;
811         int     length;
812
813         if (!handle || !ptr)
814                 return -EINVAL;
815
816         kfree(*handle);
817         *handle = NULL;
818
819         tail = strchr(ptr, ',');
820         if (!tail)
821                 length = strlen(ptr);
822         else
823                 length = tail - ptr;
824
825         *handle = kzalloc(length + 1, GFP_NOFS);
826         if (!*handle)
827                 return -ENOMEM;
828
829         memcpy(*handle, ptr, length);
830         (*handle)[length] = '\0';
831
832         return 0;
833 }
834
835 /* Collect multiple values for mgsnid specifiers */
836 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
837 {
838         lnet_nid_t nid;
839         char *tail = *ptr;
840         char *mgsnid;
841         int   length;
842         int   oldlen = 0;
843
844         /* Find end of nidlist */
845         while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
846                 ;
847         length = tail - *ptr;
848         if (length == 0) {
849                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
850                 return -EINVAL;
851         }
852
853         if (lmd->lmd_mgs)
854                 oldlen = strlen(lmd->lmd_mgs) + 1;
855
856         mgsnid = kzalloc(oldlen + length + 1, GFP_NOFS);
857         if (!mgsnid)
858                 return -ENOMEM;
859
860         if (lmd->lmd_mgs) {
861                 /* Multiple mgsnid= are taken to mean failover locations */
862                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
863                 mgsnid[oldlen - 1] = ':';
864                 kfree(lmd->lmd_mgs);
865         }
866         memcpy(mgsnid + oldlen, *ptr, length);
867         mgsnid[oldlen + length] = '\0';
868         lmd->lmd_mgs = mgsnid;
869         *ptr = tail;
870
871         return 0;
872 }
873
874 /** Parse mount line options
875  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
876  * dev is passed as device=uml1:/lustre by mount.lustre
877  */
878 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
879 {
880         char *s1, *s2, *s3, *devname = NULL;
881         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
882         int rc = 0;
883
884         LASSERT(lmd);
885         if (!options) {
886                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that /sbin/mount.lustre is installed.\n");
887                 return -EINVAL;
888         }
889
890         /* Options should be a string - try to detect old lmd data */
891         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
892                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of /sbin/mount.lustre.  Please install version %s\n",
893                                    LUSTRE_VERSION_STRING);
894                 return -EINVAL;
895         }
896         lmd->lmd_magic = LMD_MAGIC;
897
898         lmd->lmd_params = kzalloc(LMD_PARAMS_MAXLEN, GFP_NOFS);
899         if (!lmd->lmd_params)
900                 return -ENOMEM;
901         lmd->lmd_params[0] = '\0';
902
903         /* Set default flags here */
904
905         s1 = options;
906         while (*s1) {
907                 int clear = 0;
908                 int time_min = OBD_RECOVERY_TIME_MIN;
909
910                 /* Skip whitespace and extra commas */
911                 while (*s1 == ' ' || *s1 == ',')
912                         s1++;
913                 s3 = s1;
914
915                 /* Client options are parsed in ll_options: eg. flock,
916                  * user_xattr, acl
917                  */
918
919                 /* Parse non-ldiskfs options here. Rather than modifying
920                  * ldiskfs, we just zero these out here
921                  */
922                 if (strncmp(s1, "abort_recov", 11) == 0) {
923                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
924                         clear++;
925                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
926                         lmd->lmd_recovery_time_soft = max_t(int,
927                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
928                         clear++;
929                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
930                         lmd->lmd_recovery_time_hard = max_t(int,
931                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
932                         clear++;
933                 } else if (strncmp(s1, "noir", 4) == 0) {
934                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
935                         clear++;
936                 } else if (strncmp(s1, "nosvc", 5) == 0) {
937                         lmd->lmd_flags |= LMD_FLG_NOSVC;
938                         clear++;
939                 } else if (strncmp(s1, "nomgs", 5) == 0) {
940                         lmd->lmd_flags |= LMD_FLG_NOMGS;
941                         clear++;
942                 } else if (strncmp(s1, "noscrub", 7) == 0) {
943                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
944                         clear++;
945                 } else if (strncmp(s1, PARAM_MGSNODE,
946                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
947                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
948                         /* Assume the next mount opt is the first
949                          * invalid nid we get to.
950                          */
951                         rc = lmd_parse_mgs(lmd, &s2);
952                         if (rc)
953                                 goto invalid;
954                         clear++;
955                 } else if (strncmp(s1, "writeconf", 9) == 0) {
956                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
957                         clear++;
958                 } else if (strncmp(s1, "update", 6) == 0) {
959                         lmd->lmd_flags |= LMD_FLG_UPDATE;
960                         clear++;
961                 } else if (strncmp(s1, "virgin", 6) == 0) {
962                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
963                         clear++;
964                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
965                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
966                         clear++;
967                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
968                         rc = lmd_parse_mgssec(lmd, s1 + 7);
969                         if (rc)
970                                 goto invalid;
971                         s3 = s2;
972                         clear++;
973                 /* ost exclusion list */
974                 } else if (strncmp(s1, "exclude=", 8) == 0) {
975                         rc = lmd_make_exclusion(lmd, s1 + 7);
976                         if (rc)
977                                 goto invalid;
978                         clear++;
979                 } else if (strncmp(s1, "mgs", 3) == 0) {
980                         /* We are an MGS */
981                         lmd->lmd_flags |= LMD_FLG_MGS;
982                         clear++;
983                 } else if (strncmp(s1, "svname=", 7) == 0) {
984                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
985                         if (rc)
986                                 goto invalid;
987                         clear++;
988                 } else if (strncmp(s1, "param=", 6) == 0) {
989                         size_t length, params_length;
990                         char *tail = strchr(s1 + 6, ',');
991
992                         if (!tail) {
993                                 length = strlen(s1);
994                         } else {
995                                 lnet_nid_t nid;
996                                 char *param_str = tail + 1;
997                                 int supplementary = 1;
998
999                                 while (!class_parse_nid_quiet(param_str, &nid,
1000                                                               &param_str)) {
1001                                         supplementary = 0;
1002                                 }
1003                                 length = param_str - s1 - supplementary;
1004                         }
1005                         length -= 6;
1006                         params_length = strlen(lmd->lmd_params);
1007                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1008                                 return -E2BIG;
1009                         strncat(lmd->lmd_params, s1 + 6, length);
1010                         lmd->lmd_params[params_length + length] = '\0';
1011                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1012                         s3 = s1 + 6 + length;
1013                         clear++;
1014                 } else if (strncmp(s1, "osd=", 4) == 0) {
1015                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1016                         if (rc)
1017                                 goto invalid;
1018                         clear++;
1019                 }
1020                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1021                  * end of the options.
1022                  */
1023                 else if (strncmp(s1, "device=", 7) == 0) {
1024                         devname = s1 + 7;
1025                         /* terminate options right before device.  device
1026                          * must be the last one.
1027                          */
1028                         *s1 = '\0';
1029                         break;
1030                 }
1031
1032                 /* Find next opt */
1033                 s2 = strchr(s1, ',');
1034                 if (!s2) {
1035                         if (clear)
1036                                 *s1 = '\0';
1037                         break;
1038                 }
1039                 s2++;
1040                 if (clear)
1041                         memmove(s1, s2, strlen(s2) + 1);
1042                 else
1043                         s1 = s2;
1044         }
1045
1046         if (!devname) {
1047                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name (need mount option 'device=...')\n");
1048                 goto invalid;
1049         }
1050
1051         s1 = strstr(devname, ":/");
1052         if (s1) {
1053                 ++s1;
1054                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1055                 /* Remove leading /s from fsname */
1056                 while (*++s1 == '/')
1057                         ;
1058                 /* Freed in lustre_free_lsi */
1059                 lmd->lmd_profile = kasprintf(GFP_NOFS, "%s-client", s1);
1060                 if (!lmd->lmd_profile)
1061                         return -ENOMEM;
1062         }
1063
1064         /* Freed in lustre_free_lsi */
1065         lmd->lmd_dev = kzalloc(strlen(devname) + 1, GFP_NOFS);
1066         if (!lmd->lmd_dev)
1067                 return -ENOMEM;
1068         strcpy(lmd->lmd_dev, devname);
1069
1070         /* Save mount options */
1071         s1 = options + strlen(options) - 1;
1072         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1073                 *s1-- = 0;
1074         if (*options != 0) {
1075                 /* Freed in lustre_free_lsi */
1076                 lmd->lmd_opts = kzalloc(strlen(options) + 1, GFP_NOFS);
1077                 if (!lmd->lmd_opts)
1078                         return -ENOMEM;
1079                 strcpy(lmd->lmd_opts, options);
1080         }
1081
1082         lmd_print(lmd);
1083         lmd->lmd_magic = LMD_MAGIC;
1084
1085         return rc;
1086
1087 invalid:
1088         CERROR("Bad mount options %s\n", options);
1089         return -EINVAL;
1090 }
1091
1092 struct lustre_mount_data2 {
1093         void *lmd2_data;
1094         struct vfsmount *lmd2_mnt;
1095 };
1096
1097 /** This is the entry point for the mount call into Lustre.
1098  * This is called when a server or client is mounted,
1099  * and this is where we start setting things up.
1100  * @param data Mount options (e.g. -o flock,abort_recov)
1101  */
1102 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1103 {
1104         struct lustre_mount_data *lmd;
1105         struct lustre_mount_data2 *lmd2 = data;
1106         struct lustre_sb_info *lsi;
1107         int rc;
1108
1109         CDEBUG(D_MOUNT | D_VFSTRACE, "VFS Op: sb %p\n", sb);
1110
1111         lsi = lustre_init_lsi(sb);
1112         if (!lsi)
1113                 return -ENOMEM;
1114         lmd = lsi->lsi_lmd;
1115
1116         /*
1117          * Disable lockdep during mount, because mount locking patterns are
1118          * `special'.
1119          */
1120         lockdep_off();
1121
1122         /*
1123          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1124          */
1125         obd_zombie_barrier();
1126
1127         /* Figure out the lmd from the mount options */
1128         if (lmd_parse((lmd2->lmd2_data), lmd)) {
1129                 lustre_put_lsi(sb);
1130                 rc = -EINVAL;
1131                 goto out;
1132         }
1133
1134         if (lmd_is_client(lmd)) {
1135                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1136                 if (!client_fill_super)
1137                         request_module("lustre");
1138                 if (!client_fill_super) {
1139                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for client mount! Is the 'lustre' module loaded?\n");
1140                         lustre_put_lsi(sb);
1141                         rc = -ENODEV;
1142                 } else {
1143                         rc = lustre_start_mgc(sb);
1144                         if (rc) {
1145                                 lustre_common_put_super(sb);
1146                                 goto out;
1147                         }
1148                         /* Connect and start */
1149                         /* (should always be ll_fill_super) */
1150                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1151                         /* c_f_s will call lustre_common_put_super on failure */
1152                 }
1153         } else {
1154                 CERROR("This is client-side-only module, cannot handle server mount.\n");
1155                 rc = -EINVAL;
1156         }
1157
1158         /* If error happens in fill_super() call, @lsi will be killed there.
1159          * This is why we do not put it here.
1160          */
1161         goto out;
1162 out:
1163         if (rc) {
1164                 CERROR("Unable to mount %s (%d)\n",
1165                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1166         } else {
1167                 CDEBUG(D_SUPER, "Mount %s complete\n",
1168                        lmd->lmd_dev);
1169         }
1170         lockdep_on();
1171         return rc;
1172 }
1173
1174 /* We can't call ll_fill_super by name because it lives in a module that
1175  * must be loaded after this one.
1176  */
1177 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1178                                                   struct vfsmount *mnt))
1179 {
1180         client_fill_super = cfs;
1181 }
1182 EXPORT_SYMBOL(lustre_register_client_fill_super);
1183
1184 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1185 {
1186         kill_super_cb = cfs;
1187 }
1188 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1189
1190 /***************** FS registration ******************/
1191 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1192                                    const char *devname, void *data)
1193 {
1194         struct lustre_mount_data2 lmd2 = {
1195                 .lmd2_data = data,
1196                 .lmd2_mnt = NULL
1197         };
1198
1199         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1200 }
1201
1202 static void lustre_kill_super(struct super_block *sb)
1203 {
1204         struct lustre_sb_info *lsi = s2lsi(sb);
1205
1206         if (kill_super_cb && lsi)
1207                 (*kill_super_cb)(sb);
1208
1209         kill_anon_super(sb);
1210 }
1211
1212 /** Register the "lustre" fs type
1213  */
1214 static struct file_system_type lustre_fs_type = {
1215         .owner  = THIS_MODULE,
1216         .name    = "lustre",
1217         .mount  = lustre_mount,
1218         .kill_sb      = lustre_kill_super,
1219         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
1220                         FS_RENAME_DOES_D_MOVE,
1221 };
1222 MODULE_ALIAS_FS("lustre");
1223
1224 int lustre_register_fs(void)
1225 {
1226         return register_filesystem(&lustre_fs_type);
1227 }
1228
1229 int lustre_unregister_fs(void)
1230 {
1231         return unregister_filesystem(&lustre_fs_type);
1232 }