1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fid/fid_handler.c
38 * Lustre Sequence Manager
40 * Author: Yury Umanets <umka@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_FID
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <dt_object.h>
58 #include <md_object.h>
59 #include <obd_support.h>
60 #include <lustre_req_layout.h>
61 #include <lustre_fid.h>
62 #include "fid_internal.h"
65 /* Assigns client to sequence controller node. */
66 int seq_server_set_cli(struct lu_server_seq *seq,
67 struct lu_client_seq *cli,
68 const struct lu_env *env)
74 * Ask client for new range, assign that range to ->seq_space and write
75 * seq state to backing store should be atomic.
80 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
81 seq->lss_name, cli->lcs_name);
86 if (seq->lss_cli != NULL) {
87 CERROR("%s: Sequence controller is already "
88 "assigned\n", seq->lss_name);
89 GOTO(out_up, rc = -EINVAL);
92 CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
93 seq->lss_name, cli->lcs_name);
101 EXPORT_SYMBOL(seq_server_set_cli);
104 * On controller node, allocate new super sequence for regular sequence server.
106 static int __seq_server_alloc_super(struct lu_server_seq *seq,
108 struct lu_range *out,
109 const struct lu_env *env)
111 struct lu_range *space = &seq->lss_space;
115 LASSERT(range_is_sane(space));
118 CDEBUG(D_INFO, "%s: Input seq range: "
119 DRANGE"\n", seq->lss_name, PRANGE(in));
121 if (in->lr_end > space->lr_start)
122 space->lr_start = in->lr_end;
125 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
126 seq->lss_name, PRANGE(space));
128 if (range_space(space) < seq->lss_width) {
129 CWARN("%s: Sequences space to be exhausted soon. "
130 "Only "LPU64" sequences left\n", seq->lss_name,
133 space->lr_start = space->lr_end;
134 } else if (range_is_exhausted(space)) {
135 CERROR("%s: Sequences space is exhausted\n",
139 range_alloc(out, space, seq->lss_width);
143 rc = seq_store_write(seq, env);
145 CERROR("%s: Can't write space data, rc %d\n",
150 CDEBUG(D_INFO, "%s: Allocated super-sequence "
151 DRANGE"\n", seq->lss_name, PRANGE(out));
156 int seq_server_alloc_super(struct lu_server_seq *seq,
158 struct lu_range *out,
159 const struct lu_env *env)
165 rc = __seq_server_alloc_super(seq, in, out, env);
171 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
173 struct lu_range *out,
174 const struct lu_env *env)
176 struct lu_range *space = &seq->lss_space;
180 LASSERT(range_is_sane(space));
183 * This is recovery case. Adjust super range if input range looks like
184 * it is allocated from new super.
187 CDEBUG(D_INFO, "%s: Input seq range: "
188 DRANGE"\n", seq->lss_name, PRANGE(in));
190 if (range_is_exhausted(space)) {
192 * Server cannot send empty range to client, this is why
193 * we check here that range from client is "newer" than
196 LASSERT(in->lr_end > space->lr_start);
199 * Start is set to end of last allocated, because it
200 * *is* already allocated so we take that into account
201 * and do not use for other allocations.
203 space->lr_start = in->lr_end;
206 * End is set to in->lr_start + super sequence
207 * allocation unit. That is because in->lr_start is
208 * first seq in new allocated range from controller
211 space->lr_end = in->lr_start + LUSTRE_SEQ_SUPER_WIDTH;
214 CERROR("%s: No sequence controller "
215 "is attached.\n", seq->lss_name);
220 * Let controller know that this is recovery and last
221 * obtained range from it was @space.
223 rc = seq_client_replay_super(seq->lss_cli, space, env);
225 CERROR("%s: Can't replay super-sequence, "
226 "rc %d\n", seq->lss_name, rc);
231 * Update super start by end from client's range. Super
232 * end should not be changed if range was not exhausted.
234 if (in->lr_end > space->lr_start)
235 space->lr_start = in->lr_end;
240 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
241 seq->lss_name, PRANGE(space));
244 * XXX: Avoid cascading RPCs using kind of async preallocation
245 * when meta-sequence is close to exhausting.
247 if (range_is_exhausted(space)) {
249 CERROR("%s: No sequence controller "
250 "is attached.\n", seq->lss_name);
254 rc = seq_client_alloc_super(seq->lss_cli, env);
256 CERROR("%s: Can't allocate super-sequence, "
257 "rc %d\n", seq->lss_name, rc);
261 /* Saving new range to allocation space. */
262 *space = seq->lss_cli->lcs_space;
263 LASSERT(range_is_sane(space));
266 range_alloc(out, space, seq->lss_width);
269 rc = seq_store_write(seq, env);
271 CERROR("%s: Can't write space data, rc %d\n",
276 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
277 DRANGE"\n", seq->lss_name, PRANGE(out));
283 int seq_server_alloc_meta(struct lu_server_seq *seq,
285 struct lu_range *out,
286 const struct lu_env *env)
292 rc = __seq_server_alloc_meta(seq, in, out, env);
297 EXPORT_SYMBOL(seq_server_alloc_meta);
299 static int seq_server_handle(struct lu_site *site,
300 const struct lu_env *env,
301 __u32 opc, struct lu_range *in,
302 struct lu_range *out)
309 if (!site->ls_server_seq) {
310 CERROR("Sequence server is not "
314 rc = seq_server_alloc_meta(site->ls_server_seq,
317 case SEQ_ALLOC_SUPER:
318 if (!site->ls_control_seq) {
319 CERROR("Sequence controller is not "
323 rc = seq_server_alloc_super(site->ls_control_seq,
334 static int seq_req_handle(struct ptlrpc_request *req,
335 const struct lu_env *env,
336 struct seq_thread_info *info)
338 struct lu_range *out, *in = NULL;
339 struct lu_site *site;
344 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
345 LASSERT(site != NULL);
347 rc = req_capsule_server_pack(info->sti_pill);
349 RETURN(err_serious(rc));
351 opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
353 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
355 RETURN(err_serious(-EPROTO));
357 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
358 in = req_capsule_client_get(info->sti_pill,
361 LASSERT(!range_is_zero(in) && range_is_sane(in));
364 rc = seq_server_handle(site, env, *opc, in, out);
366 rc = err_serious(-EPROTO);
371 /* context key constructor/destructor: seq_key_init, seq_key_fini */
372 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
374 /* context key: seq_thread_key */
375 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
377 static void seq_thread_info_init(struct ptlrpc_request *req,
378 struct seq_thread_info *info)
380 info->sti_pill = &req->rq_pill;
381 /* Init request capsule */
382 req_capsule_init(info->sti_pill, req, RCL_SERVER);
383 req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
386 static void seq_thread_info_fini(struct seq_thread_info *info)
388 req_capsule_fini(info->sti_pill);
391 static int seq_handle(struct ptlrpc_request *req)
393 const struct lu_env *env;
394 struct seq_thread_info *info;
397 env = req->rq_svc_thread->t_env;
398 LASSERT(env != NULL);
400 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
401 LASSERT(info != NULL);
403 seq_thread_info_init(req, info);
404 rc = seq_req_handle(req, env, info);
405 seq_thread_info_fini(info);
411 * Entry point for handling FLD RPCs called from MDT.
413 int seq_query(struct com_thread_info *info)
415 return seq_handle(info->cti_pill->rc_req);
417 EXPORT_SYMBOL(seq_query);
419 static void seq_server_proc_fini(struct lu_server_seq *seq);
422 static int seq_server_proc_init(struct lu_server_seq *seq)
427 seq->lss_proc_dir = lprocfs_register(seq->lss_name,
430 if (IS_ERR(seq->lss_proc_dir)) {
431 rc = PTR_ERR(seq->lss_proc_dir);
435 rc = lprocfs_add_vars(seq->lss_proc_dir,
436 seq_server_proc_list, seq);
438 CERROR("%s: Can't init sequence manager "
439 "proc, rc %d\n", seq->lss_name, rc);
440 GOTO(out_cleanup, rc);
446 seq_server_proc_fini(seq);
450 static void seq_server_proc_fini(struct lu_server_seq *seq)
453 if (seq->lss_proc_dir != NULL) {
454 if (!IS_ERR(seq->lss_proc_dir))
455 lprocfs_remove(&seq->lss_proc_dir);
456 seq->lss_proc_dir = NULL;
461 static int seq_server_proc_init(struct lu_server_seq *seq)
466 static void seq_server_proc_fini(struct lu_server_seq *seq)
472 int seq_server_init(struct lu_server_seq *seq,
473 struct dt_device *dev,
475 enum lu_mgr_type type,
476 const struct lu_env *env)
478 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
481 LASSERT(dev != NULL);
482 LASSERT(prefix != NULL);
485 seq->lss_type = type;
486 range_zero(&seq->lss_space);
487 sema_init(&seq->lss_sem, 1);
489 seq->lss_width = is_srv ?
490 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
492 snprintf(seq->lss_name, sizeof(seq->lss_name),
493 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
495 rc = seq_store_init(seq, env, dev);
499 /* Request backing store for saved sequence info. */
500 rc = seq_store_read(seq, env);
501 if (rc == -ENODATA) {
503 /* Nothing is read, init by default value. */
504 seq->lss_space = is_srv ?
505 LUSTRE_SEQ_ZERO_RANGE:
506 LUSTRE_SEQ_SPACE_RANGE;
508 CDEBUG(D_INFO, "%s: No data found "
509 "on store. Initialize space\n",
512 /* Save default controller value to store. */
513 rc = seq_store_write(seq, env);
515 CERROR("%s: Can't write space data, "
516 "rc %d\n", seq->lss_name, rc);
519 CERROR("%s: Can't read space data, rc %d\n",
525 LASSERT(range_is_sane(&seq->lss_space));
527 LASSERT(!range_is_zero(&seq->lss_space) &&
528 range_is_sane(&seq->lss_space));
531 rc = seq_server_proc_init(seq);
538 seq_server_fini(seq, env);
541 EXPORT_SYMBOL(seq_server_init);
543 void seq_server_fini(struct lu_server_seq *seq,
544 const struct lu_env *env)
548 seq_server_proc_fini(seq);
549 seq_store_fini(seq, env);
553 EXPORT_SYMBOL(seq_server_fini);
555 cfs_proc_dir_entry_t *seq_type_proc_dir = NULL;
557 static int __init fid_mod_init(void)
559 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
562 if (IS_ERR(seq_type_proc_dir))
563 return PTR_ERR(seq_type_proc_dir);
565 LU_CONTEXT_KEY_INIT(&seq_thread_key);
566 lu_context_key_register(&seq_thread_key);
570 static void __exit fid_mod_exit(void)
572 lu_context_key_degister(&seq_thread_key);
573 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
574 lprocfs_remove(&seq_type_proc_dir);
575 seq_type_proc_dir = NULL;
579 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
580 MODULE_DESCRIPTION("Lustre FID Module");
581 MODULE_LICENSE("GPL");
583 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);