Whamcloud - gitweb
LU-17592 build: kernel 6.8 removed strlcpy()
[fs/lustre-release.git] / lustre / target / tgt_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) 2013, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/obd_mount_server.c
32  *
33  * Server mount routines
34  *
35  * Author: Nathan Rutman <nathan@clusterfs.com>
36  */
37 #define DEBUG_SUBSYSTEM S_CLASS
38 #define D_MOUNT (D_SUPER | D_CONFIG /* | D_WARNING */)
39
40 #include <linux/types.h>
41 #include <linux/generic-radix-tree.h>
42 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
43 #include <linux/selinux.h>
44 #endif
45 #include <linux/statfs.h>
46 #include <linux/version.h>
47 #include <linux/delay.h>
48 #include <linux/file.h>
49 #ifdef HAVE_FSMAP_H
50 #include <linux/fsmap.h>
51 #endif
52
53 #include <llog_swab.h>
54 #include <lustre_disk.h>
55 #include <uapi/linux/lustre/lustre_ioctl.h>
56 #include <lustre_log.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <obd.h>
59 #include <obd_class.h>
60
61 #include "tgt_internal.h"
62
63 /*********** mount lookup *********/
64
65 static DEFINE_MUTEX(lustre_mount_info_lock);
66 static LIST_HEAD(server_mount_info_list);
67
68 static struct lustre_mount_info *server_find_mount(const char *name)
69 {
70         struct list_head *tmp;
71         struct lustre_mount_info *lmi;
72
73         ENTRY;
74         list_for_each(tmp, &server_mount_info_list) {
75                 lmi = list_entry(tmp, struct lustre_mount_info,
76                                  lmi_list_chain);
77                 if (strcmp(name, lmi->lmi_name) == 0)
78                         RETURN(lmi);
79         }
80         RETURN(NULL);
81 }
82
83 /* we must register an obd for a mount before we call the setup routine.
84  *_setup will call lustre_get_mount to get the mnt struct
85  * by obd_name, since we can't pass the pointer to setup.
86  */
87 static int server_register_mount(const char *name, struct super_block *sb)
88 {
89         struct lustre_mount_info *lmi;
90         char *name_cp;
91
92         ENTRY;
93         LASSERT(sb);
94
95         OBD_ALLOC(lmi, sizeof(*lmi));
96         if (!lmi)
97                 RETURN(-ENOMEM);
98         OBD_ALLOC(name_cp, strlen(name) + 1);
99         if (!name_cp) {
100                 OBD_FREE(lmi, sizeof(*lmi));
101                 RETURN(-ENOMEM);
102         }
103         strcpy(name_cp, name);
104
105         mutex_lock(&lustre_mount_info_lock);
106
107         if (server_find_mount(name)) {
108                 mutex_unlock(&lustre_mount_info_lock);
109                 OBD_FREE(lmi, sizeof(*lmi));
110                 OBD_FREE(name_cp, strlen(name) + 1);
111                 CERROR("Already registered %s\n", name);
112                 RETURN(-EEXIST);
113         }
114         lmi->lmi_name = name_cp;
115         lmi->lmi_sb = sb;
116         list_add(&lmi->lmi_list_chain, &server_mount_info_list);
117
118         mutex_unlock(&lustre_mount_info_lock);
119
120         CDEBUG(D_MOUNT, "register mount %p from %s\n", sb, name);
121
122         RETURN(0);
123 }
124
125 /* when an obd no longer needs a mount */
126 static int server_deregister_mount(const char *name)
127 {
128         struct lustre_mount_info *lmi;
129
130         ENTRY;
131         mutex_lock(&lustre_mount_info_lock);
132         lmi = server_find_mount(name);
133         if (!lmi) {
134                 mutex_unlock(&lustre_mount_info_lock);
135                 CERROR("%s not registered\n", name);
136                 RETURN(-ENOENT);
137         }
138
139         CDEBUG(D_MOUNT, "deregister mount %p from %s\n", lmi->lmi_sb, name);
140
141         OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
142         list_del(&lmi->lmi_list_chain);
143         OBD_FREE(lmi, sizeof(*lmi));
144         mutex_unlock(&lustre_mount_info_lock);
145
146         CFS_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
147         RETURN(0);
148 }
149
150 /* obd's look up a registered mount using their obdname. This is just
151  * for initial obd setup to find the mount struct.  It should not be
152  * called every time you want to mntget.
153  */
154 struct lustre_mount_info *server_get_mount(const char *name)
155 {
156         struct lustre_mount_info *lmi;
157         struct lustre_sb_info *lsi;
158
159         ENTRY;
160         mutex_lock(&lustre_mount_info_lock);
161         lmi = server_find_mount(name);
162         mutex_unlock(&lustre_mount_info_lock);
163         if (!lmi) {
164                 CERROR("Can't find mount for %s\n", name);
165                 RETURN(NULL);
166         }
167         lsi = s2lsi(lmi->lmi_sb);
168
169         kref_get(&lsi->lsi_mounts);
170
171         CDEBUG(D_MOUNT, "get mount %p from %s, refs=%d\n", lmi->lmi_sb,
172                name, kref_read(&lsi->lsi_mounts));
173
174         RETURN(lmi);
175 }
176 EXPORT_SYMBOL(server_get_mount);
177
178 /**
179  * server_put_mount: to be called from obd_cleanup methods
180  * @name:       obd name
181  * @dereg_mnt:  0 or 1 depending on whether the mount is to be deregistered or
182  * not
183  *
184  * The caller decides whether server_deregister_mount() needs to be called or
185  * not. Calling of server_deregister_mount() does not depend on refcounting on
186  * lsi because we could have say the mgs and mds on the same node and we
187  * unmount the mds, then the ref on the lsi would still be non-zero but we
188  * would still want to deregister the mds mount.
189  */
190 int server_put_mount(const char *name, bool dereg_mnt)
191 {
192         struct lustre_mount_info *lmi;
193         struct lustre_sb_info *lsi;
194
195         ENTRY;
196         mutex_lock(&lustre_mount_info_lock);
197         lmi = server_find_mount(name);
198         mutex_unlock(&lustre_mount_info_lock);
199         if (!lmi) {
200                 CERROR("Can't find mount for %s\n", name);
201                 RETURN(-ENOENT);
202         }
203         lsi = s2lsi(lmi->lmi_sb);
204
205         CDEBUG(D_MOUNT, "put mount %p from %s, refs=%d\n",
206                lmi->lmi_sb, name, kref_read(&lsi->lsi_mounts));
207
208         if (lustre_put_lsi(lmi->lmi_sb))
209                 CDEBUG(D_MOUNT, "Last put of mount %p from %s\n",
210                        lmi->lmi_sb, name);
211
212         if (dereg_mnt)
213                 /* this obd should never need the mount again */
214                 server_deregister_mount(name);
215
216         RETURN(0);
217 }
218 EXPORT_SYMBOL(server_put_mount);
219
220 /* Set up a MGS to serve startup logs */
221 static int server_start_mgs(struct super_block *sb)
222 {
223         struct lustre_sb_info *lsi = s2lsi(sb);
224         struct lustre_mount_info *lmi;
225         int rc = 0;
226
227         ENTRY;
228         /* It is impossible to have more than 1 MGS per node, since
229          * MGC wouldn't know which to connect to
230          */
231         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
232         if (lmi) {
233                 lsi = s2lsi(lmi->lmi_sb);
234                 LCONSOLE_ERROR_MSG(0x15d,
235                                    "The MGS service was already started from server\n");
236                 RETURN(-EALREADY);
237         }
238
239         CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
240
241         rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb);
242         if (rc < 0)
243                 GOTO(report_err, rc);
244
245         rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
246                                  LUSTRE_MGS_OBDNAME, NULL, NULL,
247                                  lsi->lsi_osd_obdname, NULL);
248         /* server_deregister_mount() is not called previously, for lsi
249          * and other stuff can't be freed cleanly when mgs calls
250          * server_put_mount() in error handling case (see b=17758),
251          * this problem is caused by a bug in mgs_init0, which forgot
252          * calling server_put_mount in error case.
253          */
254         if (rc < 0) {
255                 server_deregister_mount(LUSTRE_MGS_OBDNAME);
256 report_err:
257                 LCONSOLE_ERROR_MSG(0x15e,
258                                    "Failed to start MGS '%s' (%d). Is the 'mgs' module loaded?\n",
259                                    LUSTRE_MGS_OBDNAME, rc);
260         }
261         RETURN(rc);
262 }
263
264 static int server_stop_mgs(struct super_block *sb)
265 {
266         struct obd_device *obd;
267         int rc;
268         struct lustre_mount_info *lmi;
269
270         ENTRY;
271         /* Do not stop MGS if this device is not the running MGT */
272         lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
273         if (lmi && lmi->lmi_sb != sb)
274                 RETURN(0);
275
276         CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
277
278         /* There better be only one MGS */
279         obd = class_name2obd(LUSTRE_MGS_OBDNAME);
280         if (!obd) {
281                 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
282                 RETURN(-EALREADY);
283         }
284
285         /* The MGS should always stop when we say so */
286         obd->obd_force = 1;
287         rc = class_manual_cleanup(obd);
288         RETURN(rc);
289 }
290
291 /* Since there's only one mgc per node, we have to change it's fs to get
292  * access to the right disk.
293  */
294 static int server_mgc_set_fs(const struct lu_env *env,
295                              struct obd_device *mgc, struct super_block *sb)
296 {
297         struct lustre_sb_info *lsi = s2lsi(sb);
298         int rc;
299
300         ENTRY;
301         CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
302
303         /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
304         rc = obd_set_info_async(env, mgc->obd_self_export,
305                                 sizeof(KEY_SET_FS), KEY_SET_FS,
306                                 sizeof(*sb), sb, NULL);
307         if (rc != 0)
308                 CERROR("can't set_fs %d\n", rc);
309
310         RETURN(rc);
311 }
312
313 static int server_mgc_clear_fs(const struct lu_env *env,
314                                struct obd_device *mgc)
315 {
316         int rc;
317
318         ENTRY;
319         CDEBUG(D_MOUNT, "Unassign mgc disk\n");
320         rc = obd_set_info_async(env, mgc->obd_self_export,
321                                 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
322                                 0, NULL, NULL);
323         RETURN(rc);
324 }
325
326 static inline bool is_mdc_device(const char *devname)
327 {
328         char *ptr;
329
330         ptr = strrchr(devname, '-');
331         return ptr && strcmp(ptr, "-mdc") == 0;
332 }
333
334 static inline bool tgt_is_mdt(const char *tgtname, u32 *idx)
335 {
336         int type;
337
338         type = server_name2index(tgtname, idx, NULL);
339
340         return type == LDD_F_SV_TYPE_MDT;
341 }
342
343 /*
344  * Convert OST/MDT name(fsname-{MDT,OST}xxxx) to a lwp name with the @idx:yyyy
345  * (fsname-MDTyyyy-lwp-{MDT,OST}xxxx)
346  */
347 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, u32 idx)
348 {
349         char *fsname;
350         const char *tgt;
351         int rc;
352
353         ENTRY;
354         OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
355         if (fsname == NULL)
356                 RETURN(-ENOMEM);
357
358         rc = server_name2fsname(tgt_name, fsname, &tgt);
359         if (rc != 0) {
360                 CERROR("%s: failed to get fsname from tgt_name: rc = %d\n",
361                        tgt_name, rc);
362                 GOTO(cleanup, rc);
363         }
364
365         if (*tgt != '-' && *tgt != ':') {
366                 CERROR("%s: invalid tgt_name name!\n", tgt_name);
367                 GOTO(cleanup, rc = -EINVAL);
368         }
369
370         tgt++;
371         if (strncmp(tgt, "OST", 3) != 0 && strncmp(tgt, "MDT", 3) != 0) {
372                 CERROR("%s is not an OST or MDT target!\n", tgt_name);
373                 GOTO(cleanup, rc = -EINVAL);
374         }
375         snprintf(lwp_name, len, "%s-MDT%04x-%s-%s",
376                  fsname, idx, LUSTRE_LWP_NAME, tgt);
377
378         GOTO(cleanup, rc = 0);
379
380 cleanup:
381         if (fsname != NULL)
382                 OBD_FREE(fsname, MTI_NAME_MAXLEN);
383
384         return rc;
385 }
386 EXPORT_SYMBOL(tgt_name2lwp_name);
387
388 static LIST_HEAD(lwp_register_list);
389 static DEFINE_SPINLOCK(lwp_register_list_lock);
390
391 static void lustre_put_lwp_item(struct lwp_register_item *lri)
392 {
393         if (atomic_dec_and_test(&lri->lri_ref)) {
394                 LASSERT(list_empty(&lri->lri_list));
395
396                 if (*lri->lri_exp)
397                         class_export_put(*lri->lri_exp);
398                 OBD_FREE_PTR(lri);
399         }
400 }
401
402 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
403                              register_lwp_cb cb_func, void *cb_data)
404 {
405         struct obd_device *lwp;
406         struct lwp_register_item *lri;
407         bool cb = false;
408
409         ENTRY;
410         LASSERTF(strlen(lwpname) < MTI_NAME_MAXLEN, "lwpname is too long %s\n",
411                  lwpname);
412         LASSERT(exp && !*exp);
413
414         OBD_ALLOC_PTR(lri);
415         if (lri == NULL)
416                 RETURN(-ENOMEM);
417
418         lwp = class_name2obd(lwpname);
419         if (lwp && lwp->obd_set_up == 1) {
420                 struct obd_uuid *uuid;
421
422                 OBD_ALLOC_PTR(uuid);
423                 if (uuid == NULL) {
424                         OBD_FREE_PTR(lri);
425                         RETURN(-ENOMEM);
426                 }
427                 memcpy(uuid->uuid, lwpname, strlen(lwpname));
428                 *exp = obd_uuid_lookup(lwp, uuid);
429                 OBD_FREE_PTR(uuid);
430         }
431
432         memcpy(lri->lri_name, lwpname, strlen(lwpname));
433         lri->lri_exp = exp;
434         lri->lri_cb_func = cb_func;
435         lri->lri_cb_data = cb_data;
436         INIT_LIST_HEAD(&lri->lri_list);
437         /*
438          * Initialize the lri_ref at 2, one will be released before
439          * current function returned via lustre_put_lwp_item(), the
440          * other will be released in lustre_deregister_lwp_item().
441          */
442         atomic_set(&lri->lri_ref, 2);
443
444         spin_lock(&lwp_register_list_lock);
445         list_add(&lri->lri_list, &lwp_register_list);
446         if (*exp)
447                 cb = true;
448         spin_unlock(&lwp_register_list_lock);
449
450         if (cb && cb_func)
451                 cb_func(cb_data);
452         lustre_put_lwp_item(lri);
453
454         RETURN(0);
455 }
456 EXPORT_SYMBOL(lustre_register_lwp_item);
457
458 void lustre_deregister_lwp_item(struct obd_export **exp)
459 {
460         struct lwp_register_item *lri;
461         bool removed = false;
462         int repeat = 0;
463
464         spin_lock(&lwp_register_list_lock);
465         list_for_each_entry(lri, &lwp_register_list, lri_list) {
466                 if (exp == lri->lri_exp) {
467                         list_del_init(&lri->lri_list);
468                         removed = true;
469                         break;
470                 }
471         }
472         spin_unlock(&lwp_register_list_lock);
473
474         if (!removed)
475                 return;
476
477         /* See lustre_notify_lwp_list(), in some extreme race conditions,
478          * the notify callback could be still on the fly, we need to wait
479          * for the callback done before moving on to free the data used
480          * by callback.
481          */
482         while (atomic_read(&lri->lri_ref) > 1) {
483                 CDEBUG(D_MOUNT, "lri reference count %u, repeat: %d\n",
484                        atomic_read(&lri->lri_ref), repeat);
485                 repeat++;
486                 schedule_timeout_interruptible(cfs_time_seconds(1));
487         }
488         lustre_put_lwp_item(lri);
489 }
490 EXPORT_SYMBOL(lustre_deregister_lwp_item);
491
492 struct obd_export *lustre_find_lwp_by_index(const char *dev, u32 idx)
493 {
494         struct lustre_mount_info *lmi;
495         struct lustre_sb_info *lsi;
496         struct obd_device *lwp;
497         struct obd_export *exp = NULL;
498         char fsname[16];
499         char lwp_name[24];
500         int rc;
501
502         lmi = server_get_mount(dev);
503         if (lmi == NULL)
504                 return NULL;
505
506         lsi = s2lsi(lmi->lmi_sb);
507         rc = server_name2fsname(lsi->lsi_svname, fsname, NULL);
508         if (rc != 0) {
509                 CERROR("%s: failed to get fsname: rc = %d\n",
510                        lsi->lsi_svname, rc);
511                 goto err_lmi;
512         }
513
514         snprintf(lwp_name, sizeof(lwp_name), "%s-MDT%04x", fsname, idx);
515         mutex_lock(&lsi->lsi_lwp_mutex);
516         list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
517                 char *ptr = strstr(lwp->obd_name, lwp_name);
518
519                 if (ptr && lwp->obd_lwp_export) {
520                         exp = class_export_get(lwp->obd_lwp_export);
521                         break;
522                 }
523         }
524         mutex_unlock(&lsi->lsi_lwp_mutex);
525
526 err_lmi:
527         server_put_mount(dev, false);
528
529         return exp;
530 }
531 EXPORT_SYMBOL(lustre_find_lwp_by_index);
532
533 void lustre_notify_lwp_list(struct obd_export *exp)
534 {
535         struct lwp_register_item *lri;
536
537         LASSERT(exp);
538 again:
539         spin_lock(&lwp_register_list_lock);
540         list_for_each_entry(lri, &lwp_register_list, lri_list) {
541                 if (strcmp(exp->exp_obd->obd_name, lri->lri_name))
542                         continue;
543                 if (*lri->lri_exp)
544                         continue;
545                 *lri->lri_exp = class_export_get(exp);
546                 if (!lri->lri_cb_func)
547                         continue;
548                 atomic_inc(&lri->lri_ref);
549                 spin_unlock(&lwp_register_list_lock);
550
551                 lri->lri_cb_func(lri->lri_cb_data);
552                 lustre_put_lwp_item(lri);
553
554                 /* Others may have changed the list after we unlock, we have
555                  * to rescan the list from the beginning. Usually, the list
556                  * 'lwp_register_list' is very short, and there is 'guard'
557                  * lri::lri_exp that will prevent the callback to be done
558                  * repeatedly. So rescanning the list has no problem.
559                  */
560                 goto again;
561         }
562         spin_unlock(&lwp_register_list_lock);
563 }
564 EXPORT_SYMBOL(lustre_notify_lwp_list);
565
566 static int lustre_lwp_connect(struct obd_device *lwp, bool is_mdt)
567 {
568         struct lu_env env;
569         struct lu_context session_ctx;
570         struct obd_export *exp;
571         struct obd_uuid *uuid = NULL;
572         struct obd_connect_data *data = NULL;
573         int rc;
574
575         ENTRY;
576         /* log has been fully processed, let clients connect */
577         rc = lu_env_init(&env, lwp->obd_lu_dev->ld_type->ldt_ctx_tags);
578         if (rc != 0)
579                 RETURN(rc);
580
581         lu_context_init(&session_ctx, LCT_SERVER_SESSION);
582         session_ctx.lc_thread = NULL;
583         lu_context_enter(&session_ctx);
584         env.le_ses = &session_ctx;
585
586         OBD_ALLOC_PTR(data);
587         if (data == NULL)
588                 GOTO(out, rc = -ENOMEM);
589
590         data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
591         data->ocd_version = LUSTRE_VERSION_CODE;
592         data->ocd_connect_flags |= OBD_CONNECT_FID | OBD_CONNECT_AT |
593                 OBD_CONNECT_LRU_RESIZE | OBD_CONNECT_FULL20 |
594                 OBD_CONNECT_LVB_TYPE | OBD_CONNECT_LIGHTWEIGHT |
595                 OBD_CONNECT_LFSCK | OBD_CONNECT_BULK_MBITS;
596
597         if (is_mdt)
598                 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
599
600         OBD_ALLOC_PTR(uuid);
601         if (uuid == NULL)
602                 GOTO(out, rc = -ENOMEM);
603
604         if (strlen(lwp->obd_name) > sizeof(uuid->uuid)) {
605                 CERROR("%s: Too long lwp name %s, max_size is %d\n",
606                        lwp->obd_name, lwp->obd_name, (int)sizeof(uuid->uuid));
607                 GOTO(out, rc = -EINVAL);
608         }
609
610         /* Use lwp name as the uuid, so we find the export by lwp name later */
611         memcpy(uuid->uuid, lwp->obd_name, strlen(lwp->obd_name));
612         rc = obd_connect(&env, &exp, lwp, uuid, data, NULL);
613         if (rc != 0) {
614                 CERROR("%s: connect failed: rc = %d\n", lwp->obd_name, rc);
615         } else {
616                 if (unlikely(lwp->obd_lwp_export))
617                         class_export_put(lwp->obd_lwp_export);
618                 lwp->obd_lwp_export = class_export_get(exp);
619         }
620
621         GOTO(out, rc);
622
623 out:
624         if (data != NULL)
625                 OBD_FREE_PTR(data);
626         if (uuid != NULL)
627                 OBD_FREE_PTR(uuid);
628
629         lu_env_fini(&env);
630         lu_context_exit(&session_ctx);
631         lu_context_fini(&session_ctx);
632
633         return rc;
634 }
635
636 /**
637  * lwp is used by slaves (Non-MDT0 targets) to manage the connection to MDT0,
638  * or from the OSTx to MDTy.
639  **/
640 static int lustre_lwp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi,
641                             u32 idx)
642 {
643         struct obd_device *obd;
644         char *lwpname = NULL;
645         char *lwpuuid = NULL;
646         struct lnet_nid nid;
647         int rc;
648
649         ENTRY;
650         if (lcfg->lcfg_nid)
651                 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
652         else {
653                 rc = libcfs_strnid(&nid, lustre_cfg_string(lcfg, 2));
654                 if (rc)
655                         RETURN(rc);
656         }
657         rc = class_add_uuid(lustre_cfg_string(lcfg, 1), &nid);
658         if (rc != 0) {
659                 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
660                 RETURN(rc);
661         }
662
663         OBD_ALLOC(lwpname, MTI_NAME_MAXLEN);
664         if (lwpname == NULL)
665                 GOTO(out, rc = -ENOMEM);
666
667         rc = tgt_name2lwp_name(lsi->lsi_svname, lwpname, MTI_NAME_MAXLEN, idx);
668         if (rc != 0) {
669                 CERROR("%s: failed to generate lwp name: rc = %d\n",
670                        lsi->lsi_svname, rc);
671                 GOTO(out, rc);
672         }
673
674         OBD_ALLOC(lwpuuid, MTI_NAME_MAXLEN);
675         if (!lwpuuid)
676                 GOTO(out, rc = -ENOMEM);
677
678         sprintf(lwpuuid, "%s_UUID", lwpname);
679         rc = lustre_start_simple(lwpname, LUSTRE_LWP_NAME,
680                                  lwpuuid, lustre_cfg_string(lcfg, 1),
681                                  NULL, NULL, NULL);
682         if (rc < 0) {
683                 CERROR("%s: setup up failed: rc %d\n", lwpname, rc);
684                 GOTO(out, rc);
685         }
686
687         obd = class_name2obd(lwpname);
688         LASSERT(obd);
689
690         rc = lustre_lwp_connect(obd, strstr(lsi->lsi_svname, "-MDT") != NULL);
691         if (rc < 0) {
692                 CERROR("%s: connect failed: rc = %d\n", lwpname, rc);
693                 GOTO(out, rc);
694         }
695
696         obd->u.cli.cl_max_mds_easize = MAX_MD_SIZE;
697         mutex_lock(&lsi->lsi_lwp_mutex);
698         list_add_tail(&obd->obd_lwp_list, &lsi->lsi_lwp_list);
699         mutex_unlock(&lsi->lsi_lwp_mutex);
700 out:
701         if (lwpname)
702                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
703         if (lwpuuid)
704                 OBD_FREE(lwpuuid, MTI_NAME_MAXLEN);
705
706         return rc;
707 }
708
709 /* the caller is responsible for memory free */
710 static struct obd_device *lustre_find_lwp(struct lustre_sb_info *lsi,
711                                           char **lwpname, u32 idx)
712 {
713         struct obd_device *lwp;
714         int rc = 0;
715
716         ENTRY;
717         LASSERT(lwpname);
718         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
719
720         OBD_ALLOC(*lwpname, MTI_NAME_MAXLEN);
721         if (*lwpname == NULL)
722                 RETURN(ERR_PTR(-ENOMEM));
723
724         rc = tgt_name2lwp_name(lsi->lsi_svname, *lwpname, MTI_NAME_MAXLEN, idx);
725         if (rc != 0) {
726                 CERROR("%s: failed to generate lwp name: rc = %d\n",
727                        lsi->lsi_svname, rc);
728                 GOTO(out, rc = -EINVAL);
729         }
730
731         lwp = class_name2obd(*lwpname);
732
733 out:
734         if (rc != 0) {
735                 if (*lwpname != NULL) {
736                         OBD_FREE(*lwpname, MTI_NAME_MAXLEN);
737                         *lwpname = NULL;
738                 }
739                 lwp = ERR_PTR(rc);
740         }
741
742         RETURN(lwp ? lwp : ERR_PTR(-ENOENT));
743 }
744
745 static int lustre_lwp_add_conn(struct lustre_cfg *cfg,
746                                struct lustre_sb_info *lsi, u32 idx)
747 {
748         struct lustre_cfg_bufs *bufs = NULL;
749         struct lustre_cfg *lcfg = NULL;
750         char *lwpname = NULL;
751         struct obd_device *lwp;
752         int rc;
753
754         ENTRY;
755         lwp = lustre_find_lwp(lsi, &lwpname, idx);
756         if (IS_ERR(lwp)) {
757                 CERROR("%s: can't find lwp device.\n", lsi->lsi_svname);
758                 GOTO(out, rc = PTR_ERR(lwp));
759         }
760         LASSERT(lwpname);
761
762         OBD_ALLOC_PTR(bufs);
763         if (bufs == NULL)
764                 GOTO(out, rc = -ENOMEM);
765
766         lustre_cfg_bufs_reset(bufs, lwpname);
767         lustre_cfg_bufs_set_string(bufs, 1,
768                                    lustre_cfg_string(cfg, 1));
769
770         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
771         if (!lcfg)
772                 GOTO(out_cfg, rc = -ENOMEM);
773         lustre_cfg_init(lcfg, LCFG_ADD_CONN, bufs);
774
775         rc = class_add_conn(lwp, lcfg);
776         if (rc < 0)
777                 CERROR("%s: can't add conn: rc = %d\n", lwpname, rc);
778
779         if (lcfg)
780                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
781                                               lcfg->lcfg_buflens));
782 out_cfg:
783         if (bufs)
784                 OBD_FREE_PTR(bufs);
785 out:
786         if (lwpname)
787                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
788         RETURN(rc);
789 }
790
791 /**
792  * Retrieve MDT nids from the client log, then start the lwp device.
793  * there are only two scenarios which would include mdt nid.
794  * 1.
795  * marker   5 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxx-
796  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:192.168.122.162@tcp
797  * attach    0:lustre-MDTyyyy-mdc  1:mdc  2:lustre-clilmv_UUID
798  * setup     0:lustre-MDTyyyy-mdc  1:lustre-MDTyyyy_UUID  2:192.168.122.162@tcp
799  * add_uuid  nid=192.168.172.1@tcp(0x20000c0a8ac01)  0:  1:192.168.172.1@tcp
800  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.172.1@tcp
801  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDTyyyy_UUID xxxx
802  * marker   5 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxxx-
803  * 2.
804  * marker   7 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
805  * add_uuid  nid=192.168.122.2@tcp(0x20000c0a87a02)  0:  1:192.168.122.2@tcp
806  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.122.2@tcp
807  * marker   7 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
808  **/
809 static int client_lwp_config_process(const struct lu_env *env,
810                                      struct llog_handle *handle,
811                                      struct llog_rec_hdr *rec, void *data)
812 {
813         struct config_llog_instance *cfg = data;
814         int cfg_len = rec->lrh_len;
815         char *cfg_buf = (char *) (rec + 1);
816         struct lustre_cfg *lcfg = NULL;
817         struct lustre_sb_info *lsi;
818         int rc = 0, swab = 0;
819
820         ENTRY;
821         if (rec->lrh_type != OBD_CFG_REC) {
822                 CERROR("Unknown llog record type %#x encountered\n",
823                        rec->lrh_type);
824                 RETURN(-EINVAL);
825         }
826
827         if (!cfg->cfg_sb)
828                 GOTO(out, rc = -EINVAL);
829         lsi = s2lsi(cfg->cfg_sb);
830
831         lcfg = (struct lustre_cfg *)cfg_buf;
832         if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
833                 lustre_swab_lustre_cfg(lcfg);
834                 swab = 1;
835         }
836
837         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
838         if (rc < 0)
839                 GOTO(out, rc);
840
841         switch (lcfg->lcfg_command) {
842         case LCFG_MARKER: {
843                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
844
845                 lustre_swab_cfg_marker(marker, swab,
846                                        LUSTRE_CFG_BUFLEN(lcfg, 1));
847                 if (marker->cm_flags & CM_SKIP ||
848                     marker->cm_flags & CM_EXCLUDE)
849                         GOTO(out, rc = 0);
850
851                 if (!tgt_is_mdt(marker->cm_tgtname, &cfg->cfg_lwp_idx))
852                         GOTO(out, rc = 0);
853
854                 if (IS_MDT(lsi) && cfg->cfg_lwp_idx != 0)
855                         GOTO(out, rc = 0);
856
857                 if (!strncmp(marker->cm_comment, "add mdc", 7) ||
858                     !strncmp(marker->cm_comment, "add failnid", 11)) {
859                         if (marker->cm_flags & CM_START) {
860                                 cfg->cfg_flags = CFG_F_MARKER;
861                                 /* This hack is to differentiate the
862                                  * ADD_UUID is come from "add mdc" record
863                                  * or from "add failnid" record.
864                                  */
865                                 if (!strncmp(marker->cm_comment,
866                                              "add failnid", 11))
867                                         cfg->cfg_flags |= CFG_F_SKIP;
868                         } else if (marker->cm_flags & CM_END) {
869                                 cfg->cfg_flags = 0;
870                         }
871                 }
872                 break;
873         }
874         case LCFG_ADD_UUID: {
875                 if (cfg->cfg_flags == CFG_F_MARKER) {
876                         rc = lustre_lwp_setup(lcfg, lsi, cfg->cfg_lwp_idx);
877                         /* XXX: process only the first nid as
878                          * we don't need another instance of lwp
879                          */
880                         cfg->cfg_flags |= CFG_F_SKIP;
881                 } else if (cfg->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
882                         struct lnet_nid nid;
883
884                         rc = 0;
885                         if (lcfg->lcfg_nid)
886                                 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
887                         else
888                                 rc = libcfs_strnid(&nid,
889                                                    lustre_cfg_string(lcfg, 2));
890                         if (!rc)
891                                 rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
892                                                     &nid);
893                         if (rc < 0)
894                                 CERROR("%s: Fail to add uuid, rc:%d\n",
895                                        lsi->lsi_svname, rc);
896                 }
897                 break;
898         }
899         case LCFG_ADD_CONN: {
900                 char *devname = lustre_cfg_string(lcfg, 0);
901                 char *ptr;
902                 u32 idx = 0;
903
904                 if (!is_mdc_device(devname))
905                         break;
906
907                 if (!(cfg->cfg_flags & CFG_F_MARKER)) {
908                         CDEBUG(D_CONFIG, "Skipping add_conn for %s, rec %d\n",
909                                devname, rec->lrh_index);
910                         break;
911                 }
912
913                 /* add_conn should follow by add_uuid. This
914                  * guarantee lwp device was created
915                  */
916                 if (!(cfg->cfg_flags & CFG_F_SKIP)) {
917                         CWARN("Error at config for %s rec %d, add_conn should follow by add_uuid\n",
918                               devname, rec->lrh_index);
919                         break;
920                 }
921                 ptr = strrchr(devname, '-');
922                 if (ptr == NULL)
923                         break;
924
925                 *ptr = 0;
926                 if (!tgt_is_mdt(devname, &idx)) {
927                         *ptr = '-';
928                         break;
929                 }
930                 *ptr = '-';
931
932                 if (IS_MDT(lsi) && idx != 0)
933                         break;
934
935                 rc = lustre_lwp_add_conn(lcfg, lsi, idx);
936                 break;
937         }
938         default:
939                 break;
940         }
941 out:
942         RETURN(rc);
943 }
944
945 static int lustre_disconnect_lwp(struct super_block *sb)
946 {
947         struct lustre_sb_info *lsi = s2lsi(sb);
948         struct obd_device *lwp;
949         char *logname = NULL;
950         struct lustre_cfg_bufs *bufs = NULL;
951         struct config_llog_instance *cfg = NULL;
952         int rc = 0;
953         int rc1 = 0;
954
955         ENTRY;
956         if (likely(lsi->lsi_lwp_started)) {
957                 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
958                 if (logname == NULL)
959                         RETURN(-ENOMEM);
960
961                 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
962                 if (rc != 0) {
963                         CERROR("%s: failed to get fsname from svname: rc = %d\n",
964                                lsi->lsi_svname, rc);
965                         GOTO(out, rc = -EINVAL);
966                 }
967
968                 strcat(logname, "-client");
969                 OBD_ALLOC_PTR(cfg);
970                 if (cfg == NULL)
971                         GOTO(out, rc = -ENOMEM);
972
973                 /* end log first */
974                 cfg->cfg_instance = ll_get_cfg_instance(sb);
975                 rc = lustre_end_log(sb, logname, cfg);
976                 if (rc != 0 && rc != -ENOENT)
977                         GOTO(out, rc);
978
979                 lsi->lsi_lwp_started = 0;
980         }
981
982         OBD_ALLOC_PTR(bufs);
983         if (bufs == NULL)
984                 GOTO(out, rc = -ENOMEM);
985
986         mutex_lock(&lsi->lsi_lwp_mutex);
987         list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
988                 struct lustre_cfg *lcfg;
989
990                 if (likely(lwp->obd_lwp_export)) {
991                         class_export_put(lwp->obd_lwp_export);
992                         lwp->obd_lwp_export = NULL;
993                 }
994
995                 lustre_cfg_bufs_reset(bufs, lwp->obd_name);
996                 lustre_cfg_bufs_set_string(bufs, 1, NULL);
997                 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount,
998                                                bufs->lcfg_buflen));
999                 if (!lcfg) {
1000                         rc = -ENOMEM;
1001                         break;
1002                 }
1003                 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
1004
1005                 /* Disconnect import first. NULL is passed for the '@env',
1006                  * since it will not be used.
1007                  */
1008                 rc = lwp->obd_lu_dev->ld_ops->ldo_process_config(NULL,
1009                                                         lwp->obd_lu_dev, lcfg);
1010                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1011                                               lcfg->lcfg_buflens));
1012                 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
1013                     rc != -ENOTCONN && rc != -ESHUTDOWN) {
1014                         CERROR("%s: fail to disconnect LWP: rc = %d\n",
1015                                lwp->obd_name, rc);
1016                         rc1 = rc;
1017                 }
1018         }
1019         mutex_unlock(&lsi->lsi_lwp_mutex);
1020
1021         GOTO(out, rc);
1022
1023 out:
1024         if (bufs)
1025                 OBD_FREE_PTR(bufs);
1026         if (cfg)
1027                 OBD_FREE_PTR(cfg);
1028         if (logname)
1029                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1030
1031         return rc1 != 0 ? rc1 : rc;
1032 }
1033
1034 /**
1035  * Stop the lwp for an OST/MDT target.
1036  **/
1037 static int lustre_stop_lwp(struct super_block *sb)
1038 {
1039         struct lustre_sb_info *lsi = s2lsi(sb);
1040         struct obd_device *lwp;
1041         int rc = 0;
1042         int rc1 = 0;
1043
1044         ENTRY;
1045         mutex_lock(&lsi->lsi_lwp_mutex);
1046         while (!list_empty(&lsi->lsi_lwp_list)) {
1047                 lwp = list_first_entry(&lsi->lsi_lwp_list, struct obd_device,
1048                                        obd_lwp_list);
1049                 list_del_init(&lwp->obd_lwp_list);
1050                 lwp->obd_force = 1;
1051                 mutex_unlock(&lsi->lsi_lwp_mutex);
1052
1053                 rc = class_manual_cleanup(lwp);
1054                 if (rc != 0) {
1055                         CERROR("%s: fail to stop LWP: rc = %d\n",
1056                                lwp->obd_name, rc);
1057                         rc1 = rc;
1058                 }
1059                 mutex_lock(&lsi->lsi_lwp_mutex);
1060         }
1061         mutex_unlock(&lsi->lsi_lwp_mutex);
1062
1063         RETURN(rc1 != 0 ? rc1 : rc);
1064 }
1065
1066 /**
1067  * Start the lwp(fsname-MDTyyyy-lwp-{MDT,OST}xxxx) for a MDT/OST or MDT target.
1068  **/
1069 static int lustre_start_lwp(struct super_block *sb)
1070 {
1071         struct lustre_sb_info *lsi = s2lsi(sb);
1072         struct config_llog_instance *cfg = NULL;
1073         char *logname;
1074         int rc;
1075
1076         ENTRY;
1077         if (unlikely(lsi->lsi_lwp_started))
1078                 RETURN(0);
1079
1080         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1081         if (logname == NULL)
1082                 RETURN(-ENOMEM);
1083
1084         rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
1085         if (rc != 0) {
1086                 CERROR("%s: failed to get fsname from svname: rc = %d\n",
1087                        lsi->lsi_svname, rc);
1088                 GOTO(out, rc = -EINVAL);
1089         }
1090
1091         strcat(logname, "-client");
1092         OBD_ALLOC_PTR(cfg);
1093         if (cfg == NULL)
1094                 GOTO(out, rc = -ENOMEM);
1095
1096         cfg->cfg_callback = client_lwp_config_process;
1097         cfg->cfg_instance = ll_get_cfg_instance(sb);
1098         rc = lustre_process_log(sb, logname, cfg);
1099         /* need to remove config llog from mgc */
1100         lsi->lsi_lwp_started = 1;
1101
1102         GOTO(out, rc);
1103
1104 out:
1105         OBD_FREE(logname, MTI_NAME_MAXLEN);
1106         if (cfg)
1107                 OBD_FREE_PTR(cfg);
1108
1109         return rc;
1110 }
1111
1112 static DEFINE_MUTEX(server_start_lock);
1113
1114 /* Stop MDS/OSS if nobody is using them */
1115 static int server_stop_servers(int lsiflags)
1116 {
1117         struct obd_device *obd = NULL;
1118         struct obd_type *type = NULL;
1119         int rc = 0;
1120         bool type_last;
1121
1122         ENTRY;
1123         mutex_lock(&server_start_lock);
1124
1125         /* Either an MDT or an OST or neither  */
1126         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1127         if (lsiflags & LDD_F_SV_TYPE_MDT) {
1128                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1129                 type = class_search_type(LUSTRE_MDT_NAME);
1130         } else if (lsiflags & LDD_F_SV_TYPE_OST) {
1131         /* if this was an OST, and there are no more OST's, clean up the OSS */
1132                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1133                 type = class_search_type(LUSTRE_OST_NAME);
1134         }
1135
1136         /* server_stop_servers is a pair of server_start_targets
1137          * Here we put type which was taken at server_start_targets.
1138          * If type is NULL then there is a wrong logic around type or
1139          * type reference.
1140          */
1141         LASSERTF(type, "Server flags %d, obd %s\n", lsiflags,
1142                  obd ? obd->obd_name : "NULL");
1143
1144         type_last = (atomic_read(&type->typ_refcnt) == 1);
1145
1146         class_put_type(type);
1147         if (obd && type_last) {
1148                 obd->obd_force = 1;
1149                 /* obd_fail doesn't mean much on a server obd */
1150                 rc = class_manual_cleanup(obd);
1151         }
1152
1153         /* put reference taken by class_search_type */
1154         kobject_put(&type->typ_kobj);
1155
1156         mutex_unlock(&server_start_lock);
1157
1158         RETURN(rc);
1159 }
1160
1161 int server_mti_print(const char *title, struct mgs_target_info *mti)
1162 {
1163         CDEBUG(D_MOUNT, "mti - %s\n", title);
1164         CDEBUG(D_MOUNT, "server: %s\n", mti->mti_svname);
1165         CDEBUG(D_MOUNT, "fs:     %s\n", mti->mti_fsname);
1166         CDEBUG(D_MOUNT, "uuid:   %s\n", mti->mti_uuid);
1167         CDEBUG(D_MOUNT, "ver:    %d\n", mti->mti_config_ver);
1168         CDEBUG(D_MOUNT, "flags:\n");
1169         if (mti->mti_flags & LDD_F_SV_TYPE_MDT)
1170                 CDEBUG(D_MOUNT, "        LDD_F_SV_TYPE_MDT\n");
1171         if (mti->mti_flags & LDD_F_SV_TYPE_OST)
1172                 CDEBUG(D_MOUNT, "        LDD_F_SV_TYPE_OST\n");
1173         if (mti->mti_flags & LDD_F_SV_TYPE_MGS)
1174                 CDEBUG(D_MOUNT, "        LDD_F_SV_TYPE_MGS\n");
1175         if (mti->mti_flags & LDD_F_SV_ALL)
1176                 CDEBUG(D_MOUNT, "        LDD_F_SV_ALL\n");
1177         if (mti->mti_flags & LDD_F_NEED_INDEX)
1178                 CDEBUG(D_MOUNT, "        LDD_F_NEED_INDEX\n");
1179         if (mti->mti_flags & LDD_F_VIRGIN)
1180                 CDEBUG(D_MOUNT, "        LDD_F_VIRIGIN\n");
1181         if (mti->mti_flags & LDD_F_UPDATE)
1182                 CDEBUG(D_MOUNT, "        LDD_F_UPDATE\n");
1183         if (mti->mti_flags & LDD_F_REWRITE_LDD)
1184                 CDEBUG(D_MOUNT, "        LDD_F_REWRITE_LDD\n");
1185         if (mti->mti_flags & LDD_F_WRITECONF)
1186                 CDEBUG(D_MOUNT, "        LDD_F_WRITECONF\n");
1187         if (mti->mti_flags & LDD_F_PARAM)
1188                 CDEBUG(D_MOUNT, "        LDD_F_PARAM\n");
1189         if (mti->mti_flags & LDD_F_NO_PRIMNODE)
1190                 CDEBUG(D_MOUNT, "        LDD_F_NO_PRIMNODE\n");
1191         if (mti->mti_flags & LDD_F_IR_CAPABLE)
1192                 CDEBUG(D_MOUNT, "        LDD_F_IR_CAPABLE\n");
1193         if (mti->mti_flags & LDD_F_ERROR)
1194                 CDEBUG(D_MOUNT, "        LDD_F_ERROR\n");
1195         if (mti->mti_flags & LDD_F_PARAM2)
1196                 CDEBUG(D_MOUNT, "        LDD_F_PARAM2\n");
1197         if (mti->mti_flags & LDD_F_NO_LOCAL_LOGS)
1198                 CDEBUG(D_MOUNT, "        LDD_F_NO_LOCAL_LOGS\n");
1199
1200         /* Upper 16 bits for target registering */
1201         if (target_supports_large_nid(mti))
1202                 CDEBUG(D_MOUNT, "        LDD_F_LARGE_NID\n");
1203         if (mti->mti_flags & LDD_F_OPC_REG)
1204                 CDEBUG(D_MOUNT, "        LDD_F_OPC_REG\n");
1205         if (mti->mti_flags & LDD_F_OPC_UNREG)
1206                 CDEBUG(D_MOUNT, "        LDD_F_OPC_UNREG\n");
1207         if (mti->mti_flags & LDD_F_OPC_READY)
1208                 CDEBUG(D_MOUNT, "        LDD_F_OPC_READY\n");
1209
1210         return 0;
1211 }
1212 EXPORT_SYMBOL(server_mti_print);
1213
1214 /* Generate data for registration */
1215 static struct mgs_target_info *server_lsi2mti(struct lustre_sb_info *lsi)
1216 {
1217         size_t len = offsetof(struct mgs_target_info, mti_nidlist);
1218         GENRADIX(struct lnet_processid) plist;
1219         struct lnet_processid id, *tmp;
1220         struct mgs_target_info *mti;
1221         bool large_nid = false;
1222         int nid_count = 0;
1223         int rc, i = 0;
1224         int cplen = 0;
1225
1226         ENTRY;
1227         if (!IS_SERVER(lsi))
1228                 RETURN(ERR_PTR(-EINVAL));
1229
1230         if (exp_connect_flags2(lsi->lsi_mgc->u.cli.cl_mgc_mgsexp) &
1231             OBD_CONNECT2_LARGE_NID)
1232                 large_nid = true;
1233
1234         genradix_init(&plist);
1235
1236         while (LNetGetId(i++, &id, large_nid) != -ENOENT) {
1237                 if (nid_is_lo0(&id.nid))
1238                         continue;
1239
1240                 /* server use --servicenode param, only allow specified
1241                  * nids be registered
1242                  */
1243                 if (test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) &&
1244                     class_match_nid(lsi->lsi_lmd->lmd_params,
1245                                     PARAM_FAILNODE, &id.nid) < 1)
1246                         continue;
1247
1248                 if (!class_find_param(lsi->lsi_lmd->lmd_params,
1249                                         PARAM_NETWORK, NULL)) {
1250                         if (LNetGetPeerDiscoveryStatus()) {
1251                                 CERROR("LNet Dynamic Peer Discovery is enabled"
1252                                        " on this node. 'network' option used in"
1253                                        " mkfs.lustre cannot be taken into"
1254                                        " account.\n");
1255                                 GOTO(free_list, mti = ERR_PTR(-EINVAL));
1256                         }
1257                 }
1258
1259                 /* match specified network */
1260                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1261                                      PARAM_NETWORK, LNET_NID_NET(&id.nid)))
1262                         continue;
1263
1264                 tmp = genradix_ptr_alloc(&plist, nid_count++, GFP_KERNEL);
1265                 if (!tmp)
1266                         GOTO(free_list, mti = ERR_PTR(-ENOMEM));
1267
1268                 if (large_nid)
1269                         len += LNET_NIDSTR_SIZE;
1270                 *tmp = id;
1271         }
1272
1273         if (nid_count == 0) {
1274                 CERROR("Failed to get NID for server %s, please check whether the target is specifed with improper --servicenode or --network options.\n",
1275                        lsi->lsi_svname);
1276                 GOTO(free_list, mti = ERR_PTR(-EINVAL));
1277         }
1278
1279         OBD_ALLOC(mti, len);
1280         if (!mti)
1281                 GOTO(free_list, mti = ERR_PTR(-ENOMEM));
1282
1283         rc = strscpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname));
1284         if (rc < 0)
1285                 GOTO(free_mti, rc);
1286
1287         mti->mti_nid_count = nid_count;
1288         for (i = 0; i < mti->mti_nid_count; i++) {
1289                 tmp = genradix_ptr(&plist, i);
1290
1291                 if (large_nid)
1292                         libcfs_nidstr_r(&tmp->nid, mti->mti_nidlist[i],
1293                                         sizeof(mti->mti_nidlist[i]));
1294                 else
1295                         mti->mti_nids[i] = lnet_nid_to_nid4(&tmp->nid);
1296         }
1297         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1298         mti->mti_config_ver = 0;
1299
1300         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1301         if (rc < 0)
1302                 GOTO(free_mti, rc);
1303
1304         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1305         if (rc < 0)
1306                 GOTO(free_mti, rc);
1307
1308         /* Orion requires index to be set */
1309         LASSERT(!(rc & LDD_F_NEED_INDEX));
1310         /* keep only LDD flags */
1311         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1312         if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1313                 mti->mti_flags |= LDD_F_UPDATE;
1314         /* use NID strings instead */
1315         if (large_nid)
1316                 mti->mti_flags |= LDD_F_LARGE_NID;
1317         cplen = strscpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1318                         sizeof(mti->mti_params));
1319         if (cplen >= sizeof(mti->mti_params))
1320                 rc = -E2BIG;
1321 free_mti:
1322         if (rc < 0) {
1323                 OBD_FREE(mti, len);
1324                 mti = ERR_PTR(rc);
1325         }
1326 free_list:
1327         genradix_free(&plist);
1328
1329         return mti;
1330 }
1331
1332 /* Register an old or new target with the MGS. If needed MGS will construct
1333  * startup logs and assign index
1334  */
1335 static int server_register_target(struct lustre_sb_info *lsi)
1336 {
1337         struct obd_device *mgc = lsi->lsi_mgc;
1338         struct mgs_target_info *mti = NULL;
1339         size_t mti_len = sizeof(*mti);
1340         bool must_succeed;
1341         int rc;
1342         int tried = 0;
1343
1344         ENTRY;
1345         LASSERT(mgc);
1346         mti = server_lsi2mti(lsi);
1347         if (IS_ERR(mti))
1348                 GOTO(out, rc = PTR_ERR(mti));
1349
1350         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1351                mti->mti_svname, mti->mti_fsname, mti->mti_nidlist[0],
1352                mti->mti_stripe_index, mti->mti_flags);
1353
1354         /* we cannot ignore registration failure if MGS logs must be updated. */
1355         must_succeed = !!(lsi->lsi_flags &
1356                     (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_WRITECONF |
1357                      LDD_F_VIRGIN));
1358         mti->mti_flags |= LDD_F_OPC_REG;
1359         if (target_supports_large_nid(mti))
1360                 mti_len += mti->mti_nid_count * LNET_NIDSTR_SIZE;
1361         server_mti_print("server_register_target", mti);
1362 again:
1363         /* Register the target */
1364         /* FIXME use mgc_process_config instead */
1365         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1366                                 sizeof(KEY_REGISTER_TARGET),
1367                                 KEY_REGISTER_TARGET,
1368                                 mti_len, mti, NULL);
1369         if (rc < 0) {
1370                 if (mti->mti_flags & LDD_F_ERROR) {
1371                         LCONSOLE_ERROR_MSG(0x160,
1372                                            "%s: the MGS refuses to allow this server to start: rc = %d. Please see messages on the MGS.\n",
1373                                            lsi->lsi_svname, rc);
1374                 } else if (must_succeed) {
1375                         if ((rc == -ESHUTDOWN || rc == -EIO) && ++tried < 5) {
1376                                 /* The connection with MGS is not established.
1377                                  * Try again after 2 seconds. Interruptable.
1378                                  */
1379                                 schedule_timeout_interruptible(cfs_time_seconds(2));
1380                                 if (!signal_pending(current))
1381                                         goto again;
1382                         }
1383
1384                         LCONSOLE_ERROR_MSG(0x15f,
1385                                            "%s: cannot register this server with the MGS: rc = %d. Is the MGS running?\n",
1386                                            lsi->lsi_svname, rc);
1387                 } else {
1388                         CDEBUG(D_HA,
1389                                "%s: error registering with the MGS: rc = %d (not fatal)\n",
1390                                lsi->lsi_svname, rc);
1391                         /* reset the error code for non-fatal error. */
1392                         rc = 0;
1393                 }
1394         }
1395
1396         OBD_FREE(mti, mti_len);
1397 out:
1398         RETURN(rc);
1399 }
1400
1401 /**
1402  * Notify the MGS that this target is ready.
1403  * Used by IR - if the MGS receives this message, it will notify clients.
1404  */
1405 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1406 {
1407         struct lustre_sb_info *lsi = s2lsi(sb);
1408         struct obd_device *mgc = lsi->lsi_mgc;
1409         struct mgs_target_info *mti = NULL;
1410         size_t mti_len = sizeof(*mti);
1411         int rc;
1412
1413         ENTRY;
1414         LASSERT(mgc);
1415         mti = server_lsi2mti(lsi);
1416         if (IS_ERR(mti))
1417                 GOTO(out, rc = PTR_ERR(mti));
1418
1419         mti->mti_instance = obd2obt(obd)->obt_instance;
1420         mti->mti_flags |= LDD_F_OPC_READY;
1421         if (target_supports_large_nid(mti))
1422                 mti_len += mti->mti_nid_count * LNET_NIDSTR_SIZE;
1423         server_mti_print("server_notify_target", mti);
1424
1425         /* FIXME use mgc_process_config instead */
1426         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1427                                 sizeof(KEY_REGISTER_TARGET),
1428                                 KEY_REGISTER_TARGET,
1429                                 mti_len, mti, NULL);
1430
1431         /* Imperative recovery: if the mgs informs us to use IR? */
1432         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1433             (mti->mti_flags & LDD_F_IR_CAPABLE))
1434                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1435
1436         OBD_FREE(mti, mti_len);
1437 out:
1438         RETURN(rc);
1439 }
1440
1441 /* Start server targets: MDTs and OSTs */
1442 static int server_start_targets(struct super_block *sb)
1443 {
1444         struct obd_device *obd;
1445         struct lustre_sb_info *lsi = s2lsi(sb);
1446         struct config_llog_instance cfg;
1447         struct lu_env mgc_env;
1448         struct lu_device *dev;
1449         char *name_service, *obd_name_service = NULL;
1450         struct obd_type *type = NULL;
1451         int rc;
1452
1453         ENTRY;
1454         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1455
1456         LASSERTF(IS_MDT(lsi) || IS_OST(lsi), "designed for MDT or OST only\n");
1457
1458         if (IS_MDT(lsi)) {
1459                 obd_name_service = LUSTRE_MDS_OBDNAME;
1460                 name_service = LUSTRE_MDS_NAME;
1461         } else {
1462                 obd_name_service = LUSTRE_OSS_OBDNAME;
1463                 name_service = LUSTRE_OSS_NAME;
1464         }
1465
1466         /* make sure MDS/OSS is started */
1467         mutex_lock(&server_start_lock);
1468         obd = class_name2obd(obd_name_service);
1469         if (!obd) {
1470                 rc = lustre_start_simple(obd_name_service, name_service,
1471                                          (IS_MDT(lsi) ?
1472                                           LUSTRE_MDS_OBDNAME"_uuid" :
1473                                           LUSTRE_OSS_OBDNAME"_uuid"),
1474                                          NULL, NULL, NULL, NULL);
1475                 if (rc < 0) {
1476                         mutex_unlock(&server_start_lock);
1477                         CERROR("failed to start %s: %d\n",
1478                                obd_name_service, rc);
1479                         RETURN(rc);
1480                 }
1481         }
1482         /* hold a type reference and put it at server_stop_servers */
1483         type = class_get_type(IS_MDT(lsi) ?
1484                               LUSTRE_MDT_NAME : LUSTRE_OST_NAME);
1485         if (!type) {
1486                 mutex_unlock(&server_start_lock);
1487                 GOTO(out_stop_service, rc = -ENODEV);
1488         }
1489         lsi->lsi_server_started = 1;
1490         mutex_unlock(&server_start_lock);
1491         if (CFS_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE) &&
1492             IS_MDT(lsi)) {
1493                 CFS_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1494                 msleep(2 * MSEC_PER_SEC);
1495         }
1496
1497         rc = lu_env_init(&mgc_env, LCT_MG_THREAD);
1498         if (rc != 0)
1499                 GOTO(out_stop_service, rc);
1500
1501         /* Set the mgc fs to our server disk.  This allows the MGC to
1502          * read and write configs locally, in case it can't talk to the MGS.
1503          */
1504         rc = server_mgc_set_fs(&mgc_env, lsi->lsi_mgc, sb);
1505         if (rc < 0)
1506                 GOTO(out_env, rc);
1507
1508         /* Register with MGS */
1509         rc = server_register_target(lsi);
1510         if (rc < 0)
1511                 GOTO(out_mgc, rc);
1512
1513         /* Let the target look up the mount using the target's name
1514          * (we can't pass the sb or mnt through class_process_config.)
1515          */
1516         rc = server_register_mount(lsi->lsi_svname, sb);
1517         if (rc < 0)
1518                 GOTO(out_mgc, rc);
1519
1520         /* Start targets using the llog named for the target */
1521         memset(&cfg, 0, sizeof(cfg));
1522         cfg.cfg_callback = class_config_llog_handler;
1523         cfg.cfg_sub_clds = CONFIG_SUB_SERVER;
1524         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1525         if (rc < 0) {
1526                 CERROR("failed to start server %s: %d\n",
1527                        lsi->lsi_svname, rc);
1528                 /* Do NOT call server_deregister_mount() here. This makes it
1529                  * impossible to find mount later in cleanup time and leaves
1530                  * @lsi and othder stuff leaked. -umka
1531                  */
1532                 GOTO(out_mgc, rc);
1533         }
1534
1535         obd = class_name2obd(lsi->lsi_svname);
1536         if (!obd) {
1537                 CERROR("no server named %s was started\n", lsi->lsi_svname);
1538                 GOTO(out_mgc, rc = -ENXIO);
1539         }
1540
1541         if (IS_OST(lsi) || IS_MDT(lsi)) {
1542                 rc = lustre_start_lwp(sb);
1543                 if (rc < 0) {
1544                         CERROR("%s: failed to start LWP: %d\n",
1545                                lsi->lsi_svname, rc);
1546                         GOTO(out_mgc, rc);
1547                 }
1548         }
1549
1550         server_notify_target(sb, obd);
1551
1552         /* calculate recovery timeout, do it after lustre_process_log */
1553         server_calc_timeout(lsi, obd);
1554
1555         /* log has been fully processed, let clients connect */
1556         dev = obd->obd_lu_dev;
1557         if (dev && dev->ld_ops->ldo_prepare) {
1558                 struct lu_env env;
1559
1560                 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1561                 if (rc == 0) {
1562                         struct lu_context  session_ctx;
1563
1564                         lu_context_init(&session_ctx, LCT_SERVER_SESSION);
1565                         session_ctx.lc_thread = NULL;
1566                         lu_context_enter(&session_ctx);
1567                         env.le_ses = &session_ctx;
1568
1569                         rc = dev->ld_ops->ldo_prepare(&env, NULL, dev);
1570
1571                         lu_env_fini(&env);
1572                         lu_context_exit(&session_ctx);
1573                         lu_context_fini(&session_ctx);
1574                 }
1575         }
1576
1577         /* abort recovery only on the complete stack:
1578          * many devices can be involved
1579          */
1580         if ((test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags) ||
1581             (test_bit(LMD_FLG_ABORT_RECOV_MDT, lsi->lsi_lmd->lmd_flags))) &&
1582             (obd->obd_type->typ_dt_ops->o_iocontrol)) {
1583                 struct obd_ioctl_data karg;
1584
1585                 if (test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags))
1586                         karg.ioc_type = OBD_FLG_ABORT_RECOV_OST;
1587                 else
1588                         karg.ioc_type = OBD_FLG_ABORT_RECOV_MDT;
1589
1590                 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1591                               &karg, NULL);
1592         }
1593
1594 out_mgc:
1595         /* Release the mgc fs for others to use */
1596         server_mgc_clear_fs(&mgc_env, lsi->lsi_mgc);
1597 out_env:
1598         lu_env_fini(&mgc_env);
1599 out_stop_service:
1600         /* in case of error upper function call
1601          * server_put_super->server_stop_servers()
1602          */
1603
1604         RETURN(rc);
1605 }
1606
1607 static int lsi_prepare(struct lustre_sb_info *lsi)
1608 {
1609         const char *osd_type;
1610         const char *fstype;
1611         u32 index;
1612         int rc;
1613
1614         ENTRY;
1615         LASSERT(lsi);
1616         LASSERT(lsi->lsi_lmd);
1617
1618         /* The server name is given as a mount line option */
1619         if (!lsi->lsi_lmd->lmd_profile) {
1620                 LCONSOLE_ERROR("Can't determine server name\n");
1621                 RETURN(-EINVAL);
1622         }
1623
1624         /* Determine osd type */
1625         if (!lsi->lsi_lmd->lmd_osd_type) {
1626                 osd_type = LUSTRE_OSD_LDISKFS_NAME;
1627                 fstype = "ldiskfs";
1628         } else {
1629                 osd_type = lsi->lsi_lmd->lmd_osd_type;
1630                 fstype = lsi->lsi_lmd->lmd_osd_type;
1631         }
1632
1633         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname) ||
1634             strlen(osd_type) >= sizeof(lsi->lsi_osd_type) ||
1635             strlen(fstype) >= sizeof(lsi->lsi_fstype))
1636                 RETURN(-ENAMETOOLONG);
1637
1638         strscpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile,
1639                 sizeof(lsi->lsi_svname));
1640         strscpy(lsi->lsi_osd_type, osd_type, sizeof(lsi->lsi_osd_type));
1641         /* XXX: a temp. solution for components using ldiskfs
1642          *      to be removed in one of the subsequent patches
1643          */
1644         strscpy(lsi->lsi_fstype, fstype, sizeof(lsi->lsi_fstype));
1645
1646         /* Determine server type */
1647         rc = server_name2index(lsi->lsi_svname, &index, NULL);
1648         if (rc < 0) {
1649                 if (test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags)) {
1650                         /* Assume we're a bare MGS */
1651                         rc = 0;
1652                         set_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags);
1653                 } else {
1654                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1655                                        lsi->lsi_svname);
1656                         RETURN(rc);
1657                 }
1658         }
1659         lsi->lsi_flags |= rc;
1660
1661         /* Add mount line flags that used to be in ldd:
1662          * writeconf, mgs, anything else?
1663          */
1664         lsi->lsi_flags |= test_bit(LMD_FLG_WRITECONF, lsi->lsi_lmd->lmd_flags) ?
1665                           LDD_F_WRITECONF : 0;
1666         lsi->lsi_flags |= test_bit(LMD_FLG_NO_LOCAL_LOGS, lsi->lsi_lmd->lmd_flags) ?
1667                           LDD_F_NO_LOCAL_LOGS : 0;
1668         lsi->lsi_flags |= test_bit(LMD_FLG_VIRGIN, lsi->lsi_lmd->lmd_flags) ?
1669                           LDD_F_VIRGIN : 0;
1670         lsi->lsi_flags |= test_bit(LMD_FLG_UPDATE, lsi->lsi_lmd->lmd_flags) ?
1671                           LDD_F_UPDATE : 0;
1672         lsi->lsi_flags |= test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags) ?
1673                           LDD_F_SV_TYPE_MGS : 0;
1674         lsi->lsi_flags |= test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) ?
1675                           LDD_F_NO_PRIMNODE : 0;
1676
1677         RETURN(0);
1678 }
1679
1680 /*************** server mount ******************/
1681
1682 /** Start the shutdown of servers at umount.
1683  */
1684 static void server_put_super(struct super_block *sb)
1685 {
1686         struct lustre_sb_info *lsi = s2lsi(sb);
1687         struct obd_device *obd;
1688         char *tmpname, *extraname = NULL;
1689         int tmpname_sz;
1690         int lsiflags = lsi->lsi_flags;
1691         bool stop_servers = lsi->lsi_server_started;
1692
1693         ENTRY;
1694         LASSERT(IS_SERVER(lsi));
1695
1696         tmpname_sz = strlen(lsi->lsi_svname) + 1;
1697         OBD_ALLOC(tmpname, tmpname_sz);
1698         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1699         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1700         if (IS_MDT(lsi) && test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))
1701                 snprintf(tmpname, tmpname_sz, "MGS");
1702
1703         /* disconnect the lwp first to drain off the inflight request */
1704         if (IS_OST(lsi) || IS_MDT(lsi)) {
1705                 int     rc;
1706
1707                 rc = lustre_disconnect_lwp(sb);
1708                 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
1709                     rc != -ENOTCONN && rc != -ESHUTDOWN)
1710                         CWARN("%s: failed to disconnect lwp: rc= %d\n",
1711                               tmpname, rc);
1712         }
1713
1714         /* Stop the target */
1715         if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
1716             (IS_MDT(lsi) || IS_OST(lsi))) {
1717                 struct lustre_profile *lprof = NULL;
1718
1719                 /* tell the mgc to drop the config log */
1720                 lustre_end_log(sb, lsi->lsi_svname, NULL);
1721
1722                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1723                  * If there are any setup/cleanup errors, save the lov
1724                  * name for safety cleanup later.
1725                  */
1726                 lprof = class_get_profile(lsi->lsi_svname);
1727                 if (lprof) {
1728                         if (lprof->lp_dt) {
1729                                 OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1730                                 strncpy(extraname, lprof->lp_dt,
1731                                         strlen(lprof->lp_dt) + 1);
1732                         }
1733                         class_put_profile(lprof);
1734                 }
1735
1736                 obd = class_name2obd(lsi->lsi_svname);
1737                 if (obd) {
1738                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1739                         if (lsiflags & LSI_UMOUNT_FAILOVER)
1740                                 obd->obd_fail = 1;
1741                         /* We can't seem to give an error return code
1742                          * to .put_super, so we better make sure we clean up!
1743                          */
1744                         obd->obd_force = 1;
1745                         class_manual_cleanup(obd);
1746                         if (CFS_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE)) {
1747                                 int idx;
1748
1749                                 server_name2index(lsi->lsi_svname, &idx, NULL);
1750                                 /* sleeping for MDT0001 */
1751                                 if (idx == 1)
1752                                         CFS_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1753                         }
1754                 } else {
1755                         CERROR("no obd %s\n", lsi->lsi_svname);
1756                         server_deregister_mount(lsi->lsi_svname);
1757                 }
1758         }
1759
1760         /* If they wanted the mgs to stop separately from the mdt, they
1761          * should have put it on a different device.
1762          */
1763         lustre_stop_mgc(sb);
1764         if (IS_MGS(lsi)) {
1765                 /* if MDS start with --nomgs, don't stop MGS then */
1766                 if (!test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags))
1767                         server_stop_mgs(sb);
1768         }
1769
1770         if (IS_OST(lsi) || IS_MDT(lsi)) {
1771                 if (lustre_stop_lwp(sb) < 0)
1772                         CERROR("%s: failed to stop lwp!\n", tmpname);
1773         }
1774
1775         /* Drop a ref to the mounted disk */
1776         lustre_put_lsi(sb);
1777
1778         /* wait till all in-progress cleanups are done
1779          * specifically we're interested in ofd cleanup
1780          * as it pins OSS
1781          */
1782         obd_zombie_barrier();
1783
1784         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1785          * until the target is really gone so that our type refcount check
1786          * is right.
1787          */
1788         if (stop_servers)
1789                 server_stop_servers(lsiflags);
1790
1791         /* In case of startup or cleanup err, stop related obds */
1792         if (extraname) {
1793                 obd = class_name2obd(extraname);
1794                 if (obd) {
1795                         CWARN("Cleaning orphaned obd %s\n", extraname);
1796                         obd->obd_force = 1;
1797                         class_manual_cleanup(obd);
1798                 }
1799                 OBD_FREE(extraname, strlen(extraname) + 1);
1800         }
1801
1802         LCONSOLE(D_WARNING, "server umount %s complete\n", tmpname);
1803         OBD_FREE(tmpname, tmpname_sz);
1804         EXIT;
1805 }
1806
1807 /* Called only for 'umount -f' */
1808 static void server_umount_begin(struct super_block *sb)
1809 {
1810         struct lustre_sb_info *lsi = s2lsi(sb);
1811
1812         ENTRY;
1813         CDEBUG(D_MOUNT, "umount -f\n");
1814         /* umount = failover
1815          * umount -f = force
1816          * no third way to do non-force, non-failover
1817          */
1818         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1819         EXIT;
1820 }
1821
1822 static int server_statfs(struct dentry *dentry, struct kstatfs *buf)
1823 {
1824         struct super_block *sb = dentry->d_sb;
1825         struct lustre_sb_info *lsi = s2lsi(sb);
1826         struct obd_statfs statfs;
1827         int rc;
1828
1829         ENTRY;
1830         if (lsi->lsi_dt_dev) {
1831                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1832                 if (rc == 0) {
1833                         statfs_unpack(buf, &statfs);
1834                         buf->f_type = sb->s_magic;
1835                         RETURN(0);
1836                 }
1837         }
1838
1839         /* just return 0 */
1840         buf->f_type = sb->s_magic;
1841         buf->f_bsize = sb->s_blocksize;
1842         buf->f_blocks = 1;
1843         buf->f_bfree = 0;
1844         buf->f_bavail = 0;
1845         buf->f_files = 1;
1846         buf->f_ffree = 0;
1847         buf->f_namelen = NAME_MAX;
1848         RETURN(0);
1849 }
1850
1851 static int server_show_options(struct seq_file *seq, struct dentry *dentry)
1852 {
1853         struct lustre_sb_info *lsi;
1854         struct lustre_mount_data *lmd;
1855         struct obd_statfs osfs;
1856         struct super_block *sb;
1857         int rc;
1858
1859         LASSERT(seq && dentry);
1860         lsi = s2lsi(dentry->d_sb);
1861         lmd = lsi->lsi_lmd;
1862         sb = dentry->d_sb;
1863
1864         if (lsi->lsi_dt_dev) {
1865                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &osfs);
1866                 if (!rc) {
1867                         /* Check FS State for OS_STATFS_READONLY
1868                          * (Read only) flag. If it is not set then
1869                          * toggle back the s_flag's SB_RDONLY bit.
1870                          * The SB_RDONLY bit is always set for OST/MDT
1871                          * during server prep (server_fill_super_common())
1872                          * call.
1873                          *
1874                          * Also, if server is mounted with "rdonly_dev"
1875                          * (LMD_FLG_DEV_RDONLY) then force flag to be 'ro'
1876                          */
1877                         if (!test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags) &&
1878                             !(osfs.os_state & OS_STATFS_READONLY))
1879                                 sb->s_flags &= ~SB_RDONLY;
1880                 }
1881         }
1882
1883         seq_printf(seq, ",svname=%s", lmd->lmd_profile);
1884
1885         if (test_bit(LMD_FLG_ABORT_RECOV, lmd->lmd_flags))
1886                 seq_puts(seq, ",abort_recov");
1887
1888         if (test_bit(LMD_FLG_NOIR, lmd->lmd_flags))
1889                 seq_puts(seq, ",noir");
1890
1891         if (test_bit(LMD_FLG_NOSVC, lmd->lmd_flags))
1892                 seq_puts(seq, ",nosvc");
1893
1894         if (test_bit(LMD_FLG_NOMGS, lmd->lmd_flags))
1895                 seq_puts(seq, ",nomgs");
1896
1897         if (test_bit(LMD_FLG_NOSCRUB, lmd->lmd_flags))
1898                 seq_puts(seq, ",noscrub");
1899
1900         if (test_bit(LMD_FLG_SKIP_LFSCK, lmd->lmd_flags))
1901                 seq_puts(seq, ",skip_lfsck");
1902
1903         if (test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags))
1904                 seq_puts(seq, ",rdonly_dev");
1905
1906         if (test_bit(LMD_FLG_MGS, lmd->lmd_flags))
1907                 seq_puts(seq, ",mgs");
1908
1909         if (lmd->lmd_mgs)
1910                 seq_printf(seq, ",mgsnode=%s", lmd->lmd_mgs);
1911
1912         if (lmd->lmd_osd_type)
1913                 seq_printf(seq, ",osd=%s", lmd->lmd_osd_type);
1914
1915         if (lmd->lmd_opts) {
1916                 seq_putc(seq, ',');
1917                 seq_puts(seq, lmd->lmd_opts);
1918         }
1919
1920         RETURN(0);
1921 }
1922
1923 /** The operations we support directly on the superblock:
1924  * mount, umount, and df.
1925  */
1926 static const struct super_operations server_ops = {
1927         .put_super      = server_put_super,
1928         .umount_begin   = server_umount_begin, /* umount -f */
1929         .statfs         = server_statfs,
1930         .show_options   = server_show_options,
1931 };
1932
1933 #if defined(HAVE_USER_NAMESPACE_ARG)
1934 # define IDMAP_ARG idmap,
1935 #else
1936 # define IDMAP_ARG
1937 # ifdef HAVE_INODEOPS_ENHANCED_GETATTR
1938 #  define server_getattr(ns, path, st, rq, fl) server_getattr(path, st, rq, fl)
1939 # endif
1940 #endif
1941
1942 /*
1943  * inode operations for Lustre server mountpoints
1944  */
1945 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
1946 static int server_getattr(struct mnt_idmap *idmap,
1947                           const struct path *path, struct kstat *stat,
1948                           u32 request_mask, unsigned int flags)
1949 {
1950         struct inode *inode = d_inode(path->dentry);
1951 #else
1952 static int server_getattr(struct vfsmount *mnt, struct dentry *de,
1953                           struct kstat *stat)
1954 {
1955         struct inode *inode = de->d_inode;
1956 #endif
1957         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
1958         struct vfsmount *root_mnt;
1959         struct inode *root_inode;
1960
1961         root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
1962         if (IS_ERR(root_mnt))
1963                 root_inode = igrab(inode);
1964         else
1965                 root_inode = igrab(root_mnt->mnt_sb->s_root->d_inode);
1966         if (!root_inode)
1967                 return -EACCES;
1968
1969         CDEBUG(D_SUPER, "%s: root_inode from %s ino=%lu, dev=%x\n",
1970                lsi->lsi_svname, root_inode == inode ? "lsi" : "vfsmnt",
1971                root_inode->i_ino, root_inode->i_rdev);
1972         generic_fillattr(IDMAP_ARG RQMASK_ARG root_inode, stat);
1973         iput(root_inode);
1974
1975         return 0;
1976 }
1977
1978 #ifdef HAVE_IOP_XATTR
1979 static ssize_t server_getxattr(struct dentry *dentry, const char *name,
1980                                 void *buffer, size_t size)
1981 {
1982         if (!selinux_is_enabled())
1983                 return -EOPNOTSUPP;
1984         return -ENODATA;
1985 }
1986
1987 static int server_setxattr(struct dentry *dentry, const char *name,
1988                             const void *value, size_t size, int flags)
1989 {
1990         return -EOPNOTSUPP;
1991 }
1992 #endif
1993
1994 static ssize_t server_listxattr(struct dentry *d_entry, char *name,
1995                                 size_t size)
1996 {
1997         return -EOPNOTSUPP;
1998 }
1999
2000 static bool is_cmd_supported(unsigned int cmd)
2001 {
2002         CDEBUG(D_SUPER, "ioctl cmd=%x\n", cmd);
2003
2004         switch (cmd) {
2005         case FITRIM:
2006                 return true;
2007         case LL_IOC_RESIZE_FS:
2008                 return true;
2009 #ifdef HAVE_FSMAP_H
2010         case FS_IOC_GETFSMAP:
2011                 return true;
2012 #endif
2013         default:
2014                 return false;
2015         }
2016
2017         return false;
2018 }
2019
2020 static long server_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2021 {
2022         struct lustre_sb_info *lsi = s2lsi(file_inode(filp)->i_sb);
2023         struct vfsmount *root_mnt;
2024         struct file *root_filp;
2025         struct inode *root_inode;
2026         int err = -ENOTTY;
2027
2028         if (!is_cmd_supported(cmd))
2029                 return err;
2030
2031         root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
2032         if (IS_ERR(root_mnt))
2033                 return err;
2034
2035         root_inode = igrab(root_mnt->mnt_root->d_inode);
2036         if (!root_inode)
2037                 return -EACCES;
2038
2039         root_filp = alloc_file_pseudo(root_inode, root_mnt, "/",
2040                                       O_RDWR | O_NOATIME, root_inode->i_fop);
2041         if (root_inode->i_fop && root_inode->i_fop->unlocked_ioctl)
2042                 err = root_inode->i_fop->unlocked_ioctl(root_filp, cmd, arg);
2043         fput(root_filp);
2044
2045         return err;
2046 }
2047
2048 static const struct inode_operations server_inode_operations = {
2049         .getattr        = server_getattr,
2050 #ifdef HAVE_IOP_XATTR
2051         .setxattr       = server_setxattr,
2052         .getxattr       = server_getxattr,
2053 #endif
2054         .listxattr      = server_listxattr,
2055 };
2056
2057 static const struct file_operations server_file_operations = {
2058         .unlocked_ioctl = server_ioctl,
2059 };
2060
2061 #define log2(n) ffz(~(n))
2062 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
2063
2064 static int server_fill_super_common(struct super_block *sb)
2065 {
2066         struct inode *root = NULL;
2067
2068         ENTRY;
2069         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
2070
2071         sb->s_blocksize = 4096;
2072         sb->s_blocksize_bits = log2(sb->s_blocksize);
2073         sb->s_magic = LUSTRE_SUPER_MAGIC;
2074         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
2075         sb->s_flags |= SB_RDONLY;
2076         sb->s_op = &server_ops;
2077
2078         root = new_inode(sb);
2079         if (!root) {
2080                 CERROR("Can't make root inode\n");
2081                 RETURN(-EIO);
2082         }
2083
2084         /* returns -EIO for every operation */
2085         /* make_bad_inode(root); -- badness - can't umount */
2086         /* apparently we need to be a directory for the mount to finish */
2087         root->i_mode = S_IFDIR;
2088         root->i_op = &server_inode_operations;
2089         root->i_fop = &server_file_operations;
2090         sb->s_root = d_make_root(root);
2091         if (!sb->s_root) {
2092                 CERROR("%s: can't make root dentry\n", sb->s_id);
2093                 RETURN(-EIO);
2094         }
2095
2096         RETURN(0);
2097 }
2098
2099 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
2100 {
2101         struct lustre_mount_data *lmd = lsi->lsi_lmd;
2102         struct obd_device *obd;
2103         struct dt_device_param p;
2104         char flagstr[20 + 1 + 10 + 1];
2105         u32 lmd_flags;
2106         int rc;
2107
2108         ENTRY;
2109         CDEBUG(D_MOUNT,
2110                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
2111                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
2112
2113         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
2114         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
2115         strcat(lsi->lsi_osd_uuid, "_UUID");
2116         bitmap_to_arr32(&lmd_flags, lmd->lmd_flags, LMD_FLG_NUM_FLAGS);
2117         snprintf(flagstr, sizeof(flagstr), "%lu:%u", mflags, lmd_flags);
2118
2119         obd = class_name2obd(lsi->lsi_osd_obdname);
2120         if (!obd) {
2121                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
2122                                          lsi->lsi_osd_type,
2123                                          lsi->lsi_osd_uuid, lmd->lmd_dev,
2124                                          flagstr, lsi->lsi_lmd->lmd_opts,
2125                                          lsi->lsi_svname);
2126                 if (rc < 0)
2127                         GOTO(out, rc);
2128                 obd = class_name2obd(lsi->lsi_osd_obdname);
2129                 LASSERT(obd);
2130         } else {
2131                 CDEBUG(D_MOUNT, "%s already started\n", lsi->lsi_osd_obdname);
2132                 /* but continue setup to allow special case of MDT and internal
2133                  * MGT being started separately.
2134                  */
2135                 if (!((IS_MGS(lsi) &&
2136                        test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) ||
2137                      (IS_MDT(lsi) &&
2138                       test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))))
2139                         RETURN(-EALREADY);
2140         }
2141
2142         rc = obd_connect(NULL, &lsi->lsi_osd_exp,
2143                          obd, &obd->obd_uuid, NULL, NULL);
2144
2145         if (rc < 0) {
2146                 obd->obd_force = 1;
2147                 class_manual_cleanup(obd);
2148                 lsi->lsi_dt_dev = NULL;
2149                 RETURN(rc);
2150         }
2151
2152         LASSERT(obd->obd_lu_dev);
2153         lu_device_get(obd->obd_lu_dev);
2154         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
2155         LASSERT(lsi->lsi_dt_dev);
2156
2157         /* set disk context for llog usage */
2158         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2159         obd->obd_lvfs_ctxt.dt = lsi->lsi_dt_dev;
2160
2161         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
2162 out:
2163         RETURN(rc);
2164 }
2165
2166 /** Fill in the superblock info for a Lustre server.
2167  * Mount the device with the correct options.
2168  * Read the on-disk config file.
2169  * Start the services.
2170  */
2171 int server_fill_super(struct super_block *sb)
2172 {
2173         struct lustre_sb_info *lsi = s2lsi(sb);
2174         int rc;
2175
2176         ENTRY;
2177         /* to simulate target mount race */
2178         CFS_RACE(OBD_FAIL_TGT_MOUNT_RACE);
2179
2180         rc = lsi_prepare(lsi);
2181         if (rc < 0) {
2182                 lustre_put_lsi(sb);
2183                 RETURN(rc);
2184         }
2185
2186         /* Start low level OSD */
2187         rc = osd_start(lsi, sb->s_flags);
2188         if (rc < 0) {
2189                 CERROR("Unable to start osd on %s: %d\n",
2190                        lsi->lsi_lmd->lmd_dev, rc);
2191                 lustre_put_lsi(sb);
2192                 RETURN(rc);
2193         }
2194
2195         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2196                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2197
2198         if (class_name2obd(lsi->lsi_svname)) {
2199                 LCONSOLE_ERROR_MSG(0x161,
2200                                    "The target named %s is already running. Double-mount may have compromised the disk journal.\n",
2201                                    lsi->lsi_svname);
2202                 lustre_put_lsi(sb);
2203                 RETURN(-EALREADY);
2204         }
2205
2206         /* Start MGS before MGC */
2207         if (IS_MGS(lsi) && !test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) {
2208                 rc = server_start_mgs(sb);
2209                 if (rc < 0)
2210                         GOTO(out_mnt, rc);
2211         }
2212
2213         /* Start MGC before servers */
2214         rc = lustre_start_mgc(sb);
2215         if (rc < 0)
2216                 GOTO(out_mnt, rc);
2217
2218         /* Set up all obd devices for service */
2219         if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
2220             (IS_OST(lsi) || IS_MDT(lsi))) {
2221                 rc = server_start_targets(sb);
2222                 if (rc < 0) {
2223                         CERROR("Unable to start targets: %d\n", rc);
2224                         GOTO(out_mnt, rc);
2225                 }
2226                 /* FIXME overmount client here, or can we just start a
2227                  * client log and client_fill_super on this sb?  We
2228                  * need to make sure server_put_super gets called too
2229                  * - ll_put_super calls lustre_common_put_super; check
2230                  * there for LSI_SERVER flag, call s_p_s if so.
2231                  *
2232                  * Probably should start client from new thread so we
2233                  * can return.  Client will not finish until all
2234                  * servers are connected.  Note - MGS-only server does
2235                  * NOT get a client, since there is no lustre fs
2236                  * associated - the MGS is for all lustre fs's
2237                  */
2238         }
2239
2240         rc = server_fill_super_common(sb);
2241         if (rc < 0)
2242                 GOTO(out_mnt, rc);
2243
2244         RETURN(0);
2245 out_mnt:
2246         /* We jump here in case of failure while starting targets or MGS.
2247          * In this case we can't just put @mnt and have to do real cleanup
2248          * with stoping targets, etc.
2249          */
2250         server_put_super(sb);
2251         return rc;
2252 }
2253 EXPORT_SYMBOL(server_fill_super);
2254
2255 /*
2256  * Calculate timeout value for a target.
2257  */
2258 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2259 {
2260         struct lustre_mount_data *lmd;
2261         int soft = 0;
2262         int hard = 0;
2263         int factor = 0;
2264         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2265         int min = OBD_RECOVERY_TIME_MIN;
2266
2267         LASSERT(IS_SERVER(lsi));
2268
2269         lmd = lsi->lsi_lmd;
2270         if (lmd) {
2271                 soft   = lmd->lmd_recovery_time_soft;
2272                 hard   = lmd->lmd_recovery_time_hard;
2273                 has_ir = has_ir && !test_bit(LMD_FLG_NOIR, lmd->lmd_flags);
2274                 obd->obd_no_ir = !has_ir;
2275         }
2276
2277         if (soft == 0)
2278                 soft = OBD_RECOVERY_TIME_SOFT;
2279         if (hard == 0)
2280                 hard = OBD_RECOVERY_TIME_HARD;
2281
2282         /* target may have ir_factor configured. */
2283         factor = OBD_IR_FACTOR_DEFAULT;
2284         if (obd->obd_recovery_ir_factor)
2285                 factor = obd->obd_recovery_ir_factor;
2286
2287         if (has_ir) {
2288                 int new_soft = soft;
2289
2290                 /* adjust timeout value by imperative recovery */
2291                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2292                 /* make sure the timeout is not too short */
2293                 new_soft = max(min, new_soft);
2294
2295                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery window shrunk from %d-%d down to %d-%d\n",
2296                               obd->obd_name, soft, hard, new_soft, hard);
2297
2298                 soft = new_soft;
2299         } else {
2300                 LCONSOLE_INFO("%s: Imperative Recovery not enabled, recovery window %d-%d\n",
2301                               obd->obd_name, soft, hard);
2302         }
2303
2304         /* we're done */
2305         obd->obd_recovery_timeout = max_t(time64_t, obd->obd_recovery_timeout,
2306                                           soft);
2307         obd->obd_recovery_time_hard = hard;
2308         obd->obd_recovery_ir_factor = factor;
2309 }
2310
2311 /**
2312  * This is the entry point for the mount call into Lustre.
2313  * This is called when a server target is mounted,
2314  * and this is where we start setting things up.
2315  * @param data Mount options (e.g. -o flock,abort_recov)
2316  */
2317 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
2318                                  int silent)
2319 {
2320         struct lustre_mount_data *lmd;
2321         struct lustre_sb_info *lsi;
2322         int rc;
2323
2324         ENTRY;
2325         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2326
2327         lsi = lustre_init_lsi(sb);
2328         if (!lsi)
2329                 RETURN(-ENOMEM);
2330         lmd = lsi->lsi_lmd;
2331
2332         /*
2333          * Disable lockdep during mount, because mount locking patterns are
2334          * 'special'.
2335          */
2336         lockdep_off();
2337
2338         /*
2339          * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
2340          */
2341         obd_zombie_barrier();
2342
2343         /* Figure out the lmd from the mount options */
2344         if (lmd_parse(lmd2_data, lmd)) {
2345                 lustre_put_lsi(sb);
2346                 GOTO(out, rc = -EINVAL);
2347         }
2348
2349         if (lmd_is_client(lmd)) {
2350                 rc = -ENODEV;
2351                 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
2352                        lmd->lmd_dev, rc);
2353                 lustre_put_lsi(sb);
2354                 GOTO(out, rc);
2355         }
2356
2357         CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2358         rc = server_fill_super(sb);
2359         /*
2360          * server_fill_super calls lustre_start_mgc after the mount
2361          * because we need the MGS NIDs which are stored on disk.
2362          * Plus, we may need to start the MGS first.
2363          *
2364          * server_fill_super will call server_put_super on failure
2365          *
2366          * If error happens in fill_super() call, @lsi will be killed there.
2367          * This is why we do not put it here.
2368          */
2369 out:
2370         if (rc) {
2371                 CERROR("Unable to mount %s (%d)\n",
2372                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
2373         } else {
2374                 CDEBUG(D_SUPER, "Mount %s complete\n",
2375                        lmd->lmd_dev);
2376         }
2377         lockdep_on();
2378         return rc;
2379 }
2380
2381 /***************** FS registration ******************/
2382 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
2383                                        int flags, const char *devname,
2384                                        void *data)
2385 {
2386         return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
2387 }
2388
2389 /* Register the "lustre_tgt" fs type.
2390  *
2391  * Right now this isn't any different than the normal "lustre" filesystem
2392  * type, but it is added so that there is some compatibility to allow
2393  * changing documentation and scripts to start using the "lustre_tgt" type
2394  * at mount time. That will simplify test interop, and in case of upgrades
2395  * that change to the new type and then need to roll back for some reason.
2396  *
2397  * The long-term goal is to disentangle the client and server mount code.
2398  */
2399 static struct file_system_type lustre_tgt_fstype = {
2400         .owner          = THIS_MODULE,
2401         .name           = "lustre_tgt",
2402         .mount          = lustre_tgt_mount,
2403         .kill_sb        = kill_anon_super,
2404         .fs_flags       = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
2405 };
2406 MODULE_ALIAS_FS("lustre_tgt");
2407
2408 int lustre_tgt_register_fs(void)
2409 {
2410         return register_filesystem(&lustre_tgt_fstype);
2411 }
2412
2413 void lustre_tgt_unregister_fs(void)
2414 {
2415         unregister_filesystem(&lustre_tgt_fstype);
2416 }