Whamcloud - gitweb
35efe5bd4273ab5d658387917ee8de488f375d15
[fs/lustre-release.git] / lustre / mgs / mgs_handler.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) 2010, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mgs/mgs_handler.c
33  *
34  * Author: Nathan Rutman <nathan@clusterfs.com>
35  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36  * Author: Mikhail Pershin <tappro@whamcloud.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MGS
40 #define D_MGS D_CONFIG
41
42 #include <obd_class.h>
43 #include <lprocfs_status.h>
44 #include <uapi/linux/lustre_ioctl.h>
45 #include <uapi/linux/lustre_param.h>
46 #include <lustre/lustre_barrier_user.h>
47
48 #include "mgs_internal.h"
49
50 /*
51  * Regular MGS handlers
52  */
53 static int mgs_connect(struct tgt_session_info *tsi)
54 {
55         struct ptlrpc_request   *req = tgt_ses_req(tsi);
56         int                      rc;
57
58         ENTRY;
59
60         CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, cfs_fail_val);
61         rc = tgt_connect(tsi);
62         if (rc)
63                 RETURN(rc);
64
65         if (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1)
66                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
67
68         RETURN(0);
69 }
70
71 static int mgs_disconnect(struct tgt_session_info *tsi)
72 {
73         int rc;
74
75         ENTRY;
76
77         LASSERT(tsi->tsi_exp);
78
79         rc = tgt_disconnect(tsi);
80         if (rc)
81                 RETURN(err_serious(rc));
82         RETURN(0);
83 }
84
85 static int mgs_exception(struct tgt_session_info *tsi)
86 {
87         ENTRY;
88
89         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_EXCEPTION);
90
91         RETURN(0);
92 }
93
94 static inline bool str_starts_with(const char *str, const char *prefix)
95 {
96         return strncmp(str, prefix, strlen(prefix)) == 0;
97 }
98
99 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
100 static int mgs_set_info(struct tgt_session_info *tsi)
101 {
102         struct mgs_thread_info  *mgi;
103         struct mgs_send_param   *msp, *rep_msp;
104         struct lustre_cfg       *lcfg;
105         size_t                   param_len;
106         char                    *s;
107         int                      rc;
108
109         ENTRY;
110
111         mgi = mgs_env_info(tsi->tsi_env);
112         if (IS_ERR(mgi))
113                 RETURN(err_serious(PTR_ERR(mgi)));
114
115         msp = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_SEND_PARAM);
116         if (msp == NULL)
117                 RETURN(err_serious(-EFAULT));
118
119         param_len = strnlen(msp->mgs_param, sizeof(msp->mgs_param));
120         if (param_len == 0 || param_len == sizeof(msp->mgs_param))
121                 RETURN(-EINVAL);
122
123         /* We only allow '*.lov.stripe{size,count,offset}=*' from an RPC. */
124         s = strchr(msp->mgs_param, '.');
125         if (s == NULL)
126                 RETURN(-EINVAL);
127
128         if (!str_starts_with(s + 1, "lov.stripesize=") &&
129             !str_starts_with(s + 1, "lov.stripecount=") &&
130             !str_starts_with(s + 1, "lov.stripeoffset="))
131                 RETURN(-EINVAL);
132
133         /* Construct lustre_cfg structure to pass to function mgs_setparam */
134         lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
135         lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
136         OBD_ALLOC(lcfg, lustre_cfg_len(mgi->mgi_bufs.lcfg_bufcount,
137                                        mgi->mgi_bufs.lcfg_buflen));
138         if (!lcfg)
139                 RETURN(-ENOMEM);
140         lustre_cfg_init(lcfg, LCFG_PARAM, &mgi->mgi_bufs);
141
142         rc = mgs_setparam(tsi->tsi_env, exp2mgs_dev(tsi->tsi_exp), lcfg,
143                           mgi->mgi_fsname);
144         if (rc) {
145                 LCONSOLE_WARN("%s: Unable to set parameter %s for %s: %d\n",
146                               tgt_name(tsi->tsi_tgt), msp->mgs_param,
147                               mgi->mgi_fsname, rc);
148                 GOTO(out_cfg, rc);
149         }
150
151         /* send back the whole msp in the reply */
152         rep_msp = req_capsule_server_get(tsi->tsi_pill, &RMF_MGS_SEND_PARAM);
153         *rep_msp = *msp;
154         EXIT;
155 out_cfg:
156         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
157         return rc;
158 }
159 #endif
160
161 enum ast_type {
162         AST_CONFIG      = 1,
163         AST_PARAMS      = 2,
164         AST_IR          = 3,
165         AST_BARRIER     = 4,
166 };
167
168 static int mgs_completion_ast_generic(struct ldlm_lock *lock, __u64 flags,
169                                      void *cbdata, enum ast_type type)
170 {
171         ENTRY;
172
173         if (!(flags & LDLM_FL_BLOCKED_MASK)) {
174                 struct fs_db *fsdb;
175
176                 /* l_ast_data is used as a marker to avoid cancel ldlm lock
177                  * twice. See LU-2317. */
178                 lock_res_and_lock(lock);
179                 fsdb = (struct fs_db *)lock->l_ast_data;
180                 lock->l_ast_data = NULL;
181                 unlock_res_and_lock(lock);
182
183                 if (fsdb != NULL) {
184                         struct lustre_handle lockh;
185
186                         switch(type) {
187                                 case AST_CONFIG:
188                                         /* clear the bit before lock put */
189                                         clear_bit(FSDB_REVOKING_LOCK,
190                                                   &fsdb->fsdb_flags);
191                                         break;
192                                 case AST_PARAMS:
193                                         clear_bit(FSDB_REVOKING_PARAMS,
194                                                   &fsdb->fsdb_flags);
195                                         break;
196                                 case AST_IR:
197                                         mgs_ir_notify_complete(fsdb);
198                                         break;
199                                 case AST_BARRIER:
200                                         break;
201                                 default:
202                                         LBUG();
203                         }
204
205                         ldlm_lock2handle(lock, &lockh);
206                         ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
207                 }
208         }
209
210         RETURN(ldlm_completion_ast(lock, flags, cbdata));
211 }
212
213 static int mgs_completion_ast_config(struct ldlm_lock *lock, __u64 flags,
214                                      void *cbdata)
215 {
216         return mgs_completion_ast_generic(lock, flags, cbdata, AST_CONFIG);
217 }
218
219 static int mgs_completion_ast_params(struct ldlm_lock *lock, __u64 flags,
220                                      void *cbdata)
221 {
222         return mgs_completion_ast_generic(lock, flags, cbdata, AST_PARAMS);
223 }
224
225 static int mgs_completion_ast_ir(struct ldlm_lock *lock, __u64 flags,
226                                  void *cbdata)
227 {
228         return mgs_completion_ast_generic(lock, flags, cbdata, AST_IR);
229 }
230
231 static int mgs_completion_ast_barrier(struct ldlm_lock *lock, __u64 flags,
232                                       void *cbdata)
233 {
234         return mgs_completion_ast_generic(lock, flags, cbdata, AST_BARRIER);
235 }
236
237 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
238 {
239         ldlm_completion_callback cp = NULL;
240         struct lustre_handle     lockh = {
241                 .cookie = 0,
242         };
243         struct ldlm_res_id       res_id;
244         __u64 flags = LDLM_FL_ATOMIC_CB;
245         int rc;
246         ENTRY;
247
248         LASSERT(fsdb->fsdb_name[0] != '\0');
249         rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
250         LASSERT(rc == 0);
251         switch (type) {
252         case CONFIG_T_CONFIG:
253         case CONFIG_T_NODEMAP:
254                 cp = mgs_completion_ast_config;
255                 if (test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
256                         rc = -EALREADY;
257                 break;
258         case CONFIG_T_PARAMS:
259                 cp = mgs_completion_ast_params;
260                 if (test_and_set_bit(FSDB_REVOKING_PARAMS, &fsdb->fsdb_flags))
261                         rc = -EALREADY;
262                 break;
263         case CONFIG_T_RECOVER:
264                 cp = mgs_completion_ast_ir;
265                 break;
266         case CONFIG_T_BARRIER:
267                 cp = mgs_completion_ast_barrier;
268                 break;
269         default:
270                 break;
271         }
272
273         if (!rc) {
274                 LASSERT(cp != NULL);
275                 rc = ldlm_cli_enqueue_local(mgs->mgs_obd->obd_namespace,
276                                             &res_id, LDLM_PLAIN, NULL, LCK_EX,
277                                             &flags, ldlm_blocking_ast, cp,
278                                             NULL, fsdb, 0, LVB_T_NONE, NULL,
279                                             &lockh);
280                 if (rc != ELDLM_OK) {
281                         CERROR("%s: can't take cfg lock for %#llx/%#llx : rc = %d\n",
282                                mgs->mgs_obd->obd_name,
283                                le64_to_cpu(res_id.name[0]),
284                                le64_to_cpu(res_id.name[1]), rc);
285
286                         if (type == CONFIG_T_CONFIG)
287                                 clear_bit(FSDB_REVOKING_LOCK,
288                                           &fsdb->fsdb_flags);
289
290                         if (type == CONFIG_T_PARAMS)
291                                 clear_bit(FSDB_REVOKING_PARAMS,
292                                           &fsdb->fsdb_flags);
293                 }
294                 /* lock has been cancelled in completion_ast. */
295         }
296
297         RETURN_EXIT;
298 }
299
300 /* rc=0 means ok
301       1 means update
302      <0 means error */
303 static int mgs_check_target(const struct lu_env *env,
304                             struct mgs_device *mgs,
305                             struct mgs_target_info *mti)
306 {
307         int rc;
308         ENTRY;
309
310         rc = mgs_check_index(env, mgs, mti);
311         if (rc == 0) {
312                 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
313                                    "this MGS does not know about it, preventing "
314                                    "registration.\n", mti->mti_svname);
315                 rc = -ENOENT;
316         } else if (rc == -1) {
317                 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
318                                    "disappeared! Regenerating all logs.\n",
319                                    mti->mti_fsname);
320                 mti->mti_flags |= LDD_F_WRITECONF;
321                 rc = 1;
322         } else {
323                 /* Index is correctly marked as used */
324                 rc = 0;
325         }
326
327         RETURN(rc);
328 }
329
330 /* Ensure this is not a failover node that is connecting first*/
331 static int mgs_check_failover_reg(struct mgs_target_info *mti)
332 {
333         lnet_nid_t nid;
334         char *ptr;
335         int i;
336
337         ptr = mti->mti_params;
338         while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
339                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
340                         for (i = 0; i < mti->mti_nid_count; i++) {
341                                 if (nid == mti->mti_nids[i]) {
342                                         LCONSOLE_WARN("Denying initial registra"
343                                                       "tion attempt from nid %s"
344                                                       ", specified as failover"
345                                                       "\n",libcfs_nid2str(nid));
346                                         return -EADDRNOTAVAIL;
347                                 }
348                         }
349                 }
350         }
351         return 0;
352 }
353
354 /* Called whenever a target starts up.  Flags indicate first connect, etc. */
355 static int mgs_target_reg(struct tgt_session_info *tsi)
356 {
357         struct obd_device *obd = tsi->tsi_exp->exp_obd;
358         struct mgs_device *mgs = exp2mgs_dev(tsi->tsi_exp);
359         struct mgs_target_info *mti, *rep_mti;
360         struct fs_db *b_fsdb = NULL; /* barrier fsdb */
361         struct fs_db *c_fsdb = NULL; /* config fsdb */
362         char barrier_name[20];
363         int opc;
364         int rc = 0;
365
366         ENTRY;
367
368         rc = lu_env_refill((struct lu_env *)tsi->tsi_env);
369         if (rc)
370                 return err_serious(rc);
371
372         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_TARGET_REG);
373
374         mti = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_TARGET_INFO);
375         if (mti == NULL) {
376                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no mgs_send_param");
377                 RETURN(err_serious(-EFAULT));
378         }
379
380         down_read(&mgs->mgs_barrier_rwsem);
381
382         if (OCD_HAS_FLAG(&tgt_ses_req(tsi)->rq_export->exp_connect_data,
383                          IMP_RECOV))
384                 opc = mti->mti_flags & LDD_F_OPC_MASK;
385         else
386                 opc = LDD_F_OPC_REG;
387
388         if (opc == LDD_F_OPC_READY) {
389                 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
390                         mti->mti_fsname, mti->mti_stripe_index);
391                 rc = mgs_ir_update(tsi->tsi_env, mgs, mti);
392                 if (rc) {
393                         LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
394                         CERROR("%s: Update IR return failure: rc = %d\n",
395                                mti->mti_fsname, rc);
396                 }
397
398                 GOTO(out_norevoke, rc);
399         }
400
401         /* Do not support unregistering right now. */
402         if (opc != LDD_F_OPC_REG)
403                 GOTO(out_norevoke, rc = -EINVAL);
404
405         snprintf(barrier_name, sizeof(barrier_name) - 1, "%s-%s",
406                  mti->mti_fsname, BARRIER_FILENAME);
407         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs, barrier_name, &b_fsdb);
408         if (rc) {
409                 CERROR("%s: Can't get db for %s: rc = %d\n",
410                        mti->mti_fsname, barrier_name, rc);
411
412                 GOTO(out_norevoke, rc);
413         }
414
415         CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
416                mti->mti_fsname, mti->mti_stripe_index);
417
418         if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
419                 if (b_fsdb->fsdb_barrier_status == BS_FREEZING_P1 ||
420                     b_fsdb->fsdb_barrier_status == BS_FREEZING_P2 ||
421                     b_fsdb->fsdb_barrier_status == BS_FROZEN) {
422                         LCONSOLE_WARN("%s: the system is in barrier, refuse "
423                                       "the connection from MDT %s temporary\n",
424                                       obd->obd_name, mti->mti_svname);
425
426                         GOTO(out_norevoke, rc = -EBUSY);
427                 }
428
429                 if (!(exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_BARRIER) &&
430                     !b_fsdb->fsdb_barrier_disabled) {
431                         LCONSOLE_WARN("%s: the MDT %s does not support write "
432                                       "barrier, so disable barrier on the "
433                                       "whole system.\n",
434                                       obd->obd_name, mti->mti_svname);
435
436                         b_fsdb->fsdb_barrier_disabled = 1;
437                 }
438         }
439
440         if (mti->mti_flags & LDD_F_NEED_INDEX)
441                 mti->mti_flags |= LDD_F_WRITECONF;
442
443         if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
444                                 LDD_F_UPDATE))) {
445                 /* We're just here as a startup ping. */
446                 CDEBUG(D_MGS, "Server %s is running on %s\n",
447                        mti->mti_svname, obd_export_nid2str(tsi->tsi_exp));
448                 rc = mgs_check_target(tsi->tsi_env, mgs, mti);
449                 /* above will set appropriate mti flags */
450                 if (rc <= 0)
451                         /* Nothing wrong, or fatal error */
452                         GOTO(out_norevoke, rc);
453         } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
454                 rc = mgs_check_failover_reg(mti);
455                 if (rc)
456                         GOTO(out_norevoke, rc);
457         }
458
459         OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
460
461         if (mti->mti_flags & LDD_F_WRITECONF) {
462                 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
463                     mti->mti_stripe_index == 0) {
464                         mgs_put_fsdb(mgs, b_fsdb);
465                         b_fsdb = NULL;
466                         rc = mgs_erase_logs(tsi->tsi_env, mgs,
467                                             mti->mti_fsname);
468                         LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
469                                       "request.  All servers must be restarted "
470                                       "in order to regenerate the logs: rc = %d"
471                                       "\n", obd->obd_name, mti->mti_fsname, rc);
472                         if (rc && rc != -ENOENT)
473                                 GOTO(out_norevoke, rc);
474
475                         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs,
476                                                    barrier_name, &b_fsdb);
477                         if (rc) {
478                                 CERROR("Can't get db for %s: %d\n",
479                                        barrier_name, rc);
480
481                                 GOTO(out_norevoke, rc);
482                         }
483
484                         if (!(exp_connect_flags(tsi->tsi_exp) &
485                               OBD_CONNECT_BARRIER)) {
486                                 LCONSOLE_WARN("%s: the MDT %s does not support "
487                                               "write barrier, disable barrier "
488                                               "on the whole system.\n",
489                                               obd->obd_name, mti->mti_svname);
490
491                                 b_fsdb->fsdb_barrier_disabled = 1;
492                         }
493                 } else if (mti->mti_flags &
494                            (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
495                         rc = mgs_erase_log(tsi->tsi_env, mgs, mti->mti_svname);
496                         LCONSOLE_WARN("%s: Regenerating %s log by user "
497                                       "request: rc = %d\n",
498                                       obd->obd_name, mti->mti_svname, rc);
499                         if (rc)
500                                 GOTO(out_norevoke, rc);
501                 }
502
503                 mti->mti_flags |= LDD_F_UPDATE;
504                 /* Erased logs means start from scratch. */
505                 mti->mti_flags &= ~LDD_F_UPGRADE14;
506         }
507
508         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs, mti->mti_fsname, &c_fsdb);
509         if (rc) {
510                 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
511
512                 GOTO(out_norevoke, rc);
513         }
514
515         /*
516          * Log writing contention is handled by the fsdb_mutex.
517          *
518          * It should be alright if someone was reading while we were
519          * updating the logs - if we revoke at the end they will just update
520          * from where they left off.
521          */
522
523         if (mti->mti_flags & LDD_F_UPGRADE14) {
524                 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
525                 GOTO(out, rc);
526         }
527
528         if (mti->mti_flags & LDD_F_UPDATE) {
529                 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
530                        mti->mti_stripe_index);
531
532                 /* create or update the target log
533                    and update the client/mdt logs */
534                 rc = mgs_write_log_target(tsi->tsi_env, mgs, mti, c_fsdb);
535                 if (rc) {
536                         CERROR("Failed to write %s log (%d)\n",
537                                mti->mti_svname, rc);
538                         GOTO(out, rc);
539                 }
540
541                 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
542                                     LDD_F_NEED_INDEX | LDD_F_WRITECONF |
543                                     LDD_F_UPGRADE14);
544                 mti->mti_flags |= LDD_F_REWRITE_LDD;
545         }
546
547 out:
548         mgs_revoke_lock(mgs, c_fsdb, CONFIG_T_CONFIG);
549
550 out_norevoke:
551         if (!rc && mti->mti_flags & LDD_F_SV_TYPE_MDT && b_fsdb) {
552                 if (!c_fsdb) {
553                         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs,
554                                                    mti->mti_fsname, &c_fsdb);
555                         if (rc)
556                                 CERROR("Fail to get db for %s: %d\n",
557                                        mti->mti_fsname, rc);
558                 }
559
560                 if (c_fsdb) {
561                         memcpy(b_fsdb->fsdb_mdt_index_map,
562                                c_fsdb->fsdb_mdt_index_map, INDEX_MAP_SIZE);
563                         b_fsdb->fsdb_mdt_count = c_fsdb->fsdb_mdt_count;
564                 }
565         }
566
567         up_read(&mgs->mgs_barrier_rwsem);
568
569         CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
570                mti->mti_stripe_index, rc);
571          /* An error flag is set in the mti reply rather than an error code */
572         if (rc)
573                 mti->mti_flags |= LDD_F_ERROR;
574
575         /* send back the whole mti in the reply */
576         rep_mti = req_capsule_server_get(tsi->tsi_pill, &RMF_MGS_TARGET_INFO);
577         *rep_mti = *mti;
578
579         /* Flush logs to disk */
580         dt_sync(tsi->tsi_env, mgs->mgs_bottom);
581         if (b_fsdb)
582                 mgs_put_fsdb(mgs, b_fsdb);
583         if (c_fsdb)
584                 mgs_put_fsdb(mgs, c_fsdb);
585         RETURN(rc);
586 }
587
588 /* Called whenever a target cleans up. */
589 static int mgs_target_del(struct tgt_session_info *tsi)
590 {
591         ENTRY;
592
593         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_TARGET_DEL);
594
595         RETURN(0);
596 }
597
598 static int mgs_config_read(struct tgt_session_info *tsi)
599 {
600         struct ptlrpc_request   *req = tgt_ses_req(tsi);
601         struct mgs_config_body  *body;
602         int                      rc;
603
604         ENTRY;
605
606         body = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_CONFIG_BODY);
607         if (body == NULL) {
608                 DEBUG_REQ(D_HA, req, "no mgs_config_body");
609                 RETURN(err_serious(-EFAULT));
610         }
611
612         switch (body->mcb_type) {
613         case CONFIG_T_RECOVER:
614                 rc = mgs_get_ir_logs(req);
615                 break;
616         case CONFIG_T_NODEMAP:
617                 rc = nodemap_get_config_req(req->rq_export->exp_obd, req);
618                 break;
619         case CONFIG_T_CONFIG:
620                 rc = -EOPNOTSUPP;
621                 break;
622         default:
623                 rc = -EINVAL;
624                 break;
625         }
626
627         RETURN(rc);
628 }
629
630 static int mgs_llog_open(struct tgt_session_info *tsi)
631 {
632         struct mgs_thread_info  *mgi;
633         struct ptlrpc_request   *req = tgt_ses_req(tsi);
634         char                    *logname;
635         int                      rc;
636
637         ENTRY;
638
639         rc = tgt_llog_open(tsi);
640         if (rc)
641                 RETURN(rc);
642
643         /*
644          * For old clients there is no direct way of knowing which file system
645          * a client is operating at the MGS side. But we need to pick up those
646          * clients so that the MGS can mark the corresponding file system as
647          * non-IR capable because old clients are not ready to be notified.
648          *
649          * Therefore we attempt to detect the file systems name by hacking the
650          * llog operation which is currently used by the clients to fetch
651          * configuration logs. At present this is fine because this is the
652          * ONLY llog operation between mgc and the MGS.
653          *
654          * If extra llog operation are going to be added, this function needs
655          * further work.
656          *
657          * When releases prior than 2.0 are not supported, the following code
658          * can be removed.
659          */
660         mgi = mgs_env_info(tsi->tsi_env);
661         if (IS_ERR(mgi))
662                 RETURN(PTR_ERR(mgi));
663
664         logname = req_capsule_client_get(tsi->tsi_pill, &RMF_NAME);
665         if (logname) {
666                 char *ptr = strrchr(logname, '-');
667                 int   len = (ptr != NULL) ? (int)(ptr - logname) : 0;
668
669                 if (ptr == NULL || len >= sizeof(mgi->mgi_fsname)) {
670                         if (strcmp(logname, PARAMS_FILENAME) != 0)
671                                 LCONSOLE_WARN("%s: non-config logname "
672                                               "received: %s\n",
673                                               tgt_name(tsi->tsi_tgt),
674                                               logname);
675                         /* not error, this can be llog test name */
676                 } else {
677                         strncpy(mgi->mgi_fsname, logname, len);
678                         mgi->mgi_fsname[len] = 0;
679
680                         rc = mgs_fsc_attach(tsi->tsi_env, tsi->tsi_exp,
681                                             mgi->mgi_fsname);
682                         if (rc && rc != -EEXIST) {
683                                 LCONSOLE_WARN("%s: Unable to add client %s "
684                                               "to file system %s: %d\n",
685                                               tgt_name(tsi->tsi_tgt),
686                                               libcfs_nid2str(req->rq_peer.nid),
687                                               mgi->mgi_fsname, rc);
688                         } else {
689                                 rc = 0;
690                         }
691                 }
692         } else {
693                 CERROR("%s: no logname in request\n", tgt_name(tsi->tsi_tgt));
694                 RETURN(-EINVAL);
695         }
696         RETURN(rc);
697 }
698
699 static inline int mgs_init_export(struct obd_export *exp)
700 {
701         struct mgs_export_data *data = &exp->u.eu_mgs_data;
702
703         /* init mgs_export_data for fsc */
704         spin_lock_init(&data->med_lock);
705         INIT_LIST_HEAD(&data->med_clients);
706
707         spin_lock(&exp->exp_lock);
708         exp->exp_connecting = 1;
709         spin_unlock(&exp->exp_lock);
710
711         /* self-export doesn't need client data and ldlm initialization */
712         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
713                                      &exp->exp_client_uuid)))
714                 return 0;
715         return ldlm_init_export(exp);
716 }
717
718 static inline int mgs_destroy_export(struct obd_export *exp)
719 {
720         ENTRY;
721
722         target_destroy_export(exp);
723         mgs_client_free(exp);
724
725         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
726                                      &exp->exp_client_uuid)))
727                 RETURN(0);
728
729         ldlm_destroy_export(exp);
730
731         RETURN(0);
732 }
733
734 static int mgs_extract_fs_pool(char *arg, char *fsname, char *poolname)
735 {
736         size_t len;
737         char *ptr;
738
739         ENTRY;
740         /* Validate name */
741         for (ptr = arg; *ptr != '\0'; ptr++) {
742                 if (!isalnum(*ptr) && *ptr != '_' && *ptr != '-' && *ptr != '.')
743                         return -EINVAL;
744         }
745
746         /* Test for fsname.poolname format.
747          * strlen() test if poolname is empty
748          */
749         ptr = strchr(arg, '.');
750         if (!ptr || !strlen(ptr))
751                 return -EINVAL;
752         ptr++;
753
754         /* Also make sure poolname is not to long. */
755         if (strlen(ptr) > LOV_MAXPOOLNAME)
756                 return -ENAMETOOLONG;
757         strncpy(poolname, ptr, strlen(ptr));
758
759         /* Test if fsname is empty */
760         len = strlen(arg) - strlen(ptr) - 1;
761         if (!len)
762                 return -EINVAL;
763
764         /* or too long */
765         if (len > MTI_NAME_MAXLEN)
766                 return -ENAMETOOLONG;
767
768         strncpy(fsname, arg, len);
769
770         RETURN(0);
771 }
772
773 static int mgs_iocontrol_nodemap(const struct lu_env *env,
774                                  struct mgs_device *mgs,
775                                  struct obd_ioctl_data *data)
776 {
777         struct lustre_cfg       *lcfg = NULL;
778         struct fs_db            *fsdb;
779         lnet_nid_t              nid;
780         const char              *nodemap_name = NULL;
781         const char              *nidstr = NULL;
782         const char              *client_idstr = NULL;
783         const char              *idtype_str = NULL;
784         char                    *param = NULL;
785         char                    fs_idstr[16];
786         char                    name_buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
787         int                     rc = 0;
788         unsigned long           client_id;
789         __u32                   fs_id;
790         __u32                   cmd;
791         int                     idtype;
792
793         ENTRY;
794
795         if (data->ioc_type != LUSTRE_CFG_TYPE) {
796                 CERROR("%s: unknown cfg record type: %d\n",
797                        mgs->mgs_obd->obd_name, data->ioc_type);
798                 GOTO(out, rc = -EINVAL);
799         }
800
801         if (data->ioc_plen1 > PAGE_SIZE)
802                 GOTO(out, rc = -E2BIG);
803
804         OBD_ALLOC(lcfg, data->ioc_plen1);
805         if (lcfg == NULL)
806                 GOTO(out, rc = -ENOMEM);
807
808         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
809                 GOTO(out_lcfg, rc = -EFAULT);
810
811         cmd = lcfg->lcfg_command;
812
813         switch (cmd) {
814         case LCFG_NODEMAP_ACTIVATE:
815                 if (lcfg->lcfg_bufcount != 2)
816                         GOTO(out_lcfg, rc = -EINVAL);
817                 param = lustre_cfg_string(lcfg, 1);
818                 if (strcmp(param, "1") == 0)
819                                 nodemap_activate(1);
820                 else
821                                 nodemap_activate(0);
822                 break;
823         case LCFG_NODEMAP_ADD:
824         case LCFG_NODEMAP_DEL:
825                 if (lcfg->lcfg_bufcount != 2)
826                         GOTO(out_lcfg, rc = -EINVAL);
827                 nodemap_name = lustre_cfg_string(lcfg, 1);
828                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
829                 break;
830         case LCFG_NODEMAP_TEST_NID:
831                 if (lcfg->lcfg_bufcount != 2)
832                         GOTO(out_lcfg, rc = -EINVAL);
833                 nidstr = lustre_cfg_string(lcfg, 1);
834                 nid = libcfs_str2nid(nidstr);
835                 nodemap_test_nid(nid, name_buf, sizeof(name_buf));
836                 rc = copy_to_user(data->ioc_pbuf1, name_buf,
837                                   MIN(data->ioc_plen1, sizeof(name_buf)));
838                 if (rc != 0)
839                         GOTO(out_lcfg, rc = -EFAULT);
840                 break;
841         case LCFG_NODEMAP_TEST_ID:
842                 if (lcfg->lcfg_bufcount != 4)
843                         GOTO(out_lcfg, rc = -EINVAL);
844                 nidstr = lustre_cfg_string(lcfg, 1);
845                 idtype_str = lustre_cfg_string(lcfg, 2);
846                 client_idstr = lustre_cfg_string(lcfg, 3);
847
848                 nid = libcfs_str2nid(nidstr);
849                 if (strcmp(idtype_str, "uid") == 0)
850                         idtype = NODEMAP_UID;
851                 else
852                         idtype = NODEMAP_GID;
853
854                 rc = kstrtoul(client_idstr, 10, &client_id);
855                 if (rc != 0)
856                         GOTO(out_lcfg, rc = -EINVAL);
857
858                 rc = nodemap_test_id(nid, idtype, client_id, &fs_id);
859                 if (rc < 0)
860                         GOTO(out_lcfg, rc = -EINVAL);
861
862                 if (data->ioc_plen1 < sizeof(fs_idstr))
863                         GOTO(out_lcfg, rc = -EINVAL);
864
865                 snprintf(fs_idstr, sizeof(fs_idstr), "%u", fs_id);
866                 if (copy_to_user(data->ioc_pbuf1, fs_idstr,
867                                  sizeof(fs_idstr)) != 0)
868                         GOTO(out_lcfg, rc = -EINVAL);
869                 break;
870         case LCFG_NODEMAP_ADD_RANGE:
871         case LCFG_NODEMAP_DEL_RANGE:
872         case LCFG_NODEMAP_ADD_UIDMAP:
873         case LCFG_NODEMAP_DEL_UIDMAP:
874         case LCFG_NODEMAP_ADD_GIDMAP:
875         case LCFG_NODEMAP_DEL_GIDMAP:
876         case LCFG_NODEMAP_SET_FILESET:
877                 if (lcfg->lcfg_bufcount != 3)
878                         GOTO(out_lcfg, rc = -EINVAL);
879                 nodemap_name = lustre_cfg_string(lcfg, 1);
880                 param = lustre_cfg_string(lcfg, 2);
881                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
882                 break;
883         case LCFG_NODEMAP_ADMIN:
884         case LCFG_NODEMAP_TRUSTED:
885         case LCFG_NODEMAP_DENY_UNKNOWN:
886         case LCFG_NODEMAP_SQUASH_UID:
887         case LCFG_NODEMAP_SQUASH_GID:
888         case LCFG_NODEMAP_MAP_MODE:
889                 if (lcfg->lcfg_bufcount != 4)
890                         GOTO(out_lcfg, rc = -EINVAL);
891                 nodemap_name = lustre_cfg_string(lcfg, 1);
892                 param = lustre_cfg_string(lcfg, 3);
893                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
894                 break;
895         default:
896                 rc = -ENOTTY;
897         }
898
899         if (rc != 0) {
900                 CERROR("%s: OBD_IOC_NODEMAP command %X for %s: rc = %d\n",
901                        mgs->mgs_obd->obd_name, lcfg->lcfg_command,
902                        nodemap_name, rc);
903                 GOTO(out_lcfg, rc);
904         }
905
906         /* revoke nodemap lock */
907         rc = mgs_find_or_make_fsdb(env, mgs, LUSTRE_NODEMAP_NAME, &fsdb);
908         if (rc < 0) {
909                 CWARN("%s: cannot make nodemap fsdb: rc = %d\n",
910                       mgs->mgs_obd->obd_name, rc);
911         } else {
912                 mgs_revoke_lock(mgs, fsdb, CONFIG_T_NODEMAP);
913                 mgs_put_fsdb(mgs, fsdb);
914         }
915
916 out_lcfg:
917         OBD_FREE(lcfg, data->ioc_plen1);
918 out:
919         RETURN(rc);
920 }
921
922 static int mgs_iocontrol_pool(const struct lu_env *env,
923                               struct mgs_device *mgs,
924                               struct obd_ioctl_data *data)
925 {
926         struct mgs_thread_info *mgi = mgs_env_info(env);
927         int rc;
928         struct lustre_cfg *lcfg = NULL;
929         char *poolname = NULL;
930         ENTRY;
931
932         OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
933         if (poolname == NULL)
934                 RETURN(-ENOMEM);
935
936         if (data->ioc_type != LUSTRE_CFG_TYPE) {
937                 CERROR("%s: unknown cfg record type: %d\n",
938                        mgs->mgs_obd->obd_name, data->ioc_type);
939                 GOTO(out_pool, rc = -EINVAL);
940         }
941
942         if (data->ioc_plen1 > PAGE_SIZE)
943                 GOTO(out_pool, rc = -E2BIG);
944
945         OBD_ALLOC(lcfg, data->ioc_plen1);
946         if (lcfg == NULL)
947                 GOTO(out_pool, rc = -ENOMEM);
948
949         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
950                 GOTO(out_lcfg, rc = -EFAULT);
951
952         if (lcfg->lcfg_bufcount < 2)
953                 GOTO(out_lcfg, rc = -EFAULT);
954
955         /* first arg is always <fsname>.<poolname> */
956         rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
957                                  poolname);
958         if (rc)
959                 GOTO(out_lcfg, rc);
960
961         switch (lcfg->lcfg_command) {
962         case LCFG_POOL_NEW:
963                 if (lcfg->lcfg_bufcount != 2)
964                         GOTO(out_lcfg, rc = -EINVAL);
965                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
966                                   poolname, NULL);
967                 break;
968         case LCFG_POOL_ADD:
969                 if (lcfg->lcfg_bufcount != 3)
970                         GOTO(out_lcfg, rc = -EINVAL);
971                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
972                                   poolname, lustre_cfg_string(lcfg, 2));
973                 break;
974         case LCFG_POOL_REM:
975                 if (lcfg->lcfg_bufcount != 3)
976                         GOTO(out_lcfg, rc = -EINVAL);
977                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
978                                   poolname, lustre_cfg_string(lcfg, 2));
979                 break;
980         case LCFG_POOL_DEL:
981                 if (lcfg->lcfg_bufcount != 2)
982                         GOTO(out_lcfg, rc = -EINVAL);
983                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
984                                   poolname, NULL);
985                 break;
986         default:
987                  rc = -EINVAL;
988         }
989
990         if (rc) {
991                 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
992                        rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
993                 GOTO(out_lcfg, rc);
994         }
995
996 out_lcfg:
997         OBD_FREE(lcfg, data->ioc_plen1);
998 out_pool:
999         OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
1000         RETURN(rc);
1001 }
1002
1003 /* from mdt_iocontrol */
1004 static int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1005                          void *karg, void __user *uarg)
1006 {
1007         struct mgs_device *mgs = exp2mgs_dev(exp);
1008         struct obd_ioctl_data *data = karg;
1009         struct lu_env env;
1010         int rc = 0;
1011
1012         ENTRY;
1013         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1014
1015         rc = lu_env_init(&env, LCT_MG_THREAD);
1016         if (rc)
1017                 RETURN(rc);
1018
1019         switch (cmd) {
1020
1021         case OBD_IOC_PARAM: {
1022                 struct mgs_thread_info *mgi = mgs_env_info(&env);
1023                 struct lustre_cfg *lcfg;
1024
1025                 if (data->ioc_type != LUSTRE_CFG_TYPE) {
1026                         CERROR("%s: unknown cfg record type: %d\n",
1027                                mgs->mgs_obd->obd_name, data->ioc_type);
1028                         GOTO(out, rc = -EINVAL);
1029                 }
1030
1031                 OBD_ALLOC(lcfg, data->ioc_plen1);
1032                 if (lcfg == NULL)
1033                         GOTO(out, rc = -ENOMEM);
1034                 if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
1035                         GOTO(out_free, rc = -EFAULT);
1036
1037                 if (lcfg->lcfg_bufcount < 1)
1038                         GOTO(out_free, rc = -EINVAL);
1039
1040                 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
1041                 if (rc)
1042                         CERROR("%s: setparam err: rc = %d\n",
1043                                exp->exp_obd->obd_name, rc);
1044 out_free:
1045                 OBD_FREE(lcfg, data->ioc_plen1);
1046                 break;
1047         }
1048
1049         case OBD_IOC_REPLACE_NIDS: {
1050                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
1051                         CERROR("No device name specified!\n");
1052                         rc = -EINVAL;
1053                         break;
1054                 }
1055
1056                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
1057                         CERROR("Device name is not NUL terminated!\n");
1058                         rc = -EINVAL;
1059                         break;
1060                 }
1061
1062                 if (data->ioc_plen1 > MTI_NAME_MAXLEN) {
1063                         CERROR("Device name is too long\n");
1064                         rc = -EOVERFLOW;
1065                         break;
1066                 }
1067
1068                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
1069                         CERROR("No NIDs were specified!\n");
1070                         rc = -EINVAL;
1071                         break;
1072                 }
1073
1074                 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
1075                         CERROR("NID list is not NUL terminated!\n");
1076                         rc = -EINVAL;
1077                         break;
1078                 }
1079
1080                 /* replace nids in llog */
1081                 rc = mgs_replace_nids(&env, mgs, data->ioc_inlbuf1,
1082                                       data->ioc_inlbuf2);
1083                 if (rc)
1084                         CERROR("%s: error replacing nids: rc = %d\n",
1085                                exp->exp_obd->obd_name, rc);
1086
1087                 break;
1088         }
1089
1090         case OBD_IOC_POOL:
1091                 rc = mgs_iocontrol_pool(&env, mgs, data);
1092                 break;
1093
1094         case OBD_IOC_BARRIER:
1095                 rc = mgs_iocontrol_barrier(&env, mgs, data);
1096                 break;
1097
1098         case OBD_IOC_NODEMAP:
1099                 rc = mgs_iocontrol_nodemap(&env, mgs, data);
1100                 break;
1101
1102         case OBD_IOC_LCFG_FORK:
1103                 rc = mgs_lcfg_fork(&env, mgs, data->ioc_inlbuf1,
1104                                    data->ioc_inlbuf2);
1105                 break;
1106
1107         case OBD_IOC_LCFG_ERASE:
1108                 rc = mgs_lcfg_erase(&env, mgs, data->ioc_inlbuf1);
1109                 break;
1110
1111         case OBD_IOC_CATLOGLIST:
1112                 rc = mgs_list_logs(&env, mgs, data);
1113                 break;
1114         case OBD_IOC_LLOG_CANCEL:
1115         case OBD_IOC_LLOG_REMOVE:
1116         case OBD_IOC_LLOG_CHECK:
1117         case OBD_IOC_LLOG_INFO:
1118         case OBD_IOC_LLOG_PRINT: {
1119                 struct llog_ctxt *ctxt;
1120
1121                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1122                 rc = llog_ioctl(&env, ctxt, cmd, data);
1123                 llog_ctxt_put(ctxt);
1124                 break;
1125         }
1126
1127         default:
1128                 CERROR("%s: unknown command %#x\n",
1129                        mgs->mgs_obd->obd_name,  cmd);
1130                 rc = -ENOTTY;
1131                 break;
1132         }
1133 out:
1134         lu_env_fini(&env);
1135         RETURN(rc);
1136 }
1137
1138 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
1139 {
1140         struct obd_connect_data *data = NULL;
1141         struct obd_device       *obd;
1142         int                      rc;
1143         ENTRY;
1144
1145         OBD_ALLOC_PTR(data);
1146         if (data == NULL)
1147                 RETURN(-ENOMEM);
1148
1149         obd = class_name2obd(nextdev);
1150         if (obd == NULL) {
1151                 CERROR("can't locate next device: %s\n", nextdev);
1152                 GOTO(out, rc = -ENOTCONN);
1153         }
1154
1155         data->ocd_version = LUSTRE_VERSION_CODE;
1156
1157         rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
1158                          &obd->obd_uuid, data, NULL);
1159         if (rc) {
1160                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1161                 GOTO(out, rc);
1162         }
1163
1164         m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
1165         m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1166         LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1167 out:
1168         OBD_FREE_PTR(data);
1169         RETURN(rc);
1170 }
1171
1172 static struct tgt_handler mgs_mgs_handlers[] = {
1173 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1174                 0,                      MGS_CONNECT,     mgs_connect,
1175                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1176 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1177                 0,                      MGS_DISCONNECT,  mgs_disconnect,
1178                 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
1179 TGT_MGS_HDL_VAR(0,                      MGS_EXCEPTION,   mgs_exception),
1180 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1181 TGT_MGS_HDL    (HABEO_REFERO | MUTABOR, MGS_SET_INFO,    mgs_set_info),
1182 #endif
1183 TGT_MGS_HDL    (HABEO_REFERO | MUTABOR, MGS_TARGET_REG,  mgs_target_reg),
1184 TGT_MGS_HDL_VAR(0,                      MGS_TARGET_DEL,  mgs_target_del),
1185 TGT_MGS_HDL    (HABEO_REFERO,           MGS_CONFIG_READ, mgs_config_read),
1186 };
1187
1188 static struct tgt_handler mgs_obd_handlers[] = {
1189 TGT_OBD_HDL(0,  OBD_PING,       tgt_obd_ping),
1190 };
1191
1192 static struct tgt_handler mgs_dlm_handlers[] = {
1193 [LDLM_ENQUEUE - LDLM_FIRST_OPC] = {
1194         .th_name = "LDLM_ENQUEUE",
1195         /* don't use th_fail_id for MGS to don't interfere with MDS tests.
1196          * * There are no tests for MGS with OBD_FAIL_LDLM_ENQUEUE_NET so it
1197          * * is safe. If such tests will be needed we have to distinguish
1198          * * MDS and MGS fail ids, e.g use OBD_FAIL_MGS_ENQUEUE_NET for MGS
1199          * * instead of common OBD_FAIL_LDLM_ENQUEUE_NET */
1200         .th_fail_id = 0,
1201         .th_opc = LDLM_ENQUEUE,
1202         .th_flags = HABEO_CLAVIS,
1203         .th_act = tgt_enqueue,
1204         .th_fmt = &RQF_LDLM_ENQUEUE,
1205         .th_version = LUSTRE_DLM_VERSION,
1206         },
1207 };
1208
1209 static struct tgt_handler mgs_llog_handlers[] = {
1210 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_CREATE,      mgs_llog_open),
1211 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_NEXT_BLOCK,  tgt_llog_next_block),
1212 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
1213 TGT_LLOG_HDL_VAR(0,     LLOG_ORIGIN_HANDLE_CLOSE,       tgt_llog_close),
1214 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_PREV_BLOCK,  tgt_llog_prev_block),
1215 };
1216
1217 static struct tgt_opc_slice mgs_common_slice[] = {
1218         {
1219                 .tos_opc_start = MGS_FIRST_OPC,
1220                 .tos_opc_end   = MGS_LAST_OPC,
1221                 .tos_hs        = mgs_mgs_handlers
1222         },
1223         {
1224                 .tos_opc_start = OBD_FIRST_OPC,
1225                 .tos_opc_end   = OBD_LAST_OPC,
1226                 .tos_hs        = mgs_obd_handlers
1227         },
1228         {
1229                 .tos_opc_start = LDLM_FIRST_OPC,
1230                 .tos_opc_end   = LDLM_LAST_OPC,
1231                 .tos_hs        = mgs_dlm_handlers
1232         },
1233         {
1234                 .tos_opc_start = LLOG_FIRST_OPC,
1235                 .tos_opc_end   = LLOG_LAST_OPC,
1236                 .tos_hs        = mgs_llog_handlers
1237         },
1238         {
1239                 .tos_opc_start = SEC_FIRST_OPC,
1240                 .tos_opc_end   = SEC_LAST_OPC,
1241                 .tos_hs        = tgt_sec_ctx_handlers
1242         },
1243         {
1244                 .tos_hs        = NULL
1245         }
1246 };
1247
1248 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1249                      struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1250 {
1251         struct ptlrpc_service_conf       conf;
1252         struct obd_device               *obd;
1253         struct lustre_mount_info        *lmi;
1254         struct llog_ctxt                *ctxt;
1255         int                              rc;
1256
1257         ENTRY;
1258
1259         lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1260         if (lmi == NULL)
1261                 RETURN(-ENODEV);
1262
1263         mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1264
1265         rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1266         if (rc)
1267                 GOTO(err_lmi, rc);
1268
1269         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1270         LASSERT(obd);
1271         mgs->mgs_obd = obd;
1272         mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1273
1274         obd->u.obt.obt_magic = OBT_MAGIC;
1275         obd->u.obt.obt_instance = 0;
1276
1277         /* namespace for mgs llog */
1278         obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1279                                                 LDLM_NAMESPACE_SERVER,
1280                                                 LDLM_NAMESPACE_MODEST,
1281                                                 LDLM_NS_TYPE_MGT);
1282         if (obd->obd_namespace == NULL)
1283                 GOTO(err_ops, rc = -ENOMEM);
1284
1285         /* No recovery for MGCs */
1286         obd->obd_replayable = 0;
1287
1288         rc = tgt_init(env, &mgs->mgs_lut, obd, mgs->mgs_bottom,
1289                       mgs_common_slice, OBD_FAIL_MGS_ALL_REQUEST_NET,
1290                       OBD_FAIL_MGS_ALL_REPLY_NET);
1291         if (rc)
1292                 GOTO(err_ns, rc);
1293
1294         rc = mgs_fs_setup(env, mgs);
1295         if (rc) {
1296                 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1297                        obd->obd_name, rc);
1298                 GOTO(err_tgt, rc);
1299         }
1300
1301         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1302                         obd, &llog_osd_ops);
1303         if (rc)
1304                 GOTO(err_fs, rc);
1305
1306         /* XXX: we need this trick till N:1 stack is supported
1307          * set "current" directory for named llogs */
1308         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1309         LASSERT(ctxt);
1310         ctxt->loc_dir = mgs->mgs_configs_dir;
1311         llog_ctxt_put(ctxt);
1312
1313         /* Internal mgs setup */
1314         mgs_init_fsdb_list(mgs);
1315         mutex_init(&mgs->mgs_mutex);
1316         mgs->mgs_start_time = ktime_get_real_seconds();
1317         spin_lock_init(&mgs->mgs_lock);
1318         mutex_init(&mgs->mgs_health_mutex);
1319         init_rwsem(&mgs->mgs_barrier_rwsem);
1320
1321         rc = mgs_lcfg_rename(env, mgs);
1322         if (rc)
1323                 GOTO(err_llog, rc);
1324
1325         rc = lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1326         if (rc != 0) {
1327                 CERROR("%s: cannot initialize proc entry: rc = %d\n",
1328                        obd->obd_name, rc);
1329                 GOTO(err_llog, rc);
1330         }
1331
1332         /* Setup params fsdb and log, so that other servers can make a local
1333          * copy successfully when they are mounted. See LU-4783 */
1334         rc = mgs_params_fsdb_setup(env, mgs);
1335         if (rc)
1336                 /* params fsdb and log can be setup later */
1337                 CERROR("%s: %s fsdb and log setup failed: rc = %d\n",
1338                        obd->obd_name, PARAMS_FILENAME, rc);
1339
1340         /* Setup _mgs fsdb, useful for srpc */
1341         mgs__mgs_fsdb_setup(env, mgs);
1342
1343         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1344                            "mgs_ldlm_client", &obd->obd_ldlm_client);
1345
1346         conf = (typeof(conf)) {
1347                 .psc_name               = LUSTRE_MGS_NAME,
1348                 .psc_watchdog_factor    = MGS_SERVICE_WATCHDOG_FACTOR,
1349                 .psc_buf                = {
1350                         .bc_nbufs               = MGS_NBUFS,
1351                         .bc_buf_size            = MGS_BUFSIZE,
1352                         .bc_req_max_size        = MGS_MAXREQSIZE,
1353                         .bc_rep_max_size        = MGS_MAXREPSIZE,
1354                         .bc_req_portal          = MGS_REQUEST_PORTAL,
1355                         .bc_rep_portal          = MGC_REPLY_PORTAL,
1356                 },
1357                 .psc_thr                = {
1358                         .tc_thr_name            = "ll_mgs",
1359                         .tc_nthrs_init          = MGS_NTHRS_INIT,
1360                         .tc_nthrs_max           = MGS_NTHRS_MAX,
1361                         .tc_ctx_tags            = LCT_MG_THREAD,
1362                 },
1363                 .psc_ops                = {
1364                         .so_req_handler         = tgt_request_handle,
1365                         .so_req_printer         = target_print_req,
1366                 },
1367         };
1368
1369         /* Start the service threads */
1370         mgs->mgs_service = ptlrpc_register_service(&conf, mgs->mgs_kset,
1371                                                    obd->obd_proc_entry);
1372         if (IS_ERR(mgs->mgs_service)) {
1373                 rc = PTR_ERR(mgs->mgs_service);
1374                 CERROR("failed to start mgs service: %d\n", rc);
1375                 mgs->mgs_service = NULL;
1376                 GOTO(err_lproc, rc);
1377         }
1378
1379         ping_evictor_start();
1380
1381         CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1382
1383         /* device stack is not yet fully setup to keep no objects behind */
1384         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1385         RETURN(0);
1386 err_lproc:
1387         mgs_params_fsdb_cleanup(env, mgs);
1388         lproc_mgs_cleanup(mgs);
1389 err_llog:
1390         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1391         if (ctxt) {
1392                 ctxt->loc_dir = NULL;
1393                 llog_cleanup(env, ctxt);
1394         }
1395 err_tgt:
1396         tgt_fini(env, &mgs->mgs_lut);
1397 err_fs:
1398         /* No extra cleanup needed for llog_init_commit_thread() */
1399         mgs_fs_cleanup(env, mgs);
1400 err_ns:
1401         ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1402         obd->obd_namespace = NULL;
1403 err_ops:
1404         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1405         if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1406                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1407                 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1408                                 lu_cdebug_printer);
1409         }
1410         obd_disconnect(mgs->mgs_bottom_exp);
1411 err_lmi:
1412         if (lmi)
1413                 server_put_mount(lustre_cfg_string(lcfg, 0), true);
1414         RETURN(rc);
1415 }
1416
1417 static struct lu_device *mgs_device_free(const struct lu_env *env,
1418                                          struct lu_device *lu)
1419 {
1420         struct mgs_device *mgs = lu2mgs_dev(lu);
1421         ENTRY;
1422
1423         dt_device_fini(&mgs->mgs_dt_dev);
1424         OBD_FREE_PTR(mgs);
1425         RETURN(NULL);
1426 }
1427
1428 static int mgs_process_config(const struct lu_env *env,
1429                               struct lu_device *dev,
1430                               struct lustre_cfg *lcfg)
1431 {
1432         LBUG();
1433         return 0;
1434 }
1435
1436 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1437                            const struct lu_object_conf *unused)
1438 {
1439         struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1440         struct lu_device  *under;
1441         struct lu_object  *below;
1442         int                rc = 0;
1443         ENTRY;
1444
1445         /* do no set .do_ops as mgs calls to bottom osd directly */
1446
1447         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1448                         PFID(lu_object_fid(o)));
1449
1450         under = &d->mgs_bottom->dd_lu_dev;
1451         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1452         if (below != NULL)
1453                 lu_object_add(o, below);
1454         else
1455                 rc = -ENOMEM;
1456
1457         return rc;
1458 }
1459
1460 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1461 {
1462         struct mgs_object *obj = lu2mgs_obj(o);
1463         struct lu_object_header *h = o->lo_header;
1464
1465         dt_object_fini(&obj->mgo_obj);
1466         lu_object_header_fini(h);
1467         OBD_FREE_PTR(obj);
1468 }
1469
1470 static int mgs_object_print(const struct lu_env *env, void *cookie,
1471                             lu_printer_t p, const struct lu_object *l)
1472 {
1473         const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1474
1475         return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1476 }
1477
1478 static struct lu_object_operations mgs_lu_obj_ops = {
1479         .loo_object_init        = mgs_object_init,
1480         .loo_object_free        = mgs_object_free,
1481         .loo_object_print       = mgs_object_print,
1482 };
1483
1484 static struct lu_object *mgs_object_alloc(const struct lu_env *env,
1485                                           const struct lu_object_header *hdr,
1486                                           struct lu_device *d)
1487 {
1488         struct lu_object_header *h;
1489         struct mgs_object       *o;
1490         struct lu_object        *l;
1491
1492         LASSERT(hdr == NULL);
1493
1494         OBD_ALLOC_PTR(o);
1495         if (o != NULL) {
1496                 l = &o->mgo_obj.do_lu;
1497                 h = &o->mgo_header;
1498
1499                 lu_object_header_init(h);
1500                 dt_object_init(&o->mgo_obj, h, d);
1501                 lu_object_add_top(h, l);
1502
1503                 l->lo_ops = &mgs_lu_obj_ops;
1504
1505                 return l;
1506         } else {
1507                 return NULL;
1508         }
1509 }
1510
1511 const struct lu_device_operations mgs_lu_ops = {
1512         .ldo_object_alloc       = mgs_object_alloc,
1513         .ldo_process_config     = mgs_process_config,
1514 };
1515
1516 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1517                                           struct lu_device_type *type,
1518                                           struct lustre_cfg *lcfg)
1519 {
1520         struct mgs_device *mgs;
1521         struct lu_device  *ludev;
1522
1523         OBD_ALLOC_PTR(mgs);
1524         if (mgs == NULL) {
1525                 ludev = ERR_PTR(-ENOMEM);
1526         } else {
1527                 int rc;
1528
1529                 ludev = mgs2lu_dev(mgs);
1530                 dt_device_init(&mgs->mgs_dt_dev, type);
1531                 rc = mgs_init0(env, mgs, type, lcfg);
1532                 if (rc != 0) {
1533                         mgs_device_free(env, ludev);
1534                         ludev = ERR_PTR(rc);
1535                 }
1536         }
1537         return ludev;
1538 }
1539
1540 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1541                                          struct lu_device *d)
1542 {
1543         struct mgs_device       *mgs = lu2mgs_dev(d);
1544         struct obd_device       *obd = mgs->mgs_obd;
1545         struct llog_ctxt        *ctxt;
1546
1547         ENTRY;
1548
1549         LASSERT(mgs->mgs_bottom);
1550
1551         class_disconnect_exports(obd);
1552
1553         ping_evictor_stop();
1554
1555         mutex_lock(&mgs->mgs_health_mutex);
1556         ptlrpc_unregister_service(mgs->mgs_service);
1557         mutex_unlock(&mgs->mgs_health_mutex);
1558
1559         mgs_params_fsdb_cleanup(env, mgs);
1560         mgs_cleanup_fsdb_list(mgs);
1561
1562         ldlm_namespace_free_prior(obd->obd_namespace, NULL, 1);
1563         obd_exports_barrier(obd);
1564         obd_zombie_barrier();
1565
1566         tgt_fini(env, &mgs->mgs_lut);
1567         lproc_mgs_cleanup(mgs);
1568
1569         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1570         if (ctxt) {
1571                 ctxt->loc_dir = NULL;
1572                 llog_cleanup(env, ctxt);
1573         }
1574
1575         mgs_fs_cleanup(env, mgs);
1576
1577         ldlm_namespace_free_post(obd->obd_namespace);
1578         obd->obd_namespace = NULL;
1579
1580         lu_site_purge(env, d->ld_site, ~0);
1581         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1582                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1583                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1584         }
1585
1586         LASSERT(mgs->mgs_bottom_exp);
1587         obd_disconnect(mgs->mgs_bottom_exp);
1588
1589         server_put_mount(obd->obd_name, true);
1590
1591         RETURN(NULL);
1592 }
1593
1594 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1595 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1596
1597 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1598
1599 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1600
1601 static struct lu_device_type_operations mgs_device_type_ops = {
1602         .ldto_init              = mgs_type_init,
1603         .ldto_fini              = mgs_type_fini,
1604
1605         .ldto_start             = mgs_type_start,
1606         .ldto_stop              = mgs_type_stop,
1607
1608         .ldto_device_alloc      = mgs_device_alloc,
1609         .ldto_device_free       = mgs_device_free,
1610
1611         .ldto_device_fini       = mgs_device_fini
1612 };
1613
1614 static struct lu_device_type mgs_device_type = {
1615         .ldt_tags       = LU_DEVICE_DT,
1616         .ldt_name       = LUSTRE_MGS_NAME,
1617         .ldt_ops        = &mgs_device_type_ops,
1618         .ldt_ctx_tags   = LCT_MG_THREAD
1619 };
1620
1621 static int mgs_obd_connect(const struct lu_env *env, struct obd_export **exp,
1622                            struct obd_device *obd, struct obd_uuid *cluuid,
1623                            struct obd_connect_data *data, void *localdata)
1624 {
1625         struct obd_export       *lexp;
1626         struct lustre_handle     conn = {
1627                 .cookie = 0,
1628         };
1629         int                      rc;
1630
1631         ENTRY;
1632
1633         if (exp == NULL || obd == NULL || cluuid == NULL)
1634                 RETURN(-EINVAL);
1635
1636         rc = class_connect(&conn, obd, cluuid);
1637         if (rc)
1638                 RETURN(rc);
1639
1640         lexp = class_conn2export(&conn);
1641         if (lexp == NULL)
1642                 RETURN(-EFAULT);
1643
1644         if (data != NULL) {
1645                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
1646
1647                 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
1648                         data->ocd_connect_flags2 &= MGS_CONNECT_SUPPORTED2;
1649
1650                 data->ocd_version = LUSTRE_VERSION_CODE;
1651                 lexp->exp_connect_data = *data;
1652         }
1653
1654         tgt_counter_incr(lexp, LPROC_MGS_CONNECT);
1655
1656         rc = mgs_export_stats_init(obd, lexp, localdata);
1657         if (rc)
1658                 class_disconnect(lexp);
1659         else
1660                 *exp = lexp;
1661
1662         RETURN(rc);
1663 }
1664
1665 static int mgs_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
1666                              struct obd_device *obd, struct obd_uuid *cluuid,
1667                              struct obd_connect_data *data, void *localdata)
1668 {
1669         ENTRY;
1670
1671         if (exp == NULL || obd == NULL || cluuid == NULL)
1672                 RETURN(-EINVAL);
1673
1674         tgt_counter_incr(exp, LPROC_MGS_CONNECT);
1675
1676         if (data != NULL) {
1677                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
1678
1679                 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
1680                         data->ocd_connect_flags2 &= MGS_CONNECT_SUPPORTED2;
1681
1682                 data->ocd_version = LUSTRE_VERSION_CODE;
1683                 exp->exp_connect_data = *data;
1684         }
1685
1686         RETURN(mgs_export_stats_init(obd, exp, localdata));
1687 }
1688
1689 static int mgs_obd_disconnect(struct obd_export *exp)
1690 {
1691         int rc;
1692
1693         ENTRY;
1694
1695         LASSERT(exp);
1696
1697         mgs_fsc_cleanup(exp);
1698
1699         class_export_get(exp);
1700         tgt_counter_incr(exp, LPROC_MGS_DISCONNECT);
1701
1702         rc = server_disconnect_export(exp);
1703         class_export_put(exp);
1704         RETURN(rc);
1705 }
1706
1707 static int mgs_health_check(const struct lu_env *env, struct obd_device *obd)
1708 {
1709         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
1710         int rc = 0;
1711
1712         mutex_lock(&mgs->mgs_health_mutex);
1713         rc |= ptlrpc_service_health_check(mgs->mgs_service);
1714         mutex_unlock(&mgs->mgs_health_mutex);
1715
1716         return rc != 0 ? 1 : 0;
1717 }
1718
1719 /* use obd ops to offer management infrastructure */
1720 static struct obd_ops mgs_obd_device_ops = {
1721         .o_owner                = THIS_MODULE,
1722         .o_connect              = mgs_obd_connect,
1723         .o_reconnect            = mgs_obd_reconnect,
1724         .o_disconnect           = mgs_obd_disconnect,
1725         .o_init_export          = mgs_init_export,
1726         .o_destroy_export       = mgs_destroy_export,
1727         .o_iocontrol            = mgs_iocontrol,
1728         .o_health_check         = mgs_health_check,
1729 };
1730
1731 static int __init mgs_init(void)
1732 {
1733         return class_register_type(&mgs_obd_device_ops, NULL, true, NULL,
1734                                    LUSTRE_MGS_NAME, &mgs_device_type);
1735 }
1736
1737 static void __exit mgs_exit(void)
1738 {
1739         class_unregister_type(LUSTRE_MGS_NAME);
1740 }
1741
1742 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1743 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1744 MODULE_VERSION(LUSTRE_VERSION_STRING);
1745 MODULE_LICENSE("GPL");
1746
1747 module_init(mgs_init);
1748 module_exit(mgs_exit);