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