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