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