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