Merge remote-tracking branch 'jk/vfs' into work.misc
[cascardo/linux.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/linux/lustre_compat25.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 LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
388         data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
389 #else
390 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
391 #endif
392
393         if (lmd_is_client(lsi->lsi_lmd) &&
394             lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
395                 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
396         data->ocd_version = LUSTRE_VERSION_CODE;
397         rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
398         if (rc) {
399                 CERROR("connect failed %d\n", rc);
400                 goto out;
401         }
402
403         obd->u.cli.cl_mgc_mgsexp = exp;
404
405 out:
406         /* Keep the mgc info in the sb. Note that many lsi's can point
407          * to the same mgc.
408          */
409         lsi->lsi_mgc = obd;
410 out_free:
411         mutex_unlock(&mgc_start_lock);
412
413         kfree(data);
414         kfree(mgcname);
415         kfree(niduuid);
416         return rc;
417 }
418
419 static int lustre_stop_mgc(struct super_block *sb)
420 {
421         struct lustre_sb_info *lsi = s2lsi(sb);
422         struct obd_device *obd;
423         char *niduuid = NULL, *ptr = NULL;
424         int i, rc = 0, len = 0;
425
426         if (!lsi)
427                 return -ENOENT;
428         obd = lsi->lsi_mgc;
429         if (!obd)
430                 return -ENOENT;
431         lsi->lsi_mgc = NULL;
432
433         mutex_lock(&mgc_start_lock);
434         LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
435         if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
436                 /* This is not fatal, every client that stops
437                  * will call in here.
438                  */
439                 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
440                        atomic_read(&obd->u.cli.cl_mgc_refcount));
441                 rc = -EBUSY;
442                 goto out;
443         }
444
445         /* The MGC has no recoverable data in any case.
446          * force shutdown set in umount_begin
447          */
448         obd->obd_no_recov = 1;
449
450         if (obd->u.cli.cl_mgc_mgsexp) {
451                 /* An error is not fatal, if we are unable to send the
452                  * disconnect mgs ping evictor cleans up the export
453                  */
454                 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
455                 if (rc)
456                         CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
457         }
458
459         /* Save the obdname for cleaning the nid uuids, which are obdname_XX */
460         len = strlen(obd->obd_name) + 6;
461         niduuid = kzalloc(len, GFP_NOFS);
462         if (niduuid) {
463                 strcpy(niduuid, obd->obd_name);
464                 ptr = niduuid + strlen(niduuid);
465         }
466
467         rc = class_manual_cleanup(obd);
468         if (rc)
469                 goto out;
470
471         /* Clean the nid uuids */
472         if (!niduuid) {
473                 rc = -ENOMEM;
474                 goto out;
475         }
476
477         for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
478                 sprintf(ptr, "_%x", i);
479                 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
480                              niduuid, NULL, NULL, NULL);
481                 if (rc)
482                         CERROR("del MDC UUID %s failed: rc = %d\n",
483                                niduuid, rc);
484         }
485 out:
486         kfree(niduuid);
487
488         /* class_import_put will get rid of the additional connections */
489         mutex_unlock(&mgc_start_lock);
490         return rc;
491 }
492
493 /***************** lustre superblock **************/
494
495 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
496 {
497         struct lustre_sb_info *lsi;
498
499         lsi = kzalloc(sizeof(*lsi), GFP_NOFS);
500         if (!lsi)
501                 return NULL;
502         lsi->lsi_lmd = kzalloc(sizeof(*lsi->lsi_lmd), GFP_NOFS);
503         if (!lsi->lsi_lmd) {
504                 kfree(lsi);
505                 return NULL;
506         }
507
508         lsi->lsi_lmd->lmd_exclude_count = 0;
509         lsi->lsi_lmd->lmd_recovery_time_soft = 0;
510         lsi->lsi_lmd->lmd_recovery_time_hard = 0;
511         s2lsi_nocast(sb) = lsi;
512         /* we take 1 extra ref for our setup */
513         atomic_set(&lsi->lsi_mounts, 1);
514
515         /* Default umount style */
516         lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
517
518         return lsi;
519 }
520
521 static int lustre_free_lsi(struct super_block *sb)
522 {
523         struct lustre_sb_info *lsi = s2lsi(sb);
524
525         CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
526
527         /* someone didn't call server_put_mount. */
528         LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
529
530         if (lsi->lsi_lmd) {
531                 kfree(lsi->lsi_lmd->lmd_dev);
532                 kfree(lsi->lsi_lmd->lmd_profile);
533                 kfree(lsi->lsi_lmd->lmd_mgssec);
534                 kfree(lsi->lsi_lmd->lmd_opts);
535                 if (lsi->lsi_lmd->lmd_exclude_count)
536                         kfree(lsi->lsi_lmd->lmd_exclude);
537                 kfree(lsi->lsi_lmd->lmd_mgs);
538                 kfree(lsi->lsi_lmd->lmd_osd_type);
539                 kfree(lsi->lsi_lmd->lmd_params);
540
541                 kfree(lsi->lsi_lmd);
542         }
543
544         LASSERT(!lsi->lsi_llsbi);
545         kfree(lsi);
546         s2lsi_nocast(sb) = NULL;
547
548         return 0;
549 }
550
551 /* The lsi has one reference for every server that is using the disk -
552  * e.g. MDT, MGS, and potentially MGC
553  */
554 static int lustre_put_lsi(struct super_block *sb)
555 {
556         struct lustre_sb_info *lsi = s2lsi(sb);
557
558         CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
559         if (atomic_dec_and_test(&lsi->lsi_mounts)) {
560                 lustre_free_lsi(sb);
561                 return 1;
562         }
563         return 0;
564 }
565
566 /*** SERVER NAME ***
567  * <FSNAME><SEPARATOR><TYPE><INDEX>
568  * FSNAME is between 1 and 8 characters (inclusive).
569  *      Excluded characters are '/' and ':'
570  * SEPARATOR is either ':' or '-'
571  * TYPE: "OST", "MDT", etc.
572  * INDEX: Hex representation of the index
573  */
574
575 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
576  * @param [in] svname server name including type and index
577  * @param [out] fsname Buffer to copy filesystem name prefix into.
578  *  Must have at least 'strlen(fsname) + 1' chars.
579  * @param [out] endptr if endptr isn't NULL it is set to end of fsname
580  * rc < 0  on error
581  */
582 static int server_name2fsname(const char *svname, char *fsname,
583                               const char **endptr)
584 {
585         const char *dash;
586
587         dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
588         for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
589                 ;
590         if (dash == svname)
591                 return -EINVAL;
592
593         if (fsname) {
594                 strncpy(fsname, svname, dash - svname);
595                 fsname[dash - svname] = '\0';
596         }
597
598         if (endptr)
599                 *endptr = dash;
600
601         return 0;
602 }
603
604 /* Get the index from the obd name.
605  *  rc = server type, or
606  * rc < 0  on error
607  * if endptr isn't NULL it is set to end of name
608  */
609 static int server_name2index(const char *svname, __u32 *idx,
610                              const char **endptr)
611 {
612         unsigned long index;
613         int rc;
614         const char *dash;
615
616         /* We use server_name2fsname() just for parsing */
617         rc = server_name2fsname(svname, NULL, &dash);
618         if (rc != 0)
619                 return rc;
620
621         dash++;
622
623         if (strncmp(dash, "MDT", 3) == 0)
624                 rc = LDD_F_SV_TYPE_MDT;
625         else if (strncmp(dash, "OST", 3) == 0)
626                 rc = LDD_F_SV_TYPE_OST;
627         else
628                 return -EINVAL;
629
630         dash += 3;
631
632         if (strncmp(dash, "all", 3) == 0) {
633                 if (endptr)
634                         *endptr = dash + 3;
635                 return rc | LDD_F_SV_ALL;
636         }
637
638         index = simple_strtoul(dash, (char **)endptr, 16);
639         if (idx)
640                 *idx = index;
641
642         /* Account for -mdc after index that is possible when specifying mdt */
643         if (endptr && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
644                               sizeof(LUSTRE_MDC_NAME) - 1) == 0)
645                 *endptr += sizeof(LUSTRE_MDC_NAME);
646
647         return rc;
648 }
649
650 /*************** mount common between server and client ***************/
651
652 /* Common umount */
653 int lustre_common_put_super(struct super_block *sb)
654 {
655         int rc;
656
657         CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
658
659         /* Drop a ref to the MGC */
660         rc = lustre_stop_mgc(sb);
661         if (rc && (rc != -ENOENT)) {
662                 if (rc != -EBUSY) {
663                         CERROR("Can't stop MGC: %d\n", rc);
664                         return rc;
665                 }
666                 /* BUSY just means that there's some other obd that
667                  * needs the mgc.  Let him clean it up.
668                  */
669                 CDEBUG(D_MOUNT, "MGC still in use\n");
670         }
671         /* Drop a ref to the mounted disk */
672         lustre_put_lsi(sb);
673         lu_types_stop();
674         return rc;
675 }
676 EXPORT_SYMBOL(lustre_common_put_super);
677
678 static void lmd_print(struct lustre_mount_data *lmd)
679 {
680         int i;
681
682         PRINT_CMD(D_MOUNT, "  mount data:\n");
683         if (lmd_is_client(lmd))
684                 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
685         PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
686         PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
687
688         if (lmd->lmd_opts)
689                 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
690
691         if (lmd->lmd_recovery_time_soft)
692                 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
693                           lmd->lmd_recovery_time_soft);
694
695         if (lmd->lmd_recovery_time_hard)
696                 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
697                           lmd->lmd_recovery_time_hard);
698
699         for (i = 0; i < lmd->lmd_exclude_count; i++) {
700                 PRINT_CMD(D_MOUNT, "exclude %d:  OST%04x\n", i,
701                           lmd->lmd_exclude[i]);
702         }
703 }
704
705 /* Is this server on the exclusion list */
706 int lustre_check_exclusion(struct super_block *sb, char *svname)
707 {
708         struct lustre_sb_info *lsi = s2lsi(sb);
709         struct lustre_mount_data *lmd = lsi->lsi_lmd;
710         __u32 index;
711         int i, rc;
712
713         rc = server_name2index(svname, &index, NULL);
714         if (rc != LDD_F_SV_TYPE_OST)
715                 /* Only exclude OSTs */
716                 return 0;
717
718         CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
719                index, lmd->lmd_exclude_count, lmd->lmd_dev);
720
721         for (i = 0; i < lmd->lmd_exclude_count; i++) {
722                 if (index == lmd->lmd_exclude[i]) {
723                         CWARN("Excluding %s (on exclusion list)\n", svname);
724                         return 1;
725                 }
726         }
727         return 0;
728 }
729
730 /* mount -v  -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
731 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
732 {
733         const char *s1 = ptr, *s2;
734         __u32 index, *exclude_list;
735         int rc = 0, devmax;
736
737         /* The shortest an ost name can be is 8 chars: -OST0000.
738          * We don't actually know the fsname at this time, so in fact
739          * a user could specify any fsname.
740          */
741         devmax = strlen(ptr) / 8 + 1;
742
743         /* temp storage until we figure out how many we have */
744         exclude_list = kcalloc(devmax, sizeof(index), GFP_NOFS);
745         if (!exclude_list)
746                 return -ENOMEM;
747
748         /* we enter this fn pointing at the '=' */
749         while (*s1 && *s1 != ' ' && *s1 != ',') {
750                 s1++;
751                 rc = server_name2index(s1, &index, &s2);
752                 if (rc < 0) {
753                         CERROR("Can't parse server name '%s': rc = %d\n",
754                                s1, rc);
755                         break;
756                 }
757                 if (rc == LDD_F_SV_TYPE_OST)
758                         exclude_list[lmd->lmd_exclude_count++] = index;
759                 else
760                         CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
761                                (uint)(s2-s1), s1, rc);
762                 s1 = s2;
763                 /* now we are pointing at ':' (next exclude)
764                  * or ',' (end of excludes)
765                  */
766                 if (lmd->lmd_exclude_count >= devmax)
767                         break;
768         }
769         if (rc >= 0) /* non-err */
770                 rc = 0;
771
772         if (lmd->lmd_exclude_count) {
773                 /* permanent, freed in lustre_free_lsi */
774                 lmd->lmd_exclude = kcalloc(lmd->lmd_exclude_count,
775                                            sizeof(index), GFP_NOFS);
776                 if (lmd->lmd_exclude) {
777                         memcpy(lmd->lmd_exclude, exclude_list,
778                                sizeof(index) * lmd->lmd_exclude_count);
779                 } else {
780                         rc = -ENOMEM;
781                         lmd->lmd_exclude_count = 0;
782                 }
783         }
784         kfree(exclude_list);
785         return rc;
786 }
787
788 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
789 {
790         char   *tail;
791         int     length;
792
793         kfree(lmd->lmd_mgssec);
794         lmd->lmd_mgssec = NULL;
795
796         tail = strchr(ptr, ',');
797         if (!tail)
798                 length = strlen(ptr);
799         else
800                 length = tail - ptr;
801
802         lmd->lmd_mgssec = kzalloc(length + 1, GFP_NOFS);
803         if (!lmd->lmd_mgssec)
804                 return -ENOMEM;
805
806         memcpy(lmd->lmd_mgssec, ptr, length);
807         lmd->lmd_mgssec[length] = '\0';
808         return 0;
809 }
810
811 static int lmd_parse_string(char **handle, char *ptr)
812 {
813         char   *tail;
814         int     length;
815
816         if (!handle || !ptr)
817                 return -EINVAL;
818
819         kfree(*handle);
820         *handle = NULL;
821
822         tail = strchr(ptr, ',');
823         if (!tail)
824                 length = strlen(ptr);
825         else
826                 length = tail - ptr;
827
828         *handle = kzalloc(length + 1, GFP_NOFS);
829         if (!*handle)
830                 return -ENOMEM;
831
832         memcpy(*handle, ptr, length);
833         (*handle)[length] = '\0';
834
835         return 0;
836 }
837
838 /* Collect multiple values for mgsnid specifiers */
839 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
840 {
841         lnet_nid_t nid;
842         char *tail = *ptr;
843         char *mgsnid;
844         int   length;
845         int   oldlen = 0;
846
847         /* Find end of nidlist */
848         while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
849                 ;
850         length = tail - *ptr;
851         if (length == 0) {
852                 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
853                 return -EINVAL;
854         }
855
856         if (lmd->lmd_mgs)
857                 oldlen = strlen(lmd->lmd_mgs) + 1;
858
859         mgsnid = kzalloc(oldlen + length + 1, GFP_NOFS);
860         if (!mgsnid)
861                 return -ENOMEM;
862
863         if (lmd->lmd_mgs) {
864                 /* Multiple mgsnid= are taken to mean failover locations */
865                 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
866                 mgsnid[oldlen - 1] = ':';
867                 kfree(lmd->lmd_mgs);
868         }
869         memcpy(mgsnid + oldlen, *ptr, length);
870         mgsnid[oldlen + length] = '\0';
871         lmd->lmd_mgs = mgsnid;
872         *ptr = tail;
873
874         return 0;
875 }
876
877 /** Parse mount line options
878  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
879  * dev is passed as device=uml1:/lustre by mount.lustre
880  */
881 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
882 {
883         char *s1, *s2, *devname = NULL;
884         struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
885         int rc = 0;
886
887         LASSERT(lmd);
888         if (!options) {
889                 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that /sbin/mount.lustre is installed.\n");
890                 return -EINVAL;
891         }
892
893         /* Options should be a string - try to detect old lmd data */
894         if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
895                 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of /sbin/mount.lustre.  Please install version %s\n",
896                                    LUSTRE_VERSION_STRING);
897                 return -EINVAL;
898         }
899         lmd->lmd_magic = LMD_MAGIC;
900
901         lmd->lmd_params = kzalloc(LMD_PARAMS_MAXLEN, GFP_NOFS);
902         if (!lmd->lmd_params)
903                 return -ENOMEM;
904         lmd->lmd_params[0] = '\0';
905
906         /* Set default flags here */
907
908         s1 = options;
909         while (*s1) {
910                 int clear = 0;
911                 int time_min = OBD_RECOVERY_TIME_MIN;
912
913                 /* Skip whitespace and extra commas */
914                 while (*s1 == ' ' || *s1 == ',')
915                         s1++;
916
917                 /* Client options are parsed in ll_options: eg. flock,
918                  * user_xattr, acl
919                  */
920
921                 /* Parse non-ldiskfs options here. Rather than modifying
922                  * ldiskfs, we just zero these out here
923                  */
924                 if (strncmp(s1, "abort_recov", 11) == 0) {
925                         lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
926                         clear++;
927                 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
928                         lmd->lmd_recovery_time_soft = max_t(int,
929                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
930                         clear++;
931                 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
932                         lmd->lmd_recovery_time_hard = max_t(int,
933                                 simple_strtoul(s1 + 19, NULL, 10), time_min);
934                         clear++;
935                 } else if (strncmp(s1, "noir", 4) == 0) {
936                         lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
937                         clear++;
938                 } else if (strncmp(s1, "nosvc", 5) == 0) {
939                         lmd->lmd_flags |= LMD_FLG_NOSVC;
940                         clear++;
941                 } else if (strncmp(s1, "nomgs", 5) == 0) {
942                         lmd->lmd_flags |= LMD_FLG_NOMGS;
943                         clear++;
944                 } else if (strncmp(s1, "noscrub", 7) == 0) {
945                         lmd->lmd_flags |= LMD_FLG_NOSCRUB;
946                         clear++;
947                 } else if (strncmp(s1, PARAM_MGSNODE,
948                                    sizeof(PARAM_MGSNODE) - 1) == 0) {
949                         s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
950                         /* Assume the next mount opt is the first
951                          * invalid nid we get to.
952                          */
953                         rc = lmd_parse_mgs(lmd, &s2);
954                         if (rc)
955                                 goto invalid;
956                         clear++;
957                 } else if (strncmp(s1, "writeconf", 9) == 0) {
958                         lmd->lmd_flags |= LMD_FLG_WRITECONF;
959                         clear++;
960                 } else if (strncmp(s1, "update", 6) == 0) {
961                         lmd->lmd_flags |= LMD_FLG_UPDATE;
962                         clear++;
963                 } else if (strncmp(s1, "virgin", 6) == 0) {
964                         lmd->lmd_flags |= LMD_FLG_VIRGIN;
965                         clear++;
966                 } else if (strncmp(s1, "noprimnode", 10) == 0) {
967                         lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
968                         clear++;
969                 } else if (strncmp(s1, "mgssec=", 7) == 0) {
970                         rc = lmd_parse_mgssec(lmd, s1 + 7);
971                         if (rc)
972                                 goto invalid;
973                         clear++;
974                 /* ost exclusion list */
975                 } else if (strncmp(s1, "exclude=", 8) == 0) {
976                         rc = lmd_make_exclusion(lmd, s1 + 7);
977                         if (rc)
978                                 goto invalid;
979                         clear++;
980                 } else if (strncmp(s1, "mgs", 3) == 0) {
981                         /* We are an MGS */
982                         lmd->lmd_flags |= LMD_FLG_MGS;
983                         clear++;
984                 } else if (strncmp(s1, "svname=", 7) == 0) {
985                         rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
986                         if (rc)
987                                 goto invalid;
988                         clear++;
989                 } else if (strncmp(s1, "param=", 6) == 0) {
990                         size_t length, params_length;
991                         char *tail = strchr(s1 + 6, ',');
992
993                         if (!tail)
994                                 length = strlen(s1);
995                         else
996                                 length = tail - s1;
997                         length -= 6;
998                         params_length = strlen(lmd->lmd_params);
999                         if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1000                                 return -E2BIG;
1001                         strncat(lmd->lmd_params, s1 + 6, length);
1002                         lmd->lmd_params[params_length + length] = '\0';
1003                         strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1004                         clear++;
1005                 } else if (strncmp(s1, "osd=", 4) == 0) {
1006                         rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1007                         if (rc)
1008                                 goto invalid;
1009                         clear++;
1010                 }
1011                 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1012                  * end of the options.
1013                  */
1014                 else if (strncmp(s1, "device=", 7) == 0) {
1015                         devname = s1 + 7;
1016                         /* terminate options right before device.  device
1017                          * must be the last one.
1018                          */
1019                         *s1 = '\0';
1020                         break;
1021                 }
1022
1023                 /* Find next opt */
1024                 s2 = strchr(s1, ',');
1025                 if (!s2) {
1026                         if (clear)
1027                                 *s1 = '\0';
1028                         break;
1029                 }
1030                 s2++;
1031                 if (clear)
1032                         memmove(s1, s2, strlen(s2) + 1);
1033                 else
1034                         s1 = s2;
1035         }
1036
1037         if (!devname) {
1038                 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name (need mount option 'device=...')\n");
1039                 goto invalid;
1040         }
1041
1042         s1 = strstr(devname, ":/");
1043         if (s1) {
1044                 ++s1;
1045                 lmd->lmd_flags |= LMD_FLG_CLIENT;
1046                 /* Remove leading /s from fsname */
1047                 while (*++s1 == '/')
1048                         ;
1049                 /* Freed in lustre_free_lsi */
1050                 lmd->lmd_profile = kasprintf(GFP_NOFS, "%s-client", s1);
1051                 if (!lmd->lmd_profile)
1052                         return -ENOMEM;
1053         }
1054
1055         /* Freed in lustre_free_lsi */
1056         lmd->lmd_dev = kzalloc(strlen(devname) + 1, GFP_NOFS);
1057         if (!lmd->lmd_dev)
1058                 return -ENOMEM;
1059         strcpy(lmd->lmd_dev, devname);
1060
1061         /* Save mount options */
1062         s1 = options + strlen(options) - 1;
1063         while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1064                 *s1-- = 0;
1065         if (*options != 0) {
1066                 /* Freed in lustre_free_lsi */
1067                 lmd->lmd_opts = kzalloc(strlen(options) + 1, GFP_NOFS);
1068                 if (!lmd->lmd_opts)
1069                         return -ENOMEM;
1070                 strcpy(lmd->lmd_opts, options);
1071         }
1072
1073         lmd_print(lmd);
1074         lmd->lmd_magic = LMD_MAGIC;
1075
1076         return rc;
1077
1078 invalid:
1079         CERROR("Bad mount options %s\n", options);
1080         return -EINVAL;
1081 }
1082
1083 struct lustre_mount_data2 {
1084         void *lmd2_data;
1085         struct vfsmount *lmd2_mnt;
1086 };
1087
1088 /** This is the entry point for the mount call into Lustre.
1089  * This is called when a server or client is mounted,
1090  * and this is where we start setting things up.
1091  * @param data Mount options (e.g. -o flock,abort_recov)
1092  */
1093 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1094 {
1095         struct lustre_mount_data *lmd;
1096         struct lustre_mount_data2 *lmd2 = data;
1097         struct lustre_sb_info *lsi;
1098         int rc;
1099
1100         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1101
1102         lsi = lustre_init_lsi(sb);
1103         if (!lsi)
1104                 return -ENOMEM;
1105         lmd = lsi->lsi_lmd;
1106
1107         /*
1108          * Disable lockdep during mount, because mount locking patterns are
1109          * `special'.
1110          */
1111         lockdep_off();
1112
1113         /*
1114          * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1115          */
1116         obd_zombie_barrier();
1117
1118         /* Figure out the lmd from the mount options */
1119         if (lmd_parse((lmd2->lmd2_data), lmd)) {
1120                 lustre_put_lsi(sb);
1121                 rc = -EINVAL;
1122                 goto out;
1123         }
1124
1125         if (lmd_is_client(lmd)) {
1126                 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1127                 if (!client_fill_super)
1128                         request_module("lustre");
1129                 if (!client_fill_super) {
1130                         LCONSOLE_ERROR_MSG(0x165, "Nothing registered for client mount! Is the 'lustre' module loaded?\n");
1131                         lustre_put_lsi(sb);
1132                         rc = -ENODEV;
1133                 } else {
1134                         rc = lustre_start_mgc(sb);
1135                         if (rc) {
1136                                 lustre_put_lsi(sb);
1137                                 goto out;
1138                         }
1139                         /* Connect and start */
1140                         /* (should always be ll_fill_super) */
1141                         rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1142                         /* c_f_s will call lustre_common_put_super on failure */
1143                 }
1144         } else {
1145                 CERROR("This is client-side-only module, cannot handle server mount.\n");
1146                 rc = -EINVAL;
1147         }
1148
1149         /* If error happens in fill_super() call, @lsi will be killed there.
1150          * This is why we do not put it here.
1151          */
1152         goto out;
1153 out:
1154         if (rc) {
1155                 CERROR("Unable to mount %s (%d)\n",
1156                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
1157         } else {
1158                 CDEBUG(D_SUPER, "Mount %s complete\n",
1159                        lmd->lmd_dev);
1160         }
1161         lockdep_on();
1162         return rc;
1163 }
1164
1165 /* We can't call ll_fill_super by name because it lives in a module that
1166  * must be loaded after this one.
1167  */
1168 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1169                                                   struct vfsmount *mnt))
1170 {
1171         client_fill_super = cfs;
1172 }
1173 EXPORT_SYMBOL(lustre_register_client_fill_super);
1174
1175 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1176 {
1177         kill_super_cb = cfs;
1178 }
1179 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1180
1181 /***************** FS registration ******************/
1182 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1183                                    const char *devname, void *data)
1184 {
1185         struct lustre_mount_data2 lmd2 = {
1186                 .lmd2_data = data,
1187                 .lmd2_mnt = NULL
1188         };
1189
1190         return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1191 }
1192
1193 static void lustre_kill_super(struct super_block *sb)
1194 {
1195         struct lustre_sb_info *lsi = s2lsi(sb);
1196
1197         if (kill_super_cb && lsi)
1198                 (*kill_super_cb)(sb);
1199
1200         kill_anon_super(sb);
1201 }
1202
1203 /** Register the "lustre" fs type
1204  */
1205 static struct file_system_type lustre_fs_type = {
1206         .owner  = THIS_MODULE,
1207         .name    = "lustre",
1208         .mount  = lustre_mount,
1209         .kill_sb      = lustre_kill_super,
1210         .fs_flags     = FS_BINARY_MOUNTDATA | FS_REQUIRES_DEV |
1211                         FS_RENAME_DOES_D_MOVE,
1212 };
1213 MODULE_ALIAS_FS("lustre");
1214
1215 int lustre_register_fs(void)
1216 {
1217         return register_filesystem(&lustre_fs_type);
1218 }
1219
1220 int lustre_unregister_fs(void)
1221 {
1222         return unregister_filesystem(&lustre_fs_type);
1223 }