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