b32382088e96cb654f8adf41f554126ac5e14360
[deliverable/linux.git] / drivers / staging / lustre / lustre / mgc / mgc_request.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/mgc/mgc_request.c
37 *
38 * Author: Nathan Rutman <nathan@clusterfs.com>
39 */
40
41 #define DEBUG_SUBSYSTEM S_MGC
42 #define D_MGC D_CONFIG /*|D_WARNING*/
43
44 #include <linux/module.h>
45 #include <obd_class.h>
46 #include <lustre_dlm.h>
47 #include <lprocfs_status.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <dt_object.h>
51
52 #include "mgc_internal.h"
53
54 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
55 int type)
56 {
57 __u64 resname = 0;
58
59 if (len > 8) {
60 CERROR("name too long: %s\n", name);
61 return -EINVAL;
62 }
63 if (len <= 0) {
64 CERROR("missing name: %s\n", name);
65 return -EINVAL;
66 }
67 memcpy(&resname, name, len);
68
69 /* Always use the same endianness for the resid */
70 memset(res_id, 0, sizeof(*res_id));
71 res_id->name[0] = cpu_to_le64(resname);
72 /* XXX: unfortunately, sptlprc and config llog share one lock */
73 switch(type) {
74 case CONFIG_T_CONFIG:
75 case CONFIG_T_SPTLRPC:
76 resname = 0;
77 break;
78 case CONFIG_T_RECOVER:
79 resname = type;
80 break;
81 default:
82 LBUG();
83 }
84 res_id->name[1] = cpu_to_le64(resname);
85 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
86 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
87 return 0;
88 }
89
90 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
91 {
92 /* fsname is at most 8 chars long, maybe contain "-".
93 * e.g. "lustre", "SUN-000" */
94 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
95 }
96 EXPORT_SYMBOL(mgc_fsname2resid);
97
98 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
99 {
100 char *name_end;
101 int len;
102
103 /* logname consists of "fsname-nodetype".
104 * e.g. "lustre-MDT0001", "SUN-000-client" */
105 name_end = strrchr(logname, '-');
106 LASSERT(name_end);
107 len = name_end - logname;
108 return mgc_name2resid(logname, len, res_id, type);
109 }
110
111 /********************** config llog list **********************/
112 static LIST_HEAD(config_llog_list);
113 static DEFINE_SPINLOCK(config_list_lock);
114
115 /* Take a reference to a config log */
116 static int config_log_get(struct config_llog_data *cld)
117 {
118 atomic_inc(&cld->cld_refcount);
119 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
120 atomic_read(&cld->cld_refcount));
121 return 0;
122 }
123
124 /* Drop a reference to a config log. When no longer referenced,
125 we can free the config log data */
126 static void config_log_put(struct config_llog_data *cld)
127 {
128 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
129 atomic_read(&cld->cld_refcount));
130 LASSERT(atomic_read(&cld->cld_refcount) > 0);
131
132 /* spinlock to make sure no item with 0 refcount in the list */
133 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
134 list_del(&cld->cld_list_chain);
135 spin_unlock(&config_list_lock);
136
137 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
138
139 if (cld->cld_recover)
140 config_log_put(cld->cld_recover);
141 if (cld->cld_sptlrpc)
142 config_log_put(cld->cld_sptlrpc);
143 if (cld_is_sptlrpc(cld))
144 sptlrpc_conf_log_stop(cld->cld_logname);
145
146 class_export_put(cld->cld_mgcexp);
147 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
148 }
149 }
150
151 /* Find a config log by name */
152 static
153 struct config_llog_data *config_log_find(char *logname,
154 struct config_llog_instance *cfg)
155 {
156 struct config_llog_data *cld;
157 struct config_llog_data *found = NULL;
158 void * instance;
159
160 LASSERT(logname != NULL);
161
162 instance = cfg ? cfg->cfg_instance : NULL;
163 spin_lock(&config_list_lock);
164 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
165 /* check if instance equals */
166 if (instance != cld->cld_cfg.cfg_instance)
167 continue;
168
169 /* instance may be NULL, should check name */
170 if (strcmp(logname, cld->cld_logname) == 0) {
171 found = cld;
172 break;
173 }
174 }
175 if (found) {
176 atomic_inc(&found->cld_refcount);
177 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
178 }
179 spin_unlock(&config_list_lock);
180 return found;
181 }
182
183 static
184 struct config_llog_data *do_config_log_add(struct obd_device *obd,
185 char *logname,
186 int type,
187 struct config_llog_instance *cfg,
188 struct super_block *sb)
189 {
190 struct config_llog_data *cld;
191 int rc;
192
193 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
194 cfg ? cfg->cfg_instance : 0);
195
196 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
197 if (!cld)
198 return ERR_PTR(-ENOMEM);
199
200 strcpy(cld->cld_logname, logname);
201 if (cfg)
202 cld->cld_cfg = *cfg;
203 else
204 cld->cld_cfg.cfg_callback = class_config_llog_handler;
205 mutex_init(&cld->cld_lock);
206 cld->cld_cfg.cfg_last_idx = 0;
207 cld->cld_cfg.cfg_flags = 0;
208 cld->cld_cfg.cfg_sb = sb;
209 cld->cld_type = type;
210 atomic_set(&cld->cld_refcount, 1);
211
212 /* Keep the mgc around until we are done */
213 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
214
215 if (cld_is_sptlrpc(cld)) {
216 sptlrpc_conf_log_start(logname);
217 cld->cld_cfg.cfg_obdname = obd->obd_name;
218 }
219
220 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
221
222 spin_lock(&config_list_lock);
223 list_add(&cld->cld_list_chain, &config_llog_list);
224 spin_unlock(&config_list_lock);
225
226 if (rc) {
227 config_log_put(cld);
228 return ERR_PTR(rc);
229 }
230
231 if (cld_is_sptlrpc(cld)) {
232 rc = mgc_process_log(obd, cld);
233 if (rc && rc != -ENOENT)
234 CERROR("failed processing sptlrpc log: %d\n", rc);
235 }
236
237 return cld;
238 }
239
240 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
241 char *fsname,
242 struct config_llog_instance *cfg,
243 struct super_block *sb)
244 {
245 struct config_llog_instance lcfg = *cfg;
246 struct lustre_sb_info *lsi = s2lsi(sb);
247 struct config_llog_data *cld;
248 char logname[32];
249
250 if (IS_OST(lsi))
251 return NULL;
252
253 /* for osp-on-ost, see lustre_start_osp() */
254 if (IS_MDT(lsi) && lcfg.cfg_instance)
255 return NULL;
256
257 /* we have to use different llog for clients and mdts for cmd
258 * where only clients are notified if one of cmd server restarts */
259 LASSERT(strlen(fsname) < sizeof(logname) / 2);
260 strcpy(logname, fsname);
261 if (IS_SERVER(lsi)) { /* mdt */
262 LASSERT(lcfg.cfg_instance == NULL);
263 lcfg.cfg_instance = sb;
264 strcat(logname, "-mdtir");
265 } else {
266 LASSERT(lcfg.cfg_instance != NULL);
267 strcat(logname, "-cliir");
268 }
269
270 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
271 return cld;
272 }
273
274
275 /** Add this log to the list of active logs watched by an MGC.
276 * Active means we're watching for updates.
277 * We have one active log per "mount" - client instance or servername.
278 * Each instance may be at a different point in the log.
279 */
280 static int config_log_add(struct obd_device *obd, char *logname,
281 struct config_llog_instance *cfg,
282 struct super_block *sb)
283 {
284 struct lustre_sb_info *lsi = s2lsi(sb);
285 struct config_llog_data *cld;
286 struct config_llog_data *sptlrpc_cld;
287 char seclogname[32];
288 char *ptr;
289
290 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
291
292 /*
293 * for each regular log, the depended sptlrpc log name is
294 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
295 */
296 ptr = strrchr(logname, '-');
297 if (ptr == NULL || ptr - logname > 8) {
298 CERROR("logname %s is too long\n", logname);
299 return -EINVAL;
300 }
301
302 memcpy(seclogname, logname, ptr - logname);
303 strcpy(seclogname + (ptr - logname), "-sptlrpc");
304
305 sptlrpc_cld = config_log_find(seclogname, NULL);
306 if (sptlrpc_cld == NULL) {
307 sptlrpc_cld = do_config_log_add(obd, seclogname,
308 CONFIG_T_SPTLRPC, NULL, NULL);
309 if (IS_ERR(sptlrpc_cld)) {
310 CERROR("can't create sptlrpc log: %s\n", seclogname);
311 return PTR_ERR(sptlrpc_cld);
312 }
313 }
314
315 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
316 if (IS_ERR(cld)) {
317 CERROR("can't create log: %s\n", logname);
318 config_log_put(sptlrpc_cld);
319 return PTR_ERR(cld);
320 }
321
322 cld->cld_sptlrpc = sptlrpc_cld;
323
324 LASSERT(lsi->lsi_lmd);
325 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
326 struct config_llog_data *recover_cld;
327 *strrchr(seclogname, '-') = 0;
328 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
329 if (IS_ERR(recover_cld)) {
330 config_log_put(cld);
331 return PTR_ERR(recover_cld);
332 }
333 cld->cld_recover = recover_cld;
334 }
335
336 return 0;
337 }
338
339 DEFINE_MUTEX(llog_process_lock);
340
341 /** Stop watching for updates on this log.
342 */
343 static int config_log_end(char *logname, struct config_llog_instance *cfg)
344 {
345 struct config_llog_data *cld;
346 struct config_llog_data *cld_sptlrpc = NULL;
347 struct config_llog_data *cld_recover = NULL;
348 int rc = 0;
349
350 cld = config_log_find(logname, cfg);
351 if (cld == NULL)
352 return -ENOENT;
353
354 mutex_lock(&cld->cld_lock);
355 /*
356 * if cld_stopping is set, it means we didn't start the log thus
357 * not owning the start ref. this can happen after previous umount:
358 * the cld still hanging there waiting for lock cancel, and we
359 * remount again but failed in the middle and call log_end without
360 * calling start_log.
361 */
362 if (unlikely(cld->cld_stopping)) {
363 mutex_unlock(&cld->cld_lock);
364 /* drop the ref from the find */
365 config_log_put(cld);
366 return rc;
367 }
368
369 cld->cld_stopping = 1;
370
371 cld_recover = cld->cld_recover;
372 cld->cld_recover = NULL;
373 mutex_unlock(&cld->cld_lock);
374
375 if (cld_recover) {
376 mutex_lock(&cld_recover->cld_lock);
377 cld_recover->cld_stopping = 1;
378 mutex_unlock(&cld_recover->cld_lock);
379 config_log_put(cld_recover);
380 }
381
382 spin_lock(&config_list_lock);
383 cld_sptlrpc = cld->cld_sptlrpc;
384 cld->cld_sptlrpc = NULL;
385 spin_unlock(&config_list_lock);
386
387 if (cld_sptlrpc)
388 config_log_put(cld_sptlrpc);
389
390 /* drop the ref from the find */
391 config_log_put(cld);
392 /* drop the start ref */
393 config_log_put(cld);
394
395 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
396 rc);
397 return rc;
398 }
399
400 #ifdef LPROCFS
401 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
402 {
403 struct obd_device *obd = data;
404 struct obd_import *imp = obd->u.cli.cl_import;
405 struct obd_connect_data *ocd = &imp->imp_connect_data;
406 struct config_llog_data *cld;
407
408 seq_printf(m, "imperative_recovery: %s\n",
409 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
410 seq_printf(m, "client_state:\n");
411
412 spin_lock(&config_list_lock);
413 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
414 if (cld->cld_recover == NULL)
415 continue;
416 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
417 cld->cld_logname,
418 cld->cld_recover->cld_cfg.cfg_last_idx);
419 }
420 spin_unlock(&config_list_lock);
421
422 return 0;
423 }
424 #endif
425
426 /* reenqueue any lost locks */
427 #define RQ_RUNNING 0x1
428 #define RQ_NOW 0x2
429 #define RQ_LATER 0x4
430 #define RQ_STOP 0x8
431 static int rq_state = 0;
432 static wait_queue_head_t rq_waitq;
433 static DECLARE_COMPLETION(rq_exit);
434
435 static void do_requeue(struct config_llog_data *cld)
436 {
437 LASSERT(atomic_read(&cld->cld_refcount) > 0);
438
439 /* Do not run mgc_process_log on a disconnected export or an
440 export which is being disconnected. Take the client
441 semaphore to make the check non-racy. */
442 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
443 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
444 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
445 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
446 } else {
447 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
448 cld->cld_logname);
449 }
450 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
451 }
452
453 /* this timeout represents how many seconds MGC should wait before
454 * requeue config and recover lock to the MGS. We need to randomize this
455 * in order to not flood the MGS.
456 */
457 #define MGC_TIMEOUT_MIN_SECONDS 5
458 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
459
460 static int mgc_requeue_thread(void *data)
461 {
462 int rc = 0;
463
464 CDEBUG(D_MGC, "Starting requeue thread\n");
465
466 /* Keep trying failed locks periodically */
467 spin_lock(&config_list_lock);
468 rq_state |= RQ_RUNNING;
469 while (1) {
470 struct l_wait_info lwi;
471 struct config_llog_data *cld, *cld_prev;
472 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
473 int stopped = !!(rq_state & RQ_STOP);
474 int to;
475
476 /* Any new or requeued lostlocks will change the state */
477 rq_state &= ~(RQ_NOW | RQ_LATER);
478 spin_unlock(&config_list_lock);
479
480 /* Always wait a few seconds to allow the server who
481 caused the lock revocation to finish its setup, plus some
482 random so everyone doesn't try to reconnect at once. */
483 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
484 to += rand * HZ / 100; /* rand is centi-seconds */
485 lwi = LWI_TIMEOUT(to, NULL, NULL);
486 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
487
488 /*
489 * iterate & processing through the list. for each cld, process
490 * its depending sptlrpc cld firstly (if any) and then itself.
491 *
492 * it's guaranteed any item in the list must have
493 * reference > 0; and if cld_lostlock is set, at
494 * least one reference is taken by the previous enqueue.
495 */
496 cld_prev = NULL;
497
498 spin_lock(&config_list_lock);
499 list_for_each_entry(cld, &config_llog_list,
500 cld_list_chain) {
501 if (!cld->cld_lostlock)
502 continue;
503
504 spin_unlock(&config_list_lock);
505
506 LASSERT(atomic_read(&cld->cld_refcount) > 0);
507
508 /* Whether we enqueued again or not in mgc_process_log,
509 * we're done with the ref from the old enqueue */
510 if (cld_prev)
511 config_log_put(cld_prev);
512 cld_prev = cld;
513
514 cld->cld_lostlock = 0;
515 if (likely(!stopped))
516 do_requeue(cld);
517
518 spin_lock(&config_list_lock);
519 }
520 spin_unlock(&config_list_lock);
521 if (cld_prev)
522 config_log_put(cld_prev);
523
524 /* break after scanning the list so that we can drop
525 * refcount to losing lock clds */
526 if (unlikely(stopped)) {
527 spin_lock(&config_list_lock);
528 break;
529 }
530
531 /* Wait a bit to see if anyone else needs a requeue */
532 lwi = (struct l_wait_info) { 0 };
533 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
534 &lwi);
535 spin_lock(&config_list_lock);
536 }
537 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
538 rq_state &= ~RQ_RUNNING;
539 spin_unlock(&config_list_lock);
540
541 complete(&rq_exit);
542
543 CDEBUG(D_MGC, "Ending requeue thread\n");
544 return rc;
545 }
546
547 /* Add a cld to the list to requeue. Start the requeue thread if needed.
548 We are responsible for dropping the config log reference from here on out. */
549 static void mgc_requeue_add(struct config_llog_data *cld)
550 {
551 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
552 cld->cld_logname, atomic_read(&cld->cld_refcount),
553 cld->cld_stopping, rq_state);
554 LASSERT(atomic_read(&cld->cld_refcount) > 0);
555
556 mutex_lock(&cld->cld_lock);
557 if (cld->cld_stopping || cld->cld_lostlock) {
558 mutex_unlock(&cld->cld_lock);
559 return;
560 }
561 /* this refcount will be released in mgc_requeue_thread. */
562 config_log_get(cld);
563 cld->cld_lostlock = 1;
564 mutex_unlock(&cld->cld_lock);
565
566 /* Hold lock for rq_state */
567 spin_lock(&config_list_lock);
568 if (rq_state & RQ_STOP) {
569 spin_unlock(&config_list_lock);
570 cld->cld_lostlock = 0;
571 config_log_put(cld);
572 } else {
573 rq_state |= RQ_NOW;
574 spin_unlock(&config_list_lock);
575 wake_up(&rq_waitq);
576 }
577 }
578
579 /********************** class fns **********************/
580 static int mgc_local_llog_init(const struct lu_env *env,
581 struct obd_device *obd,
582 struct obd_device *disk)
583 {
584 struct llog_ctxt *ctxt;
585 int rc;
586
587 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
588 &llog_osd_ops);
589 if (rc)
590 return rc;
591
592 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
593 LASSERT(ctxt);
594 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
595 llog_ctxt_put(ctxt);
596
597 return 0;
598 }
599
600 static int mgc_local_llog_fini(const struct lu_env *env,
601 struct obd_device *obd)
602 {
603 struct llog_ctxt *ctxt;
604
605 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
606 llog_cleanup(env, ctxt);
607
608 return 0;
609 }
610
611 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
612 {
613 struct lustre_sb_info *lsi = s2lsi(sb);
614 struct client_obd *cli = &obd->u.cli;
615 struct lu_fid rfid, fid;
616 struct dt_object *root, *dto;
617 struct lu_env *env;
618 int rc = 0;
619
620 LASSERT(lsi);
621 LASSERT(lsi->lsi_dt_dev);
622
623 OBD_ALLOC_PTR(env);
624 if (env == NULL)
625 return -ENOMEM;
626
627 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
628 down(&cli->cl_mgc_sem);
629
630 cfs_cleanup_group_info();
631
632 /* Setup the configs dir */
633 rc = lu_env_init(env, LCT_MG_THREAD);
634 if (rc)
635 GOTO(out_err, rc);
636
637 fid.f_seq = FID_SEQ_LOCAL_NAME;
638 fid.f_oid = 1;
639 fid.f_ver = 0;
640 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
641 &cli->cl_mgc_los);
642 if (rc)
643 GOTO(out_env, rc);
644
645 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
646 if (rc)
647 GOTO(out_env, rc);
648
649 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
650 &cli->cl_mgc_los->los_dev->dd_lu_dev);
651 if (unlikely(IS_ERR(root)))
652 GOTO(out_los, rc = PTR_ERR(root));
653
654 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
655 MOUNT_CONFIGS_DIR,
656 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
657 lu_object_put_nocache(env, &root->do_lu);
658 if (IS_ERR(dto))
659 GOTO(out_los, rc = PTR_ERR(dto));
660
661 cli->cl_mgc_configs_dir = dto;
662
663 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
664 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
665 if (rc)
666 GOTO(out_llog, rc);
667
668 /* We take an obd ref to insure that we can't get to mgc_cleanup
669 * without calling mgc_fs_cleanup first. */
670 class_incref(obd, "mgc_fs", obd);
671
672 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
673 out_llog:
674 if (rc) {
675 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
676 cli->cl_mgc_configs_dir = NULL;
677 }
678 out_los:
679 if (rc < 0) {
680 local_oid_storage_fini(env, cli->cl_mgc_los);
681 cli->cl_mgc_los = NULL;
682 up(&cli->cl_mgc_sem);
683 }
684 out_env:
685 lu_env_fini(env);
686 out_err:
687 OBD_FREE_PTR(env);
688 return rc;
689 }
690
691 static int mgc_fs_cleanup(struct obd_device *obd)
692 {
693 struct lu_env env;
694 struct client_obd *cli = &obd->u.cli;
695 int rc;
696
697 LASSERT(cli->cl_mgc_los != NULL);
698
699 rc = lu_env_init(&env, LCT_MG_THREAD);
700 if (rc)
701 GOTO(unlock, rc);
702
703 mgc_local_llog_fini(&env, obd);
704
705 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
706 cli->cl_mgc_configs_dir = NULL;
707
708 local_oid_storage_fini(&env, cli->cl_mgc_los);
709 cli->cl_mgc_los = NULL;
710 lu_env_fini(&env);
711
712 unlock:
713 class_decref(obd, "mgc_fs", obd);
714 up(&cli->cl_mgc_sem);
715
716 return 0;
717 }
718
719 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
720 {
721 struct llog_ctxt *ctxt;
722 int rc;
723
724 /* setup only remote ctxt, the local disk context is switched per each
725 * filesystem during mgc_fs_setup() */
726 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
727 &llog_client_ops);
728 if (rc)
729 return rc;
730
731 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
732 LASSERT(ctxt);
733
734 llog_initiator_connect(ctxt);
735 llog_ctxt_put(ctxt);
736
737 return 0;
738 }
739
740 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
741 {
742 struct llog_ctxt *ctxt;
743
744 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
745 if (ctxt)
746 llog_cleanup(env, ctxt);
747
748 return 0;
749 }
750
751 static atomic_t mgc_count = ATOMIC_INIT(0);
752 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
753 {
754 int rc = 0;
755
756 switch (stage) {
757 case OBD_CLEANUP_EARLY:
758 break;
759 case OBD_CLEANUP_EXPORTS:
760 if (atomic_dec_and_test(&mgc_count)) {
761 int running;
762 /* stop requeue thread */
763 spin_lock(&config_list_lock);
764 running = rq_state & RQ_RUNNING;
765 if (running)
766 rq_state |= RQ_STOP;
767 spin_unlock(&config_list_lock);
768 if (running) {
769 wake_up(&rq_waitq);
770 wait_for_completion(&rq_exit);
771 }
772 }
773 obd_cleanup_client_import(obd);
774 rc = mgc_llog_fini(NULL, obd);
775 if (rc != 0)
776 CERROR("failed to cleanup llogging subsystems\n");
777 break;
778 }
779 return rc;
780 }
781
782 static int mgc_cleanup(struct obd_device *obd)
783 {
784 int rc;
785
786 /* COMPAT_146 - old config logs may have added profiles we don't
787 know about */
788 if (obd->obd_type->typ_refcnt <= 1)
789 /* Only for the last mgc */
790 class_del_profiles();
791
792 lprocfs_obd_cleanup(obd);
793 ptlrpcd_decref();
794
795 rc = client_obd_cleanup(obd);
796 return rc;
797 }
798
799 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
800 {
801 struct lprocfs_static_vars lvars;
802 int rc;
803
804 ptlrpcd_addref();
805
806 rc = client_obd_setup(obd, lcfg);
807 if (rc)
808 GOTO(err_decref, rc);
809
810 rc = mgc_llog_init(NULL, obd);
811 if (rc) {
812 CERROR("failed to setup llogging subsystems\n");
813 GOTO(err_cleanup, rc);
814 }
815
816 lprocfs_mgc_init_vars(&lvars);
817 lprocfs_obd_setup(obd, lvars.obd_vars);
818 sptlrpc_lprocfs_cliobd_attach(obd);
819
820 if (atomic_inc_return(&mgc_count) == 1) {
821 rq_state = 0;
822 init_waitqueue_head(&rq_waitq);
823
824 /* start requeue thread */
825 rc = PTR_ERR(kthread_run(mgc_requeue_thread, NULL,
826 "ll_cfg_requeue"));
827 if (IS_ERR_VALUE(rc)) {
828 CERROR("%s: Cannot start requeue thread (%d),"
829 "no more log updates!\n",
830 obd->obd_name, rc);
831 GOTO(err_cleanup, rc);
832 }
833 /* rc is the task_struct pointer of mgc_requeue_thread. */
834 rc = 0;
835 }
836
837 return rc;
838
839 err_cleanup:
840 client_obd_cleanup(obd);
841 err_decref:
842 ptlrpcd_decref();
843 return rc;
844 }
845
846 /* based on ll_mdc_blocking_ast */
847 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
848 void *data, int flag)
849 {
850 struct lustre_handle lockh;
851 struct config_llog_data *cld = (struct config_llog_data *)data;
852 int rc = 0;
853
854 switch (flag) {
855 case LDLM_CB_BLOCKING:
856 /* mgs wants the lock, give it up... */
857 LDLM_DEBUG(lock, "MGC blocking CB");
858 ldlm_lock2handle(lock, &lockh);
859 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
860 break;
861 case LDLM_CB_CANCELING:
862 /* We've given up the lock, prepare ourselves to update. */
863 LDLM_DEBUG(lock, "MGC cancel CB");
864
865 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
866 PLDLMRES(lock->l_resource),
867 (char *)&lock->l_resource->lr_name.name[0]);
868
869 if (!cld) {
870 CDEBUG(D_INFO, "missing data, won't requeue\n");
871 break;
872 }
873
874 /* held at mgc_process_log(). */
875 LASSERT(atomic_read(&cld->cld_refcount) > 0);
876 /* Are we done with this log? */
877 if (cld->cld_stopping) {
878 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
879 cld->cld_logname);
880 config_log_put(cld);
881 break;
882 }
883 /* Make sure not to re-enqueue when the mgc is stopping
884 (we get called from client_disconnect_export) */
885 if (!lock->l_conn_export ||
886 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
887 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
888 cld->cld_logname);
889 config_log_put(cld);
890 break;
891 }
892
893 /* Re-enqueue now */
894 mgc_requeue_add(cld);
895 config_log_put(cld);
896 break;
897 default:
898 LBUG();
899 }
900
901 return rc;
902 }
903
904 /* Not sure where this should go... */
905 #define MGC_ENQUEUE_LIMIT 50
906 #define MGC_TARGET_REG_LIMIT 10
907 #define MGC_SEND_PARAM_LIMIT 10
908
909 /* Send parameter to MGS*/
910 static int mgc_set_mgs_param(struct obd_export *exp,
911 struct mgs_send_param *msp)
912 {
913 struct ptlrpc_request *req;
914 struct mgs_send_param *req_msp, *rep_msp;
915 int rc;
916
917 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
918 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
919 MGS_SET_INFO);
920 if (!req)
921 return -ENOMEM;
922
923 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
924 if (!req_msp) {
925 ptlrpc_req_finished(req);
926 return -ENOMEM;
927 }
928
929 memcpy(req_msp, msp, sizeof(*req_msp));
930 ptlrpc_request_set_replen(req);
931
932 /* Limit how long we will wait for the enqueue to complete */
933 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
934 rc = ptlrpc_queue_wait(req);
935 if (!rc) {
936 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
937 memcpy(msp, rep_msp, sizeof(*rep_msp));
938 }
939
940 ptlrpc_req_finished(req);
941
942 return rc;
943 }
944
945 /* Take a config lock so we can get cancel notifications */
946 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
947 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
948 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
949 void *data, __u32 lvb_len, void *lvb_swabber,
950 struct lustre_handle *lockh)
951 {
952 struct config_llog_data *cld = (struct config_llog_data *)data;
953 struct ldlm_enqueue_info einfo = {
954 .ei_type = type,
955 .ei_mode = mode,
956 .ei_cb_bl = mgc_blocking_ast,
957 .ei_cb_cp = ldlm_completion_ast,
958 };
959 struct ptlrpc_request *req;
960 int short_limit = cld_is_sptlrpc(cld);
961 int rc;
962
963 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
964 cld->cld_resid.name[0]);
965
966 /* We need a callback for every lockholder, so don't try to
967 ldlm_lock_match (see rev 1.1.2.11.2.47) */
968 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
969 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
970 LDLM_ENQUEUE);
971 if (req == NULL)
972 return -ENOMEM;
973
974 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
975 ptlrpc_request_set_replen(req);
976
977 /* check if this is server or client */
978 if (cld->cld_cfg.cfg_sb) {
979 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
980 if (lsi && IS_SERVER(lsi))
981 short_limit = 1;
982 }
983 /* Limit how long we will wait for the enqueue to complete */
984 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
985 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
986 NULL, 0, LVB_T_NONE, lockh, 0);
987 /* A failed enqueue should still call the mgc_blocking_ast,
988 where it will be requeued if needed ("grant failed"). */
989 ptlrpc_req_finished(req);
990 return rc;
991 }
992
993 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
994 __u32 mode, struct lustre_handle *lockh)
995 {
996 ldlm_lock_decref(lockh, mode);
997
998 return 0;
999 }
1000
1001 static void mgc_notify_active(struct obd_device *unused)
1002 {
1003 /* wakeup mgc_requeue_thread to requeue mgc lock */
1004 spin_lock(&config_list_lock);
1005 rq_state |= RQ_NOW;
1006 spin_unlock(&config_list_lock);
1007 wake_up(&rq_waitq);
1008
1009 /* TODO: Help the MGS rebuild nidtbl. -jay */
1010 }
1011
1012 /* Send target_reg message to MGS */
1013 static int mgc_target_register(struct obd_export *exp,
1014 struct mgs_target_info *mti)
1015 {
1016 struct ptlrpc_request *req;
1017 struct mgs_target_info *req_mti, *rep_mti;
1018 int rc;
1019
1020 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1021 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1022 MGS_TARGET_REG);
1023 if (req == NULL)
1024 return -ENOMEM;
1025
1026 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1027 if (!req_mti) {
1028 ptlrpc_req_finished(req);
1029 return -ENOMEM;
1030 }
1031
1032 memcpy(req_mti, mti, sizeof(*req_mti));
1033 ptlrpc_request_set_replen(req);
1034 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1035 /* Limit how long we will wait for the enqueue to complete */
1036 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1037
1038 rc = ptlrpc_queue_wait(req);
1039 if (!rc) {
1040 rep_mti = req_capsule_server_get(&req->rq_pill,
1041 &RMF_MGS_TARGET_INFO);
1042 memcpy(mti, rep_mti, sizeof(*rep_mti));
1043 CDEBUG(D_MGC, "register %s got index = %d\n",
1044 mti->mti_svname, mti->mti_stripe_index);
1045 }
1046 ptlrpc_req_finished(req);
1047
1048 return rc;
1049 }
1050
1051 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1052 obd_count keylen, void *key, obd_count vallen,
1053 void *val, struct ptlrpc_request_set *set)
1054 {
1055 int rc = -EINVAL;
1056
1057 /* Turn off initial_recov after we try all backup servers once */
1058 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1059 struct obd_import *imp = class_exp2cliimp(exp);
1060 int value;
1061 if (vallen != sizeof(int))
1062 return -EINVAL;
1063 value = *(int *)val;
1064 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1065 imp->imp_obd->obd_name, value,
1066 imp->imp_deactive, imp->imp_invalid,
1067 imp->imp_replayable, imp->imp_obd->obd_replayable,
1068 ptlrpc_import_state_name(imp->imp_state));
1069 /* Resurrect if we previously died */
1070 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1071 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1072 ptlrpc_reconnect_import(imp);
1073 return 0;
1074 }
1075 /* FIXME move this to mgc_process_config */
1076 if (KEY_IS(KEY_REGISTER_TARGET)) {
1077 struct mgs_target_info *mti;
1078 if (vallen != sizeof(struct mgs_target_info))
1079 return -EINVAL;
1080 mti = (struct mgs_target_info *)val;
1081 CDEBUG(D_MGC, "register_target %s %#x\n",
1082 mti->mti_svname, mti->mti_flags);
1083 rc = mgc_target_register(exp, mti);
1084 return rc;
1085 }
1086 if (KEY_IS(KEY_SET_FS)) {
1087 struct super_block *sb = (struct super_block *)val;
1088
1089 if (vallen != sizeof(struct super_block))
1090 return -EINVAL;
1091
1092 rc = mgc_fs_setup(exp->exp_obd, sb);
1093 if (rc) {
1094 CERROR("set_fs got %d\n", rc);
1095 }
1096 return rc;
1097 }
1098 if (KEY_IS(KEY_CLEAR_FS)) {
1099 if (vallen != 0)
1100 return -EINVAL;
1101 rc = mgc_fs_cleanup(exp->exp_obd);
1102 if (rc) {
1103 CERROR("clear_fs got %d\n", rc);
1104 }
1105 return rc;
1106 }
1107 if (KEY_IS(KEY_SET_INFO)) {
1108 struct mgs_send_param *msp;
1109
1110 msp = (struct mgs_send_param *)val;
1111 rc = mgc_set_mgs_param(exp, msp);
1112 return rc;
1113 }
1114 if (KEY_IS(KEY_MGSSEC)) {
1115 struct client_obd *cli = &exp->exp_obd->u.cli;
1116 struct sptlrpc_flavor flvr;
1117
1118 /*
1119 * empty string means using current flavor, if which haven't
1120 * been set yet, set it as null.
1121 *
1122 * if flavor has been set previously, check the asking flavor
1123 * must match the existing one.
1124 */
1125 if (vallen == 0) {
1126 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1127 return 0;
1128 val = "null";
1129 vallen = 4;
1130 }
1131
1132 rc = sptlrpc_parse_flavor(val, &flvr);
1133 if (rc) {
1134 CERROR("invalid sptlrpc flavor %s to MGS\n",
1135 (char *) val);
1136 return rc;
1137 }
1138
1139 /*
1140 * caller already hold a mutex
1141 */
1142 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1143 cli->cl_flvr_mgc = flvr;
1144 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1145 sizeof(flvr)) != 0) {
1146 char str[20];
1147
1148 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1149 str, sizeof(str));
1150 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1151 "currently %s is in use\n",
1152 (char *) val, str);
1153 rc = -EPERM;
1154 }
1155 return rc;
1156 }
1157
1158 return rc;
1159 }
1160
1161 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1162 __u32 keylen, void *key, __u32 *vallen, void *val,
1163 struct lov_stripe_md *unused)
1164 {
1165 int rc = -EINVAL;
1166
1167 if (KEY_IS(KEY_CONN_DATA)) {
1168 struct obd_import *imp = class_exp2cliimp(exp);
1169 struct obd_connect_data *data = val;
1170
1171 if (*vallen == sizeof(*data)) {
1172 *data = imp->imp_connect_data;
1173 rc = 0;
1174 }
1175 }
1176
1177 return rc;
1178 }
1179
1180 static int mgc_import_event(struct obd_device *obd,
1181 struct obd_import *imp,
1182 enum obd_import_event event)
1183 {
1184 int rc = 0;
1185
1186 LASSERT(imp->imp_obd == obd);
1187 CDEBUG(D_MGC, "import event %#x\n", event);
1188
1189 switch (event) {
1190 case IMP_EVENT_DISCON:
1191 /* MGC imports should not wait for recovery */
1192 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1193 ptlrpc_pinger_ir_down();
1194 break;
1195 case IMP_EVENT_INACTIVE:
1196 break;
1197 case IMP_EVENT_INVALIDATE: {
1198 struct ldlm_namespace *ns = obd->obd_namespace;
1199 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1200 break;
1201 }
1202 case IMP_EVENT_ACTIVE:
1203 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1204 /* Clearing obd_no_recov allows us to continue pinging */
1205 obd->obd_no_recov = 0;
1206 mgc_notify_active(obd);
1207 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1208 ptlrpc_pinger_ir_up();
1209 break;
1210 case IMP_EVENT_OCD:
1211 break;
1212 case IMP_EVENT_DEACTIVATE:
1213 case IMP_EVENT_ACTIVATE:
1214 break;
1215 default:
1216 CERROR("Unknown import event %#x\n", event);
1217 LBUG();
1218 }
1219 return rc;
1220 }
1221
1222 enum {
1223 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1224 CONFIG_READ_NRPAGES = 4
1225 };
1226
1227 static int mgc_apply_recover_logs(struct obd_device *mgc,
1228 struct config_llog_data *cld,
1229 __u64 max_version,
1230 void *data, int datalen, bool mne_swab)
1231 {
1232 struct config_llog_instance *cfg = &cld->cld_cfg;
1233 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1234 struct mgs_nidtbl_entry *entry;
1235 struct lustre_cfg *lcfg;
1236 struct lustre_cfg_bufs bufs;
1237 u64 prev_version = 0;
1238 char *inst;
1239 char *buf;
1240 int bufsz;
1241 int pos;
1242 int rc = 0;
1243 int off = 0;
1244
1245 LASSERT(cfg->cfg_instance != NULL);
1246 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1247
1248 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1249 if (inst == NULL)
1250 return -ENOMEM;
1251
1252 if (!IS_SERVER(lsi)) {
1253 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1254 if (pos >= PAGE_CACHE_SIZE) {
1255 OBD_FREE(inst, PAGE_CACHE_SIZE);
1256 return -E2BIG;
1257 }
1258 } else {
1259 LASSERT(IS_MDT(lsi));
1260 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1261 PAGE_CACHE_SIZE);
1262 if (rc) {
1263 OBD_FREE(inst, PAGE_CACHE_SIZE);
1264 return -EINVAL;
1265 }
1266 pos = strlen(inst);
1267 }
1268
1269 ++pos;
1270 buf = inst + pos;
1271 bufsz = PAGE_CACHE_SIZE - pos;
1272
1273 while (datalen > 0) {
1274 int entry_len = sizeof(*entry);
1275 int is_ost;
1276 struct obd_device *obd;
1277 char *obdname;
1278 char *cname;
1279 char *params;
1280 char *uuid;
1281
1282 rc = -EINVAL;
1283 if (datalen < sizeof(*entry))
1284 break;
1285
1286 entry = (typeof(entry))(data + off);
1287
1288 /* sanity check */
1289 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1290 break;
1291 if (entry->mne_nid_count == 0) /* at least one nid entry */
1292 break;
1293 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1294 break;
1295
1296 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1297 if (datalen < entry_len) /* must have entry_len at least */
1298 break;
1299
1300 /* Keep this swab for normal mixed endian handling. LU-1644 */
1301 if (mne_swab)
1302 lustre_swab_mgs_nidtbl_entry(entry);
1303 if (entry->mne_length > PAGE_CACHE_SIZE) {
1304 CERROR("MNE too large (%u)\n", entry->mne_length);
1305 break;
1306 }
1307
1308 if (entry->mne_length < entry_len)
1309 break;
1310
1311 off += entry->mne_length;
1312 datalen -= entry->mne_length;
1313 if (datalen < 0)
1314 break;
1315
1316 if (entry->mne_version > max_version) {
1317 CERROR("entry index(%lld) is over max_index(%lld)\n",
1318 entry->mne_version, max_version);
1319 break;
1320 }
1321
1322 if (prev_version >= entry->mne_version) {
1323 CERROR("index unsorted, prev %lld, now %lld\n",
1324 prev_version, entry->mne_version);
1325 break;
1326 }
1327 prev_version = entry->mne_version;
1328
1329 /*
1330 * Write a string with format "nid::instance" to
1331 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1332 */
1333
1334 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1335 memset(buf, 0, bufsz);
1336 obdname = buf;
1337 pos = 0;
1338
1339 /* lustre-OST0001-osc-<instance #> */
1340 strcpy(obdname, cld->cld_logname);
1341 cname = strrchr(obdname, '-');
1342 if (cname == NULL) {
1343 CERROR("mgc %s: invalid logname %s\n",
1344 mgc->obd_name, obdname);
1345 break;
1346 }
1347
1348 pos = cname - obdname;
1349 obdname[pos] = 0;
1350 pos += sprintf(obdname + pos, "-%s%04x",
1351 is_ost ? "OST" : "MDT", entry->mne_index);
1352
1353 cname = is_ost ? "osc" : "mdc",
1354 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1355 lustre_cfg_bufs_reset(&bufs, obdname);
1356
1357 /* find the obd by obdname */
1358 obd = class_name2obd(obdname);
1359 if (obd == NULL) {
1360 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1361 mgc->obd_name, obdname);
1362 rc = 0;
1363 /* this is a safe race, when the ost is starting up...*/
1364 continue;
1365 }
1366
1367 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1368 ++pos;
1369 params = buf + pos;
1370 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1371 uuid = buf + pos;
1372
1373 down_read(&obd->u.cli.cl_sem);
1374 if (obd->u.cli.cl_import == NULL) {
1375 /* client does not connect to the OST yet */
1376 up_read(&obd->u.cli.cl_sem);
1377 rc = 0;
1378 continue;
1379 }
1380
1381 /* TODO: iterate all nids to find one */
1382 /* find uuid by nid */
1383 rc = client_import_find_conn(obd->u.cli.cl_import,
1384 entry->u.nids[0],
1385 (struct obd_uuid *)uuid);
1386 up_read(&obd->u.cli.cl_sem);
1387 if (rc < 0) {
1388 CERROR("mgc: cannot find uuid by nid %s\n",
1389 libcfs_nid2str(entry->u.nids[0]));
1390 break;
1391 }
1392
1393 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1394 uuid, libcfs_nid2str(entry->u.nids[0]));
1395
1396 pos += strlen(uuid);
1397 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1398 LASSERT(pos < bufsz);
1399
1400 lustre_cfg_bufs_set_string(&bufs, 1, params);
1401
1402 rc = -ENOMEM;
1403 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1404 if (lcfg == NULL) {
1405 CERROR("mgc: cannot allocate memory\n");
1406 break;
1407 }
1408
1409 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1410 prev_version, max_version, obdname, params);
1411
1412 rc = class_process_config(lcfg);
1413 lustre_cfg_free(lcfg);
1414 if (rc)
1415 CDEBUG(D_INFO, "process config for %s error %d\n",
1416 obdname, rc);
1417
1418 /* continue, even one with error */
1419 }
1420
1421 OBD_FREE(inst, PAGE_CACHE_SIZE);
1422 return rc;
1423 }
1424
1425 /**
1426 * This function is called if this client was notified for target restarting
1427 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1428 */
1429 static int mgc_process_recover_log(struct obd_device *obd,
1430 struct config_llog_data *cld)
1431 {
1432 struct ptlrpc_request *req = NULL;
1433 struct config_llog_instance *cfg = &cld->cld_cfg;
1434 struct mgs_config_body *body;
1435 struct mgs_config_res *res;
1436 struct ptlrpc_bulk_desc *desc;
1437 struct page **pages;
1438 int nrpages;
1439 bool eof = true;
1440 bool mne_swab = false;
1441 int i;
1442 int ealen;
1443 int rc;
1444
1445 /* allocate buffer for bulk transfer.
1446 * if this is the first time for this mgs to read logs,
1447 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1448 * once; otherwise, it only reads increment of logs, this should be
1449 * small and CONFIG_READ_NRPAGES will be used.
1450 */
1451 nrpages = CONFIG_READ_NRPAGES;
1452 if (cfg->cfg_last_idx == 0) /* the first time */
1453 nrpages = CONFIG_READ_NRPAGES_INIT;
1454
1455 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1456 if (pages == NULL)
1457 GOTO(out, rc = -ENOMEM);
1458
1459 for (i = 0; i < nrpages; i++) {
1460 pages[i] = alloc_page(GFP_IOFS);
1461 if (pages[i] == NULL)
1462 GOTO(out, rc = -ENOMEM);
1463 }
1464
1465 again:
1466 LASSERT(cld_is_recover(cld));
1467 LASSERT(mutex_is_locked(&cld->cld_lock));
1468 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1469 &RQF_MGS_CONFIG_READ);
1470 if (req == NULL)
1471 GOTO(out, rc = -ENOMEM);
1472
1473 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1474 if (rc)
1475 GOTO(out, rc);
1476
1477 /* pack request */
1478 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1479 LASSERT(body != NULL);
1480 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1481 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1482 >= sizeof(body->mcb_name))
1483 GOTO(out, rc = -E2BIG);
1484 body->mcb_offset = cfg->cfg_last_idx + 1;
1485 body->mcb_type = cld->cld_type;
1486 body->mcb_bits = PAGE_CACHE_SHIFT;
1487 body->mcb_units = nrpages;
1488
1489 /* allocate bulk transfer descriptor */
1490 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1491 MGS_BULK_PORTAL);
1492 if (desc == NULL)
1493 GOTO(out, rc = -ENOMEM);
1494
1495 for (i = 0; i < nrpages; i++)
1496 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1497
1498 ptlrpc_request_set_replen(req);
1499 rc = ptlrpc_queue_wait(req);
1500 if (rc)
1501 GOTO(out, rc);
1502
1503 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1504 if (res->mcr_size < res->mcr_offset)
1505 GOTO(out, rc = -EINVAL);
1506
1507 /* always update the index even though it might have errors with
1508 * handling the recover logs */
1509 cfg->cfg_last_idx = res->mcr_offset;
1510 eof = res->mcr_offset == res->mcr_size;
1511
1512 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1513 res->mcr_offset, eof == false);
1514
1515 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1516 if (ealen < 0)
1517 GOTO(out, rc = ealen);
1518
1519 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1520 GOTO(out, rc = -EINVAL);
1521
1522 if (ealen == 0) { /* no logs transferred */
1523 if (!eof)
1524 rc = -EINVAL;
1525 GOTO(out, rc);
1526 }
1527
1528 mne_swab = !!ptlrpc_rep_need_swab(req);
1529 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1530 /* This import flag means the server did an extra swab of IR MNE
1531 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1532 if (unlikely(req->rq_import->imp_need_mne_swab))
1533 mne_swab = !mne_swab;
1534 #else
1535 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1536 #endif
1537
1538 for (i = 0; i < nrpages && ealen > 0; i++) {
1539 int rc2;
1540 void *ptr;
1541
1542 ptr = kmap(pages[i]);
1543 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1544 min_t(int, ealen, PAGE_CACHE_SIZE),
1545 mne_swab);
1546 kunmap(pages[i]);
1547 if (rc2 < 0) {
1548 CWARN("Process recover log %s error %d\n",
1549 cld->cld_logname, rc2);
1550 break;
1551 }
1552
1553 ealen -= PAGE_CACHE_SIZE;
1554 }
1555
1556 out:
1557 if (req)
1558 ptlrpc_req_finished(req);
1559
1560 if (rc == 0 && !eof)
1561 goto again;
1562
1563 if (pages) {
1564 for (i = 0; i < nrpages; i++) {
1565 if (pages[i] == NULL)
1566 break;
1567 __free_page(pages[i]);
1568 }
1569 OBD_FREE(pages, sizeof(*pages) * nrpages);
1570 }
1571 return rc;
1572 }
1573
1574 static int mgc_llog_local_copy(const struct lu_env *env,
1575 struct obd_device *obd,
1576 struct llog_ctxt *rctxt,
1577 struct llog_ctxt *lctxt, char *logname)
1578 {
1579 char *temp_log;
1580 int rc;
1581
1582
1583
1584 /*
1585 * - copy it to backup using llog_backup()
1586 * - copy remote llog to logname using llog_backup()
1587 * - if failed then move bakup to logname again
1588 */
1589
1590 OBD_ALLOC(temp_log, strlen(logname) + 1);
1591 if (!temp_log)
1592 return -ENOMEM;
1593 sprintf(temp_log, "%sT", logname);
1594
1595 /* make a copy of local llog at first */
1596 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1597 if (rc < 0 && rc != -ENOENT)
1598 GOTO(out, rc);
1599 /* copy remote llog to the local copy */
1600 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1601 if (rc == -ENOENT) {
1602 /* no remote llog, delete local one too */
1603 llog_erase(env, lctxt, NULL, logname);
1604 } else if (rc < 0) {
1605 /* error during backup, get local one back from the copy */
1606 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1607 out:
1608 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1609 obd->obd_name, logname, rc);
1610 }
1611 llog_erase(env, lctxt, NULL, temp_log);
1612 OBD_FREE(temp_log, strlen(logname) + 1);
1613 return rc;
1614 }
1615
1616 /* local_only means it cannot get remote llogs */
1617 static int mgc_process_cfg_log(struct obd_device *mgc,
1618 struct config_llog_data *cld, int local_only)
1619 {
1620 struct llog_ctxt *ctxt, *lctxt = NULL;
1621 struct dt_object *cl_mgc_dir = mgc->u.cli.cl_mgc_configs_dir;
1622 struct lustre_sb_info *lsi = NULL;
1623 int rc = 0;
1624 bool sptlrpc_started = false;
1625 struct lu_env *env;
1626
1627 LASSERT(cld);
1628 LASSERT(mutex_is_locked(&cld->cld_lock));
1629
1630 /*
1631 * local copy of sptlrpc log is controlled elsewhere, don't try to
1632 * read it up here.
1633 */
1634 if (cld_is_sptlrpc(cld) && local_only)
1635 return 0;
1636
1637 if (cld->cld_cfg.cfg_sb)
1638 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1639
1640 OBD_ALLOC_PTR(env);
1641 if (env == NULL)
1642 return -ENOMEM;
1643
1644 rc = lu_env_init(env, LCT_MG_THREAD);
1645 if (rc)
1646 GOTO(out_free, rc);
1647
1648 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1649 LASSERT(ctxt);
1650
1651 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1652
1653 /* Copy the setup log locally if we can. Don't mess around if we're
1654 * running an MGS though (logs are already local). */
1655 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1656 cl_mgc_dir != NULL &&
1657 lu2dt_dev(cl_mgc_dir->do_lu.lo_dev) == lsi->lsi_dt_dev) {
1658 if (!local_only)
1659 /* Only try to copy log if we have the lock. */
1660 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1661 cld->cld_logname);
1662 if (local_only || rc) {
1663 if (llog_is_empty(env, lctxt, cld->cld_logname)) {
1664 LCONSOLE_ERROR_MSG(0x13a,
1665 "Failed to get MGS log %s and no local copy.\n",
1666 cld->cld_logname);
1667 GOTO(out_pop, rc = -ENOTCONN);
1668 }
1669 CDEBUG(D_MGC,
1670 "Failed to get MGS log %s, using local copy for now, will try to update later.\n",
1671 cld->cld_logname);
1672 }
1673 /* Now, whether we copied or not, start using the local llog.
1674 * If we failed to copy, we'll start using whatever the old
1675 * log has. */
1676 llog_ctxt_put(ctxt);
1677 ctxt = lctxt;
1678 lctxt = NULL;
1679 } else {
1680 if (local_only) /* no local log at client side */
1681 GOTO(out_pop, rc = -EIO);
1682 }
1683
1684 if (cld_is_sptlrpc(cld)) {
1685 sptlrpc_conf_log_update_begin(cld->cld_logname);
1686 sptlrpc_started = true;
1687 }
1688
1689 /* logname and instance info should be the same, so use our
1690 * copy of the instance for the update. The cfg_last_idx will
1691 * be updated here. */
1692 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1693 &cld->cld_cfg);
1694
1695 out_pop:
1696 __llog_ctxt_put(env, ctxt);
1697 if (lctxt)
1698 __llog_ctxt_put(env, lctxt);
1699
1700 /*
1701 * update settings on existing OBDs. doing it inside
1702 * of llog_process_lock so no device is attaching/detaching
1703 * in parallel.
1704 * the logname must be <fsname>-sptlrpc
1705 */
1706 if (sptlrpc_started) {
1707 LASSERT(cld_is_sptlrpc(cld));
1708 sptlrpc_conf_log_update_end(cld->cld_logname);
1709 class_notify_sptlrpc_conf(cld->cld_logname,
1710 strlen(cld->cld_logname) -
1711 strlen("-sptlrpc"));
1712 }
1713
1714 lu_env_fini(env);
1715 out_free:
1716 OBD_FREE_PTR(env);
1717 return rc;
1718 }
1719
1720 /** Get a config log from the MGS and process it.
1721 * This func is called for both clients and servers.
1722 * Copy the log locally before parsing it if appropriate (non-MGS server)
1723 */
1724 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1725 {
1726 struct lustre_handle lockh = { 0 };
1727 __u64 flags = LDLM_FL_NO_LRU;
1728 int rc = 0, rcl;
1729
1730 LASSERT(cld);
1731
1732 /* I don't want multiple processes running process_log at once --
1733 sounds like badness. It actually might be fine, as long as
1734 we're not trying to update from the same log
1735 simultaneously (in which case we should use a per-log sem.) */
1736 mutex_lock(&cld->cld_lock);
1737 if (cld->cld_stopping) {
1738 mutex_unlock(&cld->cld_lock);
1739 return 0;
1740 }
1741
1742 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1743
1744 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1745 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1746
1747 /* Get the cfg lock on the llog */
1748 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1749 LCK_CR, &flags, NULL, NULL, NULL,
1750 cld, 0, NULL, &lockh);
1751 if (rcl == 0) {
1752 /* Get the cld, it will be released in mgc_blocking_ast. */
1753 config_log_get(cld);
1754 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1755 LASSERT(rc == 0);
1756 } else {
1757 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1758
1759 /* mark cld_lostlock so that it will requeue
1760 * after MGC becomes available. */
1761 cld->cld_lostlock = 1;
1762 /* Get extra reference, it will be put in requeue thread */
1763 config_log_get(cld);
1764 }
1765
1766
1767 if (cld_is_recover(cld)) {
1768 rc = 0; /* this is not a fatal error for recover log */
1769 if (rcl == 0)
1770 rc = mgc_process_recover_log(mgc, cld);
1771 } else {
1772 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1773 }
1774
1775 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1776 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1777
1778 mutex_unlock(&cld->cld_lock);
1779
1780 /* Now drop the lock so MGS can revoke it */
1781 if (!rcl) {
1782 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1783 LCK_CR, &lockh);
1784 if (rcl)
1785 CERROR("Can't drop cfg lock: %d\n", rcl);
1786 }
1787
1788 return rc;
1789 }
1790
1791
1792 /** Called from lustre_process_log.
1793 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1794 * any services, and adds it to the list logs to watch (follow).
1795 */
1796 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1797 {
1798 struct lustre_cfg *lcfg = buf;
1799 struct config_llog_instance *cfg = NULL;
1800 char *logname;
1801 int rc = 0;
1802
1803 switch(lcfg->lcfg_command) {
1804 case LCFG_LOV_ADD_OBD: {
1805 /* Overloading this cfg command: register a new target */
1806 struct mgs_target_info *mti;
1807
1808 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1809 sizeof(struct mgs_target_info))
1810 GOTO(out, rc = -EINVAL);
1811
1812 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1813 CDEBUG(D_MGC, "add_target %s %#x\n",
1814 mti->mti_svname, mti->mti_flags);
1815 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1816 break;
1817 }
1818 case LCFG_LOV_DEL_OBD:
1819 /* Unregister has no meaning at the moment. */
1820 CERROR("lov_del_obd unimplemented\n");
1821 rc = -ENOSYS;
1822 break;
1823 case LCFG_SPTLRPC_CONF: {
1824 rc = sptlrpc_process_config(lcfg);
1825 break;
1826 }
1827 case LCFG_LOG_START: {
1828 struct config_llog_data *cld;
1829 struct super_block *sb;
1830
1831 logname = lustre_cfg_string(lcfg, 1);
1832 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1833 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1834
1835 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1836 cfg->cfg_last_idx);
1837
1838 /* We're only called through here on the initial mount */
1839 rc = config_log_add(obd, logname, cfg, sb);
1840 if (rc)
1841 break;
1842 cld = config_log_find(logname, cfg);
1843 if (cld == NULL) {
1844 rc = -ENOENT;
1845 break;
1846 }
1847
1848 /* COMPAT_146 */
1849 /* FIXME only set this for old logs! Right now this forces
1850 us to always skip the "inside markers" check */
1851 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1852
1853 rc = mgc_process_log(obd, cld);
1854 if (rc == 0 && cld->cld_recover != NULL) {
1855 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1856 imp_connect_data, IMP_RECOV)) {
1857 rc = mgc_process_log(obd, cld->cld_recover);
1858 } else {
1859 struct config_llog_data *cir = cld->cld_recover;
1860 cld->cld_recover = NULL;
1861 config_log_put(cir);
1862 }
1863 if (rc)
1864 CERROR("Cannot process recover llog %d\n", rc);
1865 }
1866 config_log_put(cld);
1867
1868 break;
1869 }
1870 case LCFG_LOG_END: {
1871 logname = lustre_cfg_string(lcfg, 1);
1872
1873 if (lcfg->lcfg_bufcount >= 2)
1874 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1875 lcfg, 2);
1876 rc = config_log_end(logname, cfg);
1877 break;
1878 }
1879 default: {
1880 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1881 GOTO(out, rc = -EINVAL);
1882
1883 }
1884 }
1885 out:
1886 return rc;
1887 }
1888
1889 struct obd_ops mgc_obd_ops = {
1890 .o_owner = THIS_MODULE,
1891 .o_setup = mgc_setup,
1892 .o_precleanup = mgc_precleanup,
1893 .o_cleanup = mgc_cleanup,
1894 .o_add_conn = client_import_add_conn,
1895 .o_del_conn = client_import_del_conn,
1896 .o_connect = client_connect_import,
1897 .o_disconnect = client_disconnect_export,
1898 //.o_enqueue = mgc_enqueue,
1899 .o_cancel = mgc_cancel,
1900 //.o_iocontrol = mgc_iocontrol,
1901 .o_set_info_async = mgc_set_info_async,
1902 .o_get_info = mgc_get_info,
1903 .o_import_event = mgc_import_event,
1904 .o_process_config = mgc_process_config,
1905 };
1906
1907 int __init mgc_init(void)
1908 {
1909 return class_register_type(&mgc_obd_ops, NULL, NULL,
1910 LUSTRE_MGC_NAME, NULL);
1911 }
1912
1913 static void /*__exit*/ mgc_exit(void)
1914 {
1915 class_unregister_type(LUSTRE_MGC_NAME);
1916 }
1917
1918 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1919 MODULE_DESCRIPTION("Lustre Management Client");
1920 MODULE_LICENSE("GPL");
1921
1922 module_init(mgc_init);
1923 module_exit(mgc_exit);
This page took 0.066726 seconds and 4 git commands to generate.