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