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