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