Whamcloud - gitweb
LU-6142 obdclass: checkpatch cleanup of obd_mount_server.c
[fs/lustre-release.git] / lustre / obdclass / obd_mount_server.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         OBD_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         int rc;
644
645         ENTRY;
646         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
647                             lcfg->lcfg_nid);
648         if (rc != 0) {
649                 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
650                 RETURN(rc);
651         }
652
653         OBD_ALLOC(lwpname, MTI_NAME_MAXLEN);
654         if (lwpname == NULL)
655                 GOTO(out, rc = -ENOMEM);
656
657         rc = tgt_name2lwp_name(lsi->lsi_svname, lwpname, MTI_NAME_MAXLEN, idx);
658         if (rc != 0) {
659                 CERROR("%s: failed to generate lwp name: rc = %d\n",
660                        lsi->lsi_svname, rc);
661                 GOTO(out, rc);
662         }
663
664         OBD_ALLOC(lwpuuid, MTI_NAME_MAXLEN);
665         if (!lwpuuid)
666                 GOTO(out, rc = -ENOMEM);
667
668         sprintf(lwpuuid, "%s_UUID", lwpname);
669         rc = lustre_start_simple(lwpname, LUSTRE_LWP_NAME,
670                                  lwpuuid, lustre_cfg_string(lcfg, 1),
671                                  NULL, NULL, NULL);
672         if (rc < 0) {
673                 CERROR("%s: setup up failed: rc %d\n", lwpname, rc);
674                 GOTO(out, rc);
675         }
676
677         obd = class_name2obd(lwpname);
678         LASSERT(obd);
679
680         rc = lustre_lwp_connect(obd, strstr(lsi->lsi_svname, "-MDT") != NULL);
681         if (rc < 0) {
682                 CERROR("%s: connect failed: rc = %d\n", lwpname, rc);
683                 GOTO(out, rc);
684         }
685
686         obd->u.cli.cl_max_mds_easize = MAX_MD_SIZE;
687         mutex_lock(&lsi->lsi_lwp_mutex);
688         list_add_tail(&obd->obd_lwp_list, &lsi->lsi_lwp_list);
689         mutex_unlock(&lsi->lsi_lwp_mutex);
690 out:
691         if (lwpname)
692                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
693         if (lwpuuid)
694                 OBD_FREE(lwpuuid, MTI_NAME_MAXLEN);
695
696         return rc;
697 }
698
699 /* the caller is responsible for memory free */
700 static struct obd_device *lustre_find_lwp(struct lustre_sb_info *lsi,
701                                           char **lwpname, u32 idx)
702 {
703         struct obd_device *lwp;
704         int rc = 0;
705
706         ENTRY;
707         LASSERT(lwpname);
708         LASSERT(IS_OST(lsi) || IS_MDT(lsi));
709
710         OBD_ALLOC(*lwpname, MTI_NAME_MAXLEN);
711         if (*lwpname == NULL)
712                 RETURN(ERR_PTR(-ENOMEM));
713
714         rc = tgt_name2lwp_name(lsi->lsi_svname, *lwpname, MTI_NAME_MAXLEN, idx);
715         if (rc != 0) {
716                 CERROR("%s: failed to generate lwp name: rc = %d\n",
717                        lsi->lsi_svname, rc);
718                 GOTO(out, rc = -EINVAL);
719         }
720
721         lwp = class_name2obd(*lwpname);
722
723 out:
724         if (rc != 0) {
725                 if (*lwpname != NULL) {
726                         OBD_FREE(*lwpname, MTI_NAME_MAXLEN);
727                         *lwpname = NULL;
728                 }
729                 lwp = ERR_PTR(rc);
730         }
731
732         RETURN(lwp ? lwp : ERR_PTR(-ENOENT));
733 }
734
735 static int lustre_lwp_add_conn(struct lustre_cfg *cfg,
736                                struct lustre_sb_info *lsi, u32 idx)
737 {
738         struct lustre_cfg_bufs *bufs = NULL;
739         struct lustre_cfg *lcfg = NULL;
740         char *lwpname = NULL;
741         struct obd_device *lwp;
742         int rc;
743
744         ENTRY;
745         lwp = lustre_find_lwp(lsi, &lwpname, idx);
746         if (IS_ERR(lwp)) {
747                 CERROR("%s: can't find lwp device.\n", lsi->lsi_svname);
748                 GOTO(out, rc = PTR_ERR(lwp));
749         }
750         LASSERT(lwpname);
751
752         OBD_ALLOC_PTR(bufs);
753         if (bufs == NULL)
754                 GOTO(out, rc = -ENOMEM);
755
756         lustre_cfg_bufs_reset(bufs, lwpname);
757         lustre_cfg_bufs_set_string(bufs, 1,
758                                    lustre_cfg_string(cfg, 1));
759
760         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
761         if (!lcfg)
762                 GOTO(out_cfg, rc = -ENOMEM);
763         lustre_cfg_init(lcfg, LCFG_ADD_CONN, bufs);
764
765         rc = class_add_conn(lwp, lcfg);
766         if (rc < 0)
767                 CERROR("%s: can't add conn: rc = %d\n", lwpname, rc);
768
769         if (lcfg)
770                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
771                                               lcfg->lcfg_buflens));
772 out_cfg:
773         if (bufs)
774                 OBD_FREE_PTR(bufs);
775 out:
776         if (lwpname)
777                 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
778         RETURN(rc);
779 }
780
781 /**
782  * Retrieve MDT nids from the client log, then start the lwp device.
783  * there are only two scenarios which would include mdt nid.
784  * 1.
785  * marker   5 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxx-
786  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:192.168.122.162@tcp
787  * attach    0:lustre-MDTyyyy-mdc  1:mdc  2:lustre-clilmv_UUID
788  * setup     0:lustre-MDTyyyy-mdc  1:lustre-MDTyyyy_UUID  2:192.168.122.162@tcp
789  * add_uuid  nid=192.168.172.1@tcp(0x20000c0a8ac01)  0:  1:192.168.172.1@tcp
790  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.172.1@tcp
791  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDTyyyy_UUID xxxx
792  * marker   5 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add mdc' xxxx-
793  * 2.
794  * marker   7 (flags=0x01, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
795  * add_uuid  nid=192.168.122.2@tcp(0x20000c0a87a02)  0:  1:192.168.122.2@tcp
796  * add_conn  0:lustre-MDTyyyy-mdc  1:192.168.122.2@tcp
797  * marker   7 (flags=0x02, v2.1.54.0) lustre-MDTyyyy  'add failnid' xxxx-
798  **/
799 static int client_lwp_config_process(const struct lu_env *env,
800                                      struct llog_handle *handle,
801                                      struct llog_rec_hdr *rec, void *data)
802 {
803         struct config_llog_instance *cfg = data;
804         int cfg_len = rec->lrh_len;
805         char *cfg_buf = (char *) (rec + 1);
806         struct lustre_cfg *lcfg = NULL;
807         struct lustre_sb_info *lsi;
808         int rc = 0, swab = 0;
809
810         ENTRY;
811         if (rec->lrh_type != OBD_CFG_REC) {
812                 CERROR("Unknown llog record type %#x encountered\n",
813                        rec->lrh_type);
814                 RETURN(-EINVAL);
815         }
816
817         if (!cfg->cfg_sb)
818                 GOTO(out, rc = -EINVAL);
819         lsi = s2lsi(cfg->cfg_sb);
820
821         lcfg = (struct lustre_cfg *)cfg_buf;
822         if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
823                 lustre_swab_lustre_cfg(lcfg);
824                 swab = 1;
825         }
826
827         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
828         if (rc < 0)
829                 GOTO(out, rc);
830
831         switch (lcfg->lcfg_command) {
832         case LCFG_MARKER: {
833                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
834
835                 lustre_swab_cfg_marker(marker, swab,
836                                        LUSTRE_CFG_BUFLEN(lcfg, 1));
837                 if (marker->cm_flags & CM_SKIP ||
838                     marker->cm_flags & CM_EXCLUDE)
839                         GOTO(out, rc = 0);
840
841                 if (!tgt_is_mdt(marker->cm_tgtname, &cfg->cfg_lwp_idx))
842                         GOTO(out, rc = 0);
843
844                 if (IS_MDT(lsi) && cfg->cfg_lwp_idx != 0)
845                         GOTO(out, rc = 0);
846
847                 if (!strncmp(marker->cm_comment, "add mdc", 7) ||
848                     !strncmp(marker->cm_comment, "add failnid", 11)) {
849                         if (marker->cm_flags & CM_START) {
850                                 cfg->cfg_flags = CFG_F_MARKER;
851                                 /* This hack is to differentiate the
852                                  * ADD_UUID is come from "add mdc" record
853                                  * or from "add failnid" record.
854                                  */
855                                 if (!strncmp(marker->cm_comment,
856                                              "add failnid", 11))
857                                         cfg->cfg_flags |= CFG_F_SKIP;
858                         } else if (marker->cm_flags & CM_END) {
859                                 cfg->cfg_flags = 0;
860                         }
861                 }
862                 break;
863         }
864         case LCFG_ADD_UUID: {
865                 if (cfg->cfg_flags == CFG_F_MARKER) {
866                         rc = lustre_lwp_setup(lcfg, lsi, cfg->cfg_lwp_idx);
867                         /* XXX: process only the first nid as
868                          * we don't need another instance of lwp
869                          */
870                         cfg->cfg_flags |= CFG_F_SKIP;
871                 } else if (cfg->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
872                         rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
873                                             lcfg->lcfg_nid);
874                         if (rc < 0)
875                                 CERROR("%s: Fail to add uuid, rc:%d\n",
876                                        lsi->lsi_svname, rc);
877                 }
878                 break;
879         }
880         case LCFG_ADD_CONN: {
881                 char *devname = lustre_cfg_string(lcfg, 0);
882                 char *ptr;
883                 u32 idx = 0;
884
885                 if (!is_mdc_device(devname))
886                         break;
887
888                 if (!(cfg->cfg_flags & CFG_F_MARKER)) {
889                         CDEBUG(D_CONFIG, "Skipping add_conn for %s, rec %d\n",
890                                devname, rec->lrh_index);
891                         break;
892                 }
893
894                 /* add_conn should follow by add_uuid. This
895                  * guarantee lwp device was created
896                  */
897                 if (!(cfg->cfg_flags & CFG_F_SKIP)) {
898                         CWARN("Error at config for %s rec %d, add_conn should follow by add_uuid\n",
899                               devname, rec->lrh_index);
900                         break;
901                 }
902                 ptr = strrchr(devname, '-');
903                 if (ptr == NULL)
904                         break;
905
906                 *ptr = 0;
907                 if (!tgt_is_mdt(devname, &idx)) {
908                         *ptr = '-';
909                         break;
910                 }
911                 *ptr = '-';
912
913                 if (IS_MDT(lsi) && idx != 0)
914                         break;
915
916                 rc = lustre_lwp_add_conn(lcfg, lsi, idx);
917                 break;
918         }
919         default:
920                 break;
921         }
922 out:
923         RETURN(rc);
924 }
925
926 static int lustre_disconnect_lwp(struct super_block *sb)
927 {
928         struct lustre_sb_info *lsi = s2lsi(sb);
929         struct obd_device *lwp;
930         char *logname = NULL;
931         struct lustre_cfg_bufs *bufs = NULL;
932         struct config_llog_instance *cfg = NULL;
933         int rc = 0;
934         int rc1 = 0;
935
936         ENTRY;
937         if (likely(lsi->lsi_lwp_started)) {
938                 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
939                 if (logname == NULL)
940                         RETURN(-ENOMEM);
941
942                 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
943                 if (rc != 0) {
944                         CERROR("%s: failed to get fsname from svname: rc = %d\n",
945                                lsi->lsi_svname, rc);
946                         GOTO(out, rc = -EINVAL);
947                 }
948
949                 strcat(logname, "-client");
950                 OBD_ALLOC_PTR(cfg);
951                 if (cfg == NULL)
952                         GOTO(out, rc = -ENOMEM);
953
954                 /* end log first */
955                 cfg->cfg_instance = ll_get_cfg_instance(sb);
956                 rc = lustre_end_log(sb, logname, cfg);
957                 if (rc != 0 && rc != -ENOENT)
958                         GOTO(out, rc);
959
960                 lsi->lsi_lwp_started = 0;
961         }
962
963         OBD_ALLOC_PTR(bufs);
964         if (bufs == NULL)
965                 GOTO(out, rc = -ENOMEM);
966
967         mutex_lock(&lsi->lsi_lwp_mutex);
968         list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
969                 struct lustre_cfg *lcfg;
970
971                 if (likely(lwp->obd_lwp_export)) {
972                         class_export_put(lwp->obd_lwp_export);
973                         lwp->obd_lwp_export = NULL;
974                 }
975
976                 lustre_cfg_bufs_reset(bufs, lwp->obd_name);
977                 lustre_cfg_bufs_set_string(bufs, 1, NULL);
978                 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount,
979                                                bufs->lcfg_buflen));
980                 if (!lcfg) {
981                         rc = -ENOMEM;
982                         break;
983                 }
984                 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
985
986                 /* Disconnect import first. NULL is passed for the '@env',
987                  * since it will not be used.
988                  */
989                 rc = lwp->obd_lu_dev->ld_ops->ldo_process_config(NULL,
990                                                         lwp->obd_lu_dev, lcfg);
991                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
992                                               lcfg->lcfg_buflens));
993                 if (rc != 0 && rc != -ETIMEDOUT) {
994                         CERROR("%s: fail to disconnect LWP: rc = %d\n",
995                                lwp->obd_name, rc);
996                         rc1 = rc;
997                 }
998         }
999         mutex_unlock(&lsi->lsi_lwp_mutex);
1000
1001         GOTO(out, rc);
1002
1003 out:
1004         if (bufs)
1005                 OBD_FREE_PTR(bufs);
1006         if (cfg)
1007                 OBD_FREE_PTR(cfg);
1008         if (logname)
1009                 OBD_FREE(logname, MTI_NAME_MAXLEN);
1010
1011         return rc1 != 0 ? rc1 : rc;
1012 }
1013
1014 /**
1015  * Stop the lwp for an OST/MDT target.
1016  **/
1017 static int lustre_stop_lwp(struct super_block *sb)
1018 {
1019         struct lustre_sb_info *lsi = s2lsi(sb);
1020         struct obd_device *lwp;
1021         int rc = 0;
1022         int rc1 = 0;
1023
1024         ENTRY;
1025         mutex_lock(&lsi->lsi_lwp_mutex);
1026         while (!list_empty(&lsi->lsi_lwp_list)) {
1027                 lwp = list_entry(lsi->lsi_lwp_list.next, struct obd_device,
1028                                  obd_lwp_list);
1029                 list_del_init(&lwp->obd_lwp_list);
1030                 lwp->obd_force = 1;
1031                 mutex_unlock(&lsi->lsi_lwp_mutex);
1032
1033                 rc = class_manual_cleanup(lwp);
1034                 if (rc != 0) {
1035                         CERROR("%s: fail to stop LWP: rc = %d\n",
1036                                lwp->obd_name, rc);
1037                         rc1 = rc;
1038                 }
1039                 mutex_lock(&lsi->lsi_lwp_mutex);
1040         }
1041         mutex_unlock(&lsi->lsi_lwp_mutex);
1042
1043         RETURN(rc1 != 0 ? rc1 : rc);
1044 }
1045
1046 /**
1047  * Start the lwp(fsname-MDTyyyy-lwp-{MDT,OST}xxxx) for a MDT/OST or MDT target.
1048  **/
1049 static int lustre_start_lwp(struct super_block *sb)
1050 {
1051         struct lustre_sb_info *lsi = s2lsi(sb);
1052         struct config_llog_instance *cfg = NULL;
1053         char *logname;
1054         int rc;
1055
1056         ENTRY;
1057         if (unlikely(lsi->lsi_lwp_started))
1058                 RETURN(0);
1059
1060         OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1061         if (logname == NULL)
1062                 RETURN(-ENOMEM);
1063
1064         rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
1065         if (rc != 0) {
1066                 CERROR("%s: failed to get fsname from svname: rc = %d\n",
1067                        lsi->lsi_svname, rc);
1068                 GOTO(out, rc = -EINVAL);
1069         }
1070
1071         strcat(logname, "-client");
1072         OBD_ALLOC_PTR(cfg);
1073         if (cfg == NULL)
1074                 GOTO(out, rc = -ENOMEM);
1075
1076         cfg->cfg_callback = client_lwp_config_process;
1077         cfg->cfg_instance = ll_get_cfg_instance(sb);
1078         rc = lustre_process_log(sb, logname, cfg);
1079         /* need to remove config llog from mgc */
1080         lsi->lsi_lwp_started = 1;
1081
1082         GOTO(out, rc);
1083
1084 out:
1085         OBD_FREE(logname, MTI_NAME_MAXLEN);
1086         if (cfg)
1087                 OBD_FREE_PTR(cfg);
1088
1089         return rc;
1090 }
1091
1092 static DEFINE_MUTEX(server_start_lock);
1093
1094 /* Stop MDS/OSS if nobody is using them */
1095 static int server_stop_servers(int lsiflags)
1096 {
1097         struct obd_device *obd = NULL;
1098         struct obd_type *type = NULL;
1099         int rc = 0;
1100         bool type_last;
1101
1102         ENTRY;
1103         mutex_lock(&server_start_lock);
1104
1105         /* Either an MDT or an OST or neither  */
1106         /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1107         if (lsiflags & LDD_F_SV_TYPE_MDT) {
1108                 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1109                 type = class_search_type(LUSTRE_MDT_NAME);
1110         } else if (lsiflags & LDD_F_SV_TYPE_OST) {
1111         /* if this was an OST, and there are no more OST's, clean up the OSS */
1112                 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1113                 type = class_search_type(LUSTRE_OST_NAME);
1114         }
1115
1116         /* server_stop_servers is a pair of server_start_targets
1117          * Here we put type which was taken at server_start_targets.
1118          * If type is NULL then there is a wrong logic around type or
1119          * type reference.
1120          */
1121         LASSERTF(type, "Server flags %d, obd %s\n", lsiflags,
1122                  obd ? obd->obd_name : "NULL");
1123
1124         type_last = (atomic_read(&type->typ_refcnt) == 1);
1125
1126         class_put_type(type);
1127         if (obd && type_last) {
1128                 obd->obd_force = 1;
1129                 /* obd_fail doesn't mean much on a server obd */
1130                 rc = class_manual_cleanup(obd);
1131         }
1132
1133         /* put reference taken by class_search_type */
1134         kobject_put(&type->typ_kobj);
1135
1136         mutex_unlock(&server_start_lock);
1137
1138         RETURN(rc);
1139 }
1140
1141 int server_mti_print(const char *title, struct mgs_target_info *mti)
1142 {
1143         PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1144         PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1145         PRINT_CMD(PRINT_MASK, "fs:     %s\n", mti->mti_fsname);
1146         PRINT_CMD(PRINT_MASK, "uuid:   %s\n", mti->mti_uuid);
1147         PRINT_CMD(PRINT_MASK, "ver: %d  flags: %#x\n",
1148                   mti->mti_config_ver, mti->mti_flags);
1149         return 0;
1150 }
1151
1152 /* Generate data for registration */
1153 static int server_lsi2mti(struct lustre_sb_info *lsi,
1154                           struct mgs_target_info *mti)
1155 {
1156         struct lnet_processid id;
1157         int rc, i = 0;
1158         int cplen = 0;
1159
1160         ENTRY;
1161         if (!IS_SERVER(lsi))
1162                 RETURN(-EINVAL);
1163
1164         if (strlcpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname))
1165             >= sizeof(mti->mti_svname))
1166                 RETURN(-E2BIG);
1167
1168         mti->mti_nid_count = 0;
1169         while (LNetGetId(i++, &id) != -ENOENT) {
1170                 if (nid_is_lo0(&id.nid))
1171                         continue;
1172
1173                 /* server use --servicenode param, only allow specified
1174                  * nids be registered
1175                  */
1176                 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) != 0 &&
1177                     class_match_nid(lsi->lsi_lmd->lmd_params,
1178                                     PARAM_FAILNODE,
1179                                     lnet_nid_to_nid4(&id.nid)) < 1)
1180                         continue;
1181
1182                 /* match specified network */
1183                 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1184                                      PARAM_NETWORK, LNET_NID_NET(&id.nid)))
1185                         continue;
1186
1187                 mti->mti_nids[mti->mti_nid_count] = lnet_nid_to_nid4(&id.nid);
1188                 mti->mti_nid_count++;
1189                 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1190                         CWARN("Only using first %d nids for %s\n",
1191                               mti->mti_nid_count, mti->mti_svname);
1192                         break;
1193                 }
1194         }
1195
1196         if (mti->mti_nid_count == 0) {
1197                 CERROR("Failed to get NID for server %s, please check whether the target is specifed with improper --servicenode or --network options.\n",
1198                        mti->mti_svname);
1199                 RETURN(-EINVAL);
1200         }
1201
1202         mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1203         mti->mti_config_ver = 0;
1204
1205         rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1206         if (rc != 0)
1207                 return rc;
1208
1209         rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1210         if (rc < 0)
1211                 return rc;
1212         /* Orion requires index to be set */
1213         LASSERT(!(rc & LDD_F_NEED_INDEX));
1214         /* keep only LDD flags */
1215         mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1216         if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1217                 mti->mti_flags |= LDD_F_UPDATE;
1218         cplen = strlcpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1219                         sizeof(mti->mti_params));
1220         if (cplen >= sizeof(mti->mti_params))
1221                 return -E2BIG;
1222         return 0;
1223 }
1224
1225 /* Register an old or new target with the MGS. If needed MGS will construct
1226  * startup logs and assign index
1227  */
1228 static int server_register_target(struct lustre_sb_info *lsi)
1229 {
1230         struct obd_device *mgc = lsi->lsi_mgc;
1231         struct mgs_target_info *mti = NULL;
1232         bool must_succeed;
1233         int rc;
1234         int tried = 0;
1235
1236         ENTRY;
1237         LASSERT(mgc);
1238
1239         if (!IS_SERVER(lsi))
1240                 RETURN(-EINVAL);
1241
1242         OBD_ALLOC_PTR(mti);
1243         if (!mti)
1244                 RETURN(-ENOMEM);
1245
1246         rc = server_lsi2mti(lsi, mti);
1247         if (rc < 0)
1248                 GOTO(out, rc);
1249
1250         CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1251                mti->mti_svname, mti->mti_fsname,
1252                libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1253                mti->mti_flags);
1254
1255         /* we cannot ignore registration failure if MGS logs must be updated. */
1256         must_succeed = !!(lsi->lsi_flags &
1257                     (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_WRITECONF |
1258                      LDD_F_VIRGIN));
1259         mti->mti_flags |= LDD_F_OPC_REG;
1260
1261 again:
1262         /* Register the target */
1263         /* FIXME use mgc_process_config instead */
1264         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1265                                 sizeof(KEY_REGISTER_TARGET),
1266                                 KEY_REGISTER_TARGET,
1267                                 sizeof(*mti), mti, NULL);
1268         if (rc < 0) {
1269                 if (mti->mti_flags & LDD_F_ERROR) {
1270                         LCONSOLE_ERROR_MSG(0x160,
1271                                            "%s: the MGS refuses to allow this server to start: rc = %d. Please see messages on the MGS.\n",
1272                                            lsi->lsi_svname, rc);
1273                 } else if (must_succeed) {
1274                         if ((rc == -ESHUTDOWN || rc == -EIO) && ++tried < 5) {
1275                                 /* The connection with MGS is not established.
1276                                  * Try again after 2 seconds. Interruptable.
1277                                  */
1278                                 schedule_timeout_interruptible(cfs_time_seconds(2));
1279                                 if (!signal_pending(current))
1280                                         goto again;
1281                         }
1282
1283                         LCONSOLE_ERROR_MSG(0x15f,
1284                                            "%s: cannot register this server with the MGS: rc = %d. Is the MGS running?\n",
1285                                            lsi->lsi_svname, rc);
1286                 } else {
1287                         CDEBUG(D_HA,
1288                                "%s: error registering with the MGS: rc = %d (not fatal)\n",
1289                                lsi->lsi_svname, rc);
1290                         /* reset the error code for non-fatal error. */
1291                         rc = 0;
1292                 }
1293                 GOTO(out, rc);
1294         }
1295
1296 out:
1297         if (mti)
1298                 OBD_FREE_PTR(mti);
1299         RETURN(rc);
1300 }
1301
1302 /**
1303  * Notify the MGS that this target is ready.
1304  * Used by IR - if the MGS receives this message, it will notify clients.
1305  */
1306 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1307 {
1308         struct lustre_sb_info *lsi = s2lsi(sb);
1309         struct obd_device *mgc = lsi->lsi_mgc;
1310         struct mgs_target_info *mti = NULL;
1311         int rc;
1312
1313         ENTRY;
1314         LASSERT(mgc);
1315
1316         if (!(IS_SERVER(lsi)))
1317                 RETURN(-EINVAL);
1318
1319         OBD_ALLOC_PTR(mti);
1320         if (!mti)
1321                 RETURN(-ENOMEM);
1322         rc = server_lsi2mti(lsi, mti);
1323         if (rc < 0)
1324                 GOTO(out, rc);
1325
1326         mti->mti_instance = obd->u.obt.obt_instance;
1327         mti->mti_flags |= LDD_F_OPC_READY;
1328
1329         /* FIXME use mgc_process_config instead */
1330         rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1331                                 sizeof(KEY_REGISTER_TARGET),
1332                                 KEY_REGISTER_TARGET,
1333                                 sizeof(*mti), mti, NULL);
1334
1335         /* Imperative recovery: if the mgs informs us to use IR? */
1336         if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1337             (mti->mti_flags & LDD_F_IR_CAPABLE))
1338                 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1339
1340 out:
1341         if (mti)
1342                 OBD_FREE_PTR(mti);
1343         RETURN(rc);
1344
1345 }
1346
1347 /* Start server targets: MDTs and OSTs */
1348 static int server_start_targets(struct super_block *sb)
1349 {
1350         struct obd_device *obd;
1351         struct lustre_sb_info *lsi = s2lsi(sb);
1352         struct config_llog_instance cfg;
1353         struct lu_env mgc_env;
1354         struct lu_device *dev;
1355         char *name_service, *obd_name_service = NULL;
1356         struct obd_type *type = NULL;
1357         int rc;
1358
1359         ENTRY;
1360         CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1361
1362         LASSERTF(IS_MDT(lsi) || IS_OST(lsi), "designed for MDT or OST only\n");
1363
1364         if (IS_MDT(lsi)) {
1365                 obd_name_service = LUSTRE_MDS_OBDNAME;
1366                 name_service = LUSTRE_MDS_NAME;
1367         } else {
1368                 obd_name_service = LUSTRE_OSS_OBDNAME;
1369                 name_service = LUSTRE_OSS_NAME;
1370         }
1371
1372         /* make sure MDS/OSS is started */
1373         mutex_lock(&server_start_lock);
1374         obd = class_name2obd(obd_name_service);
1375         if (!obd) {
1376                 rc = lustre_start_simple(obd_name_service, name_service,
1377                                          (IS_MDT(lsi) ?
1378                                           LUSTRE_MDS_OBDNAME"_uuid" :
1379                                           LUSTRE_OSS_OBDNAME"_uuid"),
1380                                          NULL, NULL, NULL, NULL);
1381                 if (rc < 0) {
1382                         mutex_unlock(&server_start_lock);
1383                         CERROR("failed to start %s: %d\n",
1384                                obd_name_service, rc);
1385                         RETURN(rc);
1386                 }
1387         }
1388         /* hold a type reference and put it at server_stop_servers */
1389         type = class_get_type(IS_MDT(lsi) ?
1390                               LUSTRE_MDT_NAME : LUSTRE_OST_NAME);
1391         if (!type) {
1392                 mutex_unlock(&server_start_lock);
1393                 GOTO(out_stop_service, rc = -ENODEV);
1394         }
1395         lsi->lsi_server_started = 1;
1396         mutex_unlock(&server_start_lock);
1397         if (OBD_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE) &&
1398             IS_MDT(lsi)) {
1399                 OBD_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1400                 msleep(2 * MSEC_PER_SEC);
1401         }
1402
1403         rc = lu_env_init(&mgc_env, LCT_MG_THREAD);
1404         if (rc != 0)
1405                 GOTO(out_stop_service, rc);
1406
1407         /* Set the mgc fs to our server disk.  This allows the MGC to
1408          * read and write configs locally, in case it can't talk to the MGS.
1409          */
1410         rc = server_mgc_set_fs(&mgc_env, lsi->lsi_mgc, sb);
1411         if (rc < 0)
1412                 GOTO(out_env, rc);
1413
1414         /* Register with MGS */
1415         rc = server_register_target(lsi);
1416         if (rc < 0)
1417                 GOTO(out_mgc, rc);
1418
1419         /* Let the target look up the mount using the target's name
1420          * (we can't pass the sb or mnt through class_process_config.)
1421          */
1422         rc = server_register_mount(lsi->lsi_svname, sb);
1423         if (rc < 0)
1424                 GOTO(out_mgc, rc);
1425
1426         /* Start targets using the llog named for the target */
1427         memset(&cfg, 0, sizeof(cfg));
1428         cfg.cfg_callback = class_config_llog_handler;
1429         cfg.cfg_sub_clds = CONFIG_SUB_SERVER;
1430         rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1431         if (rc < 0) {
1432                 CERROR("failed to start server %s: %d\n",
1433                        lsi->lsi_svname, rc);
1434                 /* Do NOT call server_deregister_mount() here. This makes it
1435                  * impossible to find mount later in cleanup time and leaves
1436                  * @lsi and othder stuff leaked. -umka
1437                  */
1438                 GOTO(out_mgc, rc);
1439         }
1440
1441         obd = class_name2obd(lsi->lsi_svname);
1442         if (!obd) {
1443                 CERROR("no server named %s was started\n", lsi->lsi_svname);
1444                 GOTO(out_mgc, rc = -ENXIO);
1445         }
1446
1447         if (IS_OST(lsi) || IS_MDT(lsi)) {
1448                 rc = lustre_start_lwp(sb);
1449                 if (rc < 0) {
1450                         CERROR("%s: failed to start LWP: %d\n",
1451                                lsi->lsi_svname, rc);
1452                         GOTO(out_mgc, rc);
1453                 }
1454         }
1455
1456         server_notify_target(sb, obd);
1457
1458         /* calculate recovery timeout, do it after lustre_process_log */
1459         server_calc_timeout(lsi, obd);
1460
1461         /* log has been fully processed, let clients connect */
1462         dev = obd->obd_lu_dev;
1463         if (dev && dev->ld_ops->ldo_prepare) {
1464                 struct lu_env env;
1465
1466                 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1467                 if (rc == 0) {
1468                         struct lu_context  session_ctx;
1469
1470                         lu_context_init(&session_ctx, LCT_SERVER_SESSION);
1471                         session_ctx.lc_thread = NULL;
1472                         lu_context_enter(&session_ctx);
1473                         env.le_ses = &session_ctx;
1474
1475                         rc = dev->ld_ops->ldo_prepare(&env, NULL, dev);
1476
1477                         lu_env_fini(&env);
1478                         lu_context_exit(&session_ctx);
1479                         lu_context_fini(&session_ctx);
1480                 }
1481         }
1482
1483         /* abort recovery only on the complete stack:
1484          * many devices can be involved
1485          */
1486         if ((lsi->lsi_lmd->lmd_flags &
1487              (LMD_FLG_ABORT_RECOV | LMD_FLG_ABORT_RECOV_MDT)) &&
1488             (OBP(obd, iocontrol))) {
1489                 struct obd_ioctl_data karg = {
1490                         .ioc_type = lsi->lsi_lmd->lmd_flags,
1491                 };
1492
1493                 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1494                               &karg, NULL);
1495         }
1496
1497 out_mgc:
1498         /* Release the mgc fs for others to use */
1499         server_mgc_clear_fs(&mgc_env, lsi->lsi_mgc);
1500 out_env:
1501         lu_env_fini(&mgc_env);
1502 out_stop_service:
1503         /* in case of error upper function call
1504          * server_put_super->server_stop_servers()
1505          */
1506
1507         RETURN(rc);
1508 }
1509
1510 static int lsi_prepare(struct lustre_sb_info *lsi)
1511 {
1512         const char *osd_type;
1513         const char *fstype;
1514         u32 index;
1515         int rc;
1516
1517         ENTRY;
1518         LASSERT(lsi);
1519         LASSERT(lsi->lsi_lmd);
1520
1521         /* The server name is given as a mount line option */
1522         if (!lsi->lsi_lmd->lmd_profile) {
1523                 LCONSOLE_ERROR("Can't determine server name\n");
1524                 RETURN(-EINVAL);
1525         }
1526
1527         /* Determine osd type */
1528         if (!lsi->lsi_lmd->lmd_osd_type) {
1529                 osd_type = LUSTRE_OSD_LDISKFS_NAME;
1530                 fstype = "ldiskfs";
1531         } else {
1532                 osd_type = lsi->lsi_lmd->lmd_osd_type;
1533                 fstype = lsi->lsi_lmd->lmd_osd_type;
1534         }
1535
1536         if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname) ||
1537             strlen(osd_type) >= sizeof(lsi->lsi_osd_type) ||
1538             strlen(fstype) >= sizeof(lsi->lsi_fstype))
1539                 RETURN(-ENAMETOOLONG);
1540
1541         strlcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile,
1542                 sizeof(lsi->lsi_svname));
1543         strlcpy(lsi->lsi_osd_type, osd_type, sizeof(lsi->lsi_osd_type));
1544         /* XXX: a temp. solution for components using ldiskfs
1545          *      to be removed in one of the subsequent patches
1546          */
1547         strlcpy(lsi->lsi_fstype, fstype, sizeof(lsi->lsi_fstype));
1548
1549         /* Determine server type */
1550         rc = server_name2index(lsi->lsi_svname, &index, NULL);
1551         if (rc < 0) {
1552                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) {
1553                         /* Assume we're a bare MGS */
1554                         rc = 0;
1555                         lsi->lsi_lmd->lmd_flags |= LMD_FLG_NOSVC;
1556                 } else {
1557                         LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1558                                        lsi->lsi_svname);
1559                         RETURN(rc);
1560                 }
1561         }
1562         lsi->lsi_flags |= rc;
1563
1564         /* Add mount line flags that used to be in ldd:
1565          * writeconf, mgs, anything else?
1566          */
1567         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_WRITECONF) ?
1568                 LDD_F_WRITECONF : 0;
1569         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_LOCAL_LOGS) ?
1570                 LDD_F_NO_LOCAL_LOGS : 0;
1571         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_VIRGIN) ?
1572                 LDD_F_VIRGIN : 0;
1573         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_UPDATE) ?
1574                 LDD_F_UPDATE : 0;
1575         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_MGS) ?
1576                 LDD_F_SV_TYPE_MGS : 0;
1577         lsi->lsi_flags |= (lsi->lsi_lmd->lmd_flags & LMD_FLG_NO_PRIMNODE) ?
1578                 LDD_F_NO_PRIMNODE : 0;
1579
1580         RETURN(0);
1581 }
1582
1583 /*************** server mount ******************/
1584
1585 /** Start the shutdown of servers at umount.
1586  */
1587 static void server_put_super(struct super_block *sb)
1588 {
1589         struct lustre_sb_info *lsi = s2lsi(sb);
1590         struct obd_device *obd;
1591         char *tmpname, *extraname = NULL;
1592         int tmpname_sz;
1593         int lsiflags = lsi->lsi_flags;
1594         bool stop_servers = lsi->lsi_server_started;
1595
1596         ENTRY;
1597         LASSERT(IS_SERVER(lsi));
1598
1599         tmpname_sz = strlen(lsi->lsi_svname) + 1;
1600         OBD_ALLOC(tmpname, tmpname_sz);
1601         memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1602         CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1603         if (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC))
1604                 snprintf(tmpname, tmpname_sz, "MGS");
1605
1606         /* disconnect the lwp first to drain off the inflight request */
1607         if (IS_OST(lsi) || IS_MDT(lsi)) {
1608                 int     rc;
1609
1610                 rc = lustre_disconnect_lwp(sb);
1611                 if (rc != 0 && rc != -ETIMEDOUT &&
1612                     rc != -ENOTCONN && rc != -ESHUTDOWN)
1613                         CWARN("%s: failed to disconnect lwp: rc= %d\n",
1614                               tmpname, rc);
1615         }
1616
1617         /* Stop the target */
1618         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
1619             (IS_MDT(lsi) || IS_OST(lsi))) {
1620                 struct lustre_profile *lprof = NULL;
1621
1622                 /* tell the mgc to drop the config log */
1623                 lustre_end_log(sb, lsi->lsi_svname, NULL);
1624
1625                 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1626                  * If there are any setup/cleanup errors, save the lov
1627                  * name for safety cleanup later.
1628                  */
1629                 lprof = class_get_profile(lsi->lsi_svname);
1630                 if (lprof) {
1631                         if (lprof->lp_dt) {
1632                                 OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1633                                 strncpy(extraname, lprof->lp_dt,
1634                                         strlen(lprof->lp_dt) + 1);
1635                         }
1636                         class_put_profile(lprof);
1637                 }
1638
1639                 obd = class_name2obd(lsi->lsi_svname);
1640                 if (obd) {
1641                         CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1642                         if (lsiflags & LSI_UMOUNT_FAILOVER)
1643                                 obd->obd_fail = 1;
1644                         /* We can't seem to give an error return code
1645                          * to .put_super, so we better make sure we clean up!
1646                          */
1647                         obd->obd_force = 1;
1648                         class_manual_cleanup(obd);
1649                         if (OBD_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE)) {
1650                                 int idx;
1651
1652                                 server_name2index(lsi->lsi_svname, &idx, NULL);
1653                                 /* sleeping for MDT0001 */
1654                                 if (idx == 1)
1655                                         OBD_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1656                         }
1657                 } else {
1658                         CERROR("no obd %s\n", lsi->lsi_svname);
1659                         server_deregister_mount(lsi->lsi_svname);
1660                 }
1661         }
1662
1663         /* If they wanted the mgs to stop separately from the mdt, they
1664          * should have put it on a different device.
1665          */
1666         if (IS_MGS(lsi)) {
1667                 /* if MDS start with --nomgs, don't stop MGS then */
1668                 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS))
1669                         server_stop_mgs(sb);
1670         }
1671
1672         if (IS_OST(lsi) || IS_MDT(lsi)) {
1673                 if (lustre_stop_lwp(sb) < 0)
1674                         CERROR("%s: failed to stop lwp!\n", tmpname);
1675         }
1676
1677         /* Clean the mgc and sb */
1678         lustre_common_put_super(sb);
1679
1680         /* wait till all in-progress cleanups are done
1681          * specifically we're interested in ofd cleanup
1682          * as it pins OSS
1683          */
1684         obd_zombie_barrier();
1685
1686         /* Stop the servers (MDS, OSS) if no longer needed.  We must wait
1687          * until the target is really gone so that our type refcount check
1688          * is right.
1689          */
1690         if (stop_servers)
1691                 server_stop_servers(lsiflags);
1692
1693         /* In case of startup or cleanup err, stop related obds */
1694         if (extraname) {
1695                 obd = class_name2obd(extraname);
1696                 if (obd) {
1697                         CWARN("Cleaning orphaned obd %s\n", extraname);
1698                         obd->obd_force = 1;
1699                         class_manual_cleanup(obd);
1700                 }
1701                 OBD_FREE(extraname, strlen(extraname) + 1);
1702         }
1703
1704         LCONSOLE_WARN("server umount %s complete\n", tmpname);
1705         OBD_FREE(tmpname, tmpname_sz);
1706         EXIT;
1707 }
1708
1709 /* Called only for 'umount -f' */
1710 static void server_umount_begin(struct super_block *sb)
1711 {
1712         struct lustre_sb_info *lsi = s2lsi(sb);
1713
1714         ENTRY;
1715         CDEBUG(D_MOUNT, "umount -f\n");
1716         /* umount = failover
1717          * umount -f = force
1718          * no third way to do non-force, non-failover
1719          */
1720         lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1721         EXIT;
1722 }
1723
1724 static int server_statfs(struct dentry *dentry, struct kstatfs *buf)
1725 {
1726         struct super_block *sb = dentry->d_sb;
1727         struct lustre_sb_info *lsi = s2lsi(sb);
1728         struct obd_statfs statfs;
1729         int rc;
1730
1731         ENTRY;
1732         if (lsi->lsi_dt_dev) {
1733                 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1734                 if (rc == 0) {
1735                         statfs_unpack(buf, &statfs);
1736                         buf->f_type = sb->s_magic;
1737                         RETURN(0);
1738                 }
1739         }
1740
1741         /* just return 0 */
1742         buf->f_type = sb->s_magic;
1743         buf->f_bsize = sb->s_blocksize;
1744         buf->f_blocks = 1;
1745         buf->f_bfree = 0;
1746         buf->f_bavail = 0;
1747         buf->f_files = 1;
1748         buf->f_ffree = 0;
1749         buf->f_namelen = NAME_MAX;
1750         RETURN(0);
1751 }
1752
1753 int server_show_options(struct seq_file *seq, struct dentry *dentry)
1754 {
1755         struct lustre_sb_info *lsi;
1756         struct lustre_mount_data *lmd;
1757
1758         LASSERT(seq && dentry);
1759         lsi = s2lsi(dentry->d_sb);
1760         lmd = lsi->lsi_lmd;
1761         seq_printf(seq, ",svname=%s", lmd->lmd_profile);
1762
1763         if  (lmd->lmd_flags & LMD_FLG_ABORT_RECOV)
1764                 seq_puts(seq, ",abort_recov");
1765
1766         if (lmd->lmd_flags & LMD_FLG_NOIR)
1767                 seq_puts(seq, ",noir");
1768
1769         if (lmd->lmd_flags & LMD_FLG_NOSVC)
1770                 seq_puts(seq, ",nosvc");
1771
1772         if (lmd->lmd_flags & LMD_FLG_NOMGS)
1773                 seq_puts(seq, ",nomgs");
1774
1775         if (lmd->lmd_flags & LMD_FLG_NOSCRUB)
1776                 seq_puts(seq, ",noscrub");
1777         if (lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
1778                 seq_puts(seq, ",skip_lfsck");
1779
1780         if (lmd->lmd_flags & LMD_FLG_DEV_RDONLY)
1781                 seq_puts(seq, ",rdonly_dev");
1782
1783         if (lmd->lmd_flags & LMD_FLG_MGS)
1784                 seq_puts(seq, ",mgs");
1785
1786         if (lmd->lmd_mgs)
1787                 seq_printf(seq, ",mgsnode=%s", lmd->lmd_mgs);
1788
1789         if (lmd->lmd_osd_type)
1790                 seq_printf(seq, ",osd=%s", lmd->lmd_osd_type);
1791
1792         if (lmd->lmd_opts) {
1793                 seq_putc(seq, ',');
1794                 seq_puts(seq, lmd->lmd_opts);
1795         }
1796
1797         RETURN(0);
1798 }
1799
1800 /** The operations we support directly on the superblock:
1801  * mount, umount, and df.
1802  */
1803 static const struct super_operations server_ops = {
1804         .put_super      = server_put_super,
1805         .umount_begin   = server_umount_begin, /* umount -f */
1806         .statfs         = server_statfs,
1807         .show_options   = server_show_options,
1808 };
1809
1810 /*
1811  * Xattr support for Lustre servers
1812  */
1813 #ifdef HAVE_IOP_XATTR
1814 static ssize_t lustre_getxattr(struct dentry *dentry, const char *name,
1815                                 void *buffer, size_t size)
1816 {
1817         if (!selinux_is_enabled())
1818                 return -EOPNOTSUPP;
1819         return -ENODATA;
1820 }
1821
1822 static int lustre_setxattr(struct dentry *dentry, const char *name,
1823                             const void *value, size_t size, int flags)
1824 {
1825         return -EOPNOTSUPP;
1826 }
1827 #endif
1828
1829 static ssize_t lustre_listxattr(struct dentry *d_entry, char *name,
1830                                 size_t size)
1831 {
1832         return -EOPNOTSUPP;
1833 }
1834
1835 static bool is_cmd_supported(unsigned int command)
1836 {
1837         switch (command) {
1838         case FITRIM:
1839                 return true;
1840         default:
1841                 return false;
1842         }
1843
1844         return false;
1845 }
1846
1847 static long server_ioctl(struct file *filp, unsigned int command,
1848                          unsigned long arg)
1849 {
1850         struct file active_filp;
1851         struct inode *inode = file_inode(filp);
1852         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
1853         struct super_block *dd_sb = dt_mnt_sb_get(lsi->lsi_dt_dev);
1854         struct inode *active_inode;
1855         int err = -EOPNOTSUPP;
1856
1857         if (IS_ERR(dd_sb) || !is_cmd_supported(command))
1858                 return err;
1859
1860         active_inode = igrab(dd_sb->s_root->d_inode);
1861         if (!active_inode)
1862                 return -EACCES;
1863
1864         active_filp.f_inode = active_inode;
1865         if (active_inode->i_fop && active_inode->i_fop->unlocked_ioctl)
1866                 err = active_inode->i_fop->unlocked_ioctl(&active_filp,
1867                                                           command, arg);
1868         iput(active_inode);
1869         return err;
1870 }
1871
1872 static const struct inode_operations server_inode_operations = {
1873 #ifdef HAVE_IOP_XATTR
1874         .setxattr       = lustre_setxattr,
1875         .getxattr       = lustre_getxattr,
1876 #endif
1877         .listxattr      = lustre_listxattr,
1878 };
1879
1880 static const struct file_operations server_file_operations = {
1881         .unlocked_ioctl = server_ioctl,
1882 };
1883
1884 #define log2(n) ffz(~(n))
1885 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1886
1887 static int server_fill_super_common(struct super_block *sb)
1888 {
1889         struct inode *root = NULL;
1890
1891         ENTRY;
1892         CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1893
1894         sb->s_blocksize = 4096;
1895         sb->s_blocksize_bits = log2(sb->s_blocksize);
1896         sb->s_magic = LUSTRE_SUPER_MAGIC;
1897         sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
1898         sb->s_flags |= SB_RDONLY;
1899         sb->s_op = &server_ops;
1900
1901         root = new_inode(sb);
1902         if (!root) {
1903                 CERROR("Can't make root inode\n");
1904                 RETURN(-EIO);
1905         }
1906
1907         /* returns -EIO for every operation */
1908         /* make_bad_inode(root); -- badness - can't umount */
1909         /* apparently we need to be a directory for the mount to finish */
1910         root->i_mode = S_IFDIR;
1911         root->i_op = &server_inode_operations;
1912         root->i_fop = &server_file_operations;
1913         sb->s_root = d_make_root(root);
1914         if (!sb->s_root) {
1915                 CERROR("%s: can't make root dentry\n", sb->s_id);
1916                 RETURN(-EIO);
1917         }
1918
1919         RETURN(0);
1920 }
1921
1922 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
1923 {
1924         struct lustre_mount_data *lmd = lsi->lsi_lmd;
1925         struct obd_device *obd;
1926         struct dt_device_param p;
1927         char flagstr[20 + 1 + 10 + 1];
1928         int rc;
1929
1930         ENTRY;
1931         CDEBUG(D_MOUNT,
1932                "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
1933                lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
1934
1935         sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
1936         strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
1937         strcat(lsi->lsi_osd_uuid, "_UUID");
1938         snprintf(flagstr, sizeof(flagstr), "%lu:%u", mflags, lmd->lmd_flags);
1939
1940         obd = class_name2obd(lsi->lsi_osd_obdname);
1941         if (!obd) {
1942                 rc = lustre_start_simple(lsi->lsi_osd_obdname,
1943                                          lsi->lsi_osd_type,
1944                                          lsi->lsi_osd_uuid, lmd->lmd_dev,
1945                                          flagstr, lsi->lsi_lmd->lmd_opts,
1946                                          lsi->lsi_svname);
1947                 if (rc < 0)
1948                         GOTO(out, rc);
1949                 obd = class_name2obd(lsi->lsi_osd_obdname);
1950                 LASSERT(obd);
1951         } else {
1952                 CDEBUG(D_MOUNT, "%s already started\n", lsi->lsi_osd_obdname);
1953                 /* but continue setup to allow special case of MDT and internal
1954                  * MGT being started separately.
1955                  */
1956                 if (!((IS_MGS(lsi) && (lsi->lsi_lmd->lmd_flags &
1957                                       LMD_FLG_NOMGS)) ||
1958                      (IS_MDT(lsi) && (lsi->lsi_lmd->lmd_flags &
1959                                       LMD_FLG_NOSVC))))
1960                         RETURN(-EALREADY);
1961         }
1962
1963         rc = obd_connect(NULL, &lsi->lsi_osd_exp,
1964                          obd, &obd->obd_uuid, NULL, NULL);
1965
1966         if (rc < 0) {
1967                 obd->obd_force = 1;
1968                 class_manual_cleanup(obd);
1969                 lsi->lsi_dt_dev = NULL;
1970                 RETURN(rc);
1971         }
1972
1973         LASSERT(obd->obd_lu_dev);
1974         lu_device_get(obd->obd_lu_dev);
1975         lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
1976         LASSERT(lsi->lsi_dt_dev);
1977
1978         /* set disk context for llog usage */
1979         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1980         obd->obd_lvfs_ctxt.dt = lsi->lsi_dt_dev;
1981
1982         dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
1983 out:
1984         RETURN(rc);
1985 }
1986
1987 /** Fill in the superblock info for a Lustre server.
1988  * Mount the device with the correct options.
1989  * Read the on-disk config file.
1990  * Start the services.
1991  */
1992 int server_fill_super(struct super_block *sb)
1993 {
1994         struct lustre_sb_info *lsi = s2lsi(sb);
1995         int rc;
1996
1997         ENTRY;
1998         /* to simulate target mount race */
1999         OBD_RACE(OBD_FAIL_TGT_MOUNT_RACE);
2000
2001         rc = lsi_prepare(lsi);
2002         if (rc < 0) {
2003                 lustre_put_lsi(sb);
2004                 RETURN(rc);
2005         }
2006
2007         /* Start low level OSD */
2008         rc = osd_start(lsi, sb->s_flags);
2009         if (rc < 0) {
2010                 CERROR("Unable to start osd on %s: %d\n",
2011                        lsi->lsi_lmd->lmd_dev, rc);
2012                 lustre_put_lsi(sb);
2013                 RETURN(rc);
2014         }
2015
2016         CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2017                lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2018
2019         if (class_name2obd(lsi->lsi_svname)) {
2020                 LCONSOLE_ERROR_MSG(0x161,
2021                                    "The target named %s is already running. Double-mount may have compromised the disk journal.\n",
2022                                    lsi->lsi_svname);
2023                 lustre_put_lsi(sb);
2024                 RETURN(-EALREADY);
2025         }
2026
2027         /* Start MGS before MGC */
2028         if (IS_MGS(lsi) && !(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOMGS)) {
2029                 rc = server_start_mgs(sb);
2030                 if (rc < 0)
2031                         GOTO(out_mnt, rc);
2032         }
2033
2034         /* Start MGC before servers */
2035         rc = lustre_start_mgc(sb);
2036         if (rc < 0)
2037                 GOTO(out_mnt, rc);
2038
2039         /* Set up all obd devices for service */
2040         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOSVC) &&
2041             (IS_OST(lsi) || IS_MDT(lsi))) {
2042                 rc = server_start_targets(sb);
2043                 if (rc < 0) {
2044                         CERROR("Unable to start targets: %d\n", rc);
2045                         GOTO(out_mnt, rc);
2046                 }
2047                 /* FIXME overmount client here, or can we just start a
2048                  * client log and client_fill_super on this sb?  We
2049                  * need to make sure server_put_super gets called too
2050                  * - ll_put_super calls lustre_common_put_super; check
2051                  * there for LSI_SERVER flag, call s_p_s if so.
2052                  *
2053                  * Probably should start client from new thread so we
2054                  * can return.  Client will not finish until all
2055                  * servers are connected.  Note - MGS-only server does
2056                  * NOT get a client, since there is no lustre fs
2057                  * associated - the MGS is for all lustre fs's
2058                  */
2059         }
2060
2061         rc = server_fill_super_common(sb);
2062         if (rc < 0)
2063                 GOTO(out_mnt, rc);
2064
2065         RETURN(0);
2066 out_mnt:
2067         /* We jump here in case of failure while starting targets or MGS.
2068          * In this case we can't just put @mnt and have to do real cleanup
2069          * with stoping targets, etc.
2070          */
2071         server_put_super(sb);
2072         return rc;
2073 }
2074 EXPORT_SYMBOL(server_fill_super);
2075
2076 /*
2077  * Calculate timeout value for a target.
2078  */
2079 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2080 {
2081         struct lustre_mount_data *lmd;
2082         int soft = 0;
2083         int hard = 0;
2084         int factor = 0;
2085         bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2086         int min = OBD_RECOVERY_TIME_MIN;
2087
2088         LASSERT(IS_SERVER(lsi));
2089
2090         lmd = lsi->lsi_lmd;
2091         if (lmd) {
2092                 soft   = lmd->lmd_recovery_time_soft;
2093                 hard   = lmd->lmd_recovery_time_hard;
2094                 has_ir = has_ir && !(lmd->lmd_flags & LMD_FLG_NOIR);
2095                 obd->obd_no_ir = !has_ir;
2096         }
2097
2098         if (soft == 0)
2099                 soft = OBD_RECOVERY_TIME_SOFT;
2100         if (hard == 0)
2101                 hard = OBD_RECOVERY_TIME_HARD;
2102
2103         /* target may have ir_factor configured. */
2104         factor = OBD_IR_FACTOR_DEFAULT;
2105         if (obd->obd_recovery_ir_factor)
2106                 factor = obd->obd_recovery_ir_factor;
2107
2108         if (has_ir) {
2109                 int new_soft = soft;
2110
2111                 /* adjust timeout value by imperative recovery */
2112                 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2113                 /* make sure the timeout is not too short */
2114                 new_soft = max(min, new_soft);
2115
2116                 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery window shrunk from %d-%d down to %d-%d\n",
2117                               obd->obd_name, soft, hard, new_soft, hard);
2118
2119                 soft = new_soft;
2120         } else {
2121                 LCONSOLE_INFO("%s: Imperative Recovery not enabled, recovery window %d-%d\n",
2122                               obd->obd_name, soft, hard);
2123         }
2124
2125         /* we're done */
2126         obd->obd_recovery_timeout = max_t(time64_t, obd->obd_recovery_timeout,
2127                                           soft);
2128         obd->obd_recovery_time_hard = hard;
2129         obd->obd_recovery_ir_factor = factor;
2130 }
2131
2132 /**
2133  * This is the entry point for the mount call into Lustre.
2134  * This is called when a server target is mounted,
2135  * and this is where we start setting things up.
2136  * @param data Mount options (e.g. -o flock,abort_recov)
2137  */
2138 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
2139                                  int silent)
2140 {
2141         struct lustre_mount_data *lmd;
2142         struct lustre_sb_info *lsi;
2143         int rc;
2144
2145         ENTRY;
2146         CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2147
2148         lsi = lustre_init_lsi(sb);
2149         if (!lsi)
2150                 RETURN(-ENOMEM);
2151         lmd = lsi->lsi_lmd;
2152
2153         /*
2154          * Disable lockdep during mount, because mount locking patterns are
2155          * 'special'.
2156          */
2157         lockdep_off();
2158
2159         /*
2160          * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
2161          */
2162         obd_zombie_barrier();
2163
2164         /* Figure out the lmd from the mount options */
2165         if (lmd_parse(lmd2_data, lmd)) {
2166                 lustre_put_lsi(sb);
2167                 GOTO(out, rc = -EINVAL);
2168         }
2169
2170         if (lmd_is_client(lmd)) {
2171                 rc = -ENODEV;
2172                 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
2173                        lmd->lmd_dev, rc);
2174                 lustre_put_lsi(sb);
2175                 GOTO(out, rc);
2176         }
2177
2178         CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2179         rc = server_fill_super(sb);
2180         /*
2181          * server_fill_super calls lustre_start_mgc after the mount
2182          * because we need the MGS NIDs which are stored on disk.
2183          * Plus, we may need to start the MGS first.
2184          *
2185          * server_fill_super will call server_put_super on failure
2186          *
2187          * If error happens in fill_super() call, @lsi will be killed there.
2188          * This is why we do not put it here.
2189          */
2190 out:
2191         if (rc) {
2192                 CERROR("Unable to mount %s (%d)\n",
2193                        s2lsi(sb) ? lmd->lmd_dev : "", rc);
2194         } else {
2195                 CDEBUG(D_SUPER, "Mount %s complete\n",
2196                        lmd->lmd_dev);
2197         }
2198         lockdep_on();
2199         return rc;
2200 }
2201
2202 /***************** FS registration ******************/
2203 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
2204                                        int flags, const char *devname,
2205                                        void *data)
2206 {
2207         return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
2208 }
2209
2210 /* Register the "lustre_tgt" fs type.
2211  *
2212  * Right now this isn't any different than the normal "lustre" filesystem
2213  * type, but it is added so that there is some compatibility to allow
2214  * changing documentation and scripts to start using the "lustre_tgt" type
2215  * at mount time. That will simplify test interop, and in case of upgrades
2216  * that change to the new type and then need to roll back for some reason.
2217  *
2218  * The long-term goal is to disentangle the client and server mount code.
2219  */
2220 static struct file_system_type lustre_tgt_fstype = {
2221         .owner          = THIS_MODULE,
2222         .name           = "lustre_tgt",
2223         .mount          = lustre_tgt_mount,
2224         .kill_sb        = kill_anon_super,
2225         .fs_flags       = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
2226 };
2227 MODULE_ALIAS_FS("lustre_tgt");
2228
2229 int lustre_tgt_register_fs(void)
2230 {
2231         return register_filesystem(&lustre_tgt_fstype);
2232 }
2233
2234 void lustre_tgt_unregister_fs(void)
2235 {
2236         unregister_filesystem(&lustre_tgt_fstype);
2237 }