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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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);
96 cli->lcs_space.lsr_mdt = seq->lss_site->ms_node_id;
102 EXPORT_SYMBOL(seq_server_set_cli);
105 * On controller node, allocate new super sequence for regular sequence server.
106 * As this super sequence controller, this node suppose to maintain fld
108 * \a out range always has currect mds node number of requester.
111 static int __seq_server_alloc_super(struct lu_server_seq *seq,
112 struct lu_seq_range *in,
113 struct lu_seq_range *out,
114 const struct lu_env *env)
116 struct lu_seq_range *space = &seq->lss_space;
118 __u64 mdt = out->lsr_mdt;
122 LASSERT(range_is_sane(space));
125 CDEBUG(D_INFO, "%s: Input seq range: "
126 DRANGE"\n", seq->lss_name, PRANGE(in));
128 if (in->lsr_end > space->lsr_start)
129 space->lsr_start = in->lsr_end;
132 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
133 seq->lss_name, PRANGE(space));
135 if (range_space(space) < seq->lss_width) {
136 CWARN("%s: Sequences space to be exhausted soon. "
137 "Only "LPU64" sequences left\n", seq->lss_name,
140 space->lsr_start = space->lsr_end;
141 } else if (range_is_exhausted(space)) {
142 CERROR("%s: Sequences space is exhausted\n",
146 range_alloc(out, space, seq->lss_width);
151 credit = SEQ_TXN_STORE_CREDITS + FLD_TXN_INDEX_INSERT_CREDITS;
153 th = seq_store_trans_start(seq, env, credit);
157 rc = seq_store_write(seq, env, th);
159 CERROR("%s: Can't write space data, rc %d\n",
164 rc = fld_server_create(seq->lss_site->ms_server_fld,
167 CERROR("%s: Can't Update fld database, rc %d\n",
172 seq_store_trans_stop(seq, env, th);
174 CDEBUG(D_INFO, "%s: super-sequence allocation rc = %d "
175 DRANGE"\n", seq->lss_name, rc, PRANGE(out));
180 int seq_server_alloc_super(struct lu_server_seq *seq,
181 struct lu_seq_range *in,
182 struct lu_seq_range *out,
183 const struct lu_env *env)
189 rc = __seq_server_alloc_super(seq, in, out, env);
195 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
196 struct lu_seq_range *in,
197 struct lu_seq_range *out,
198 const struct lu_env *env)
200 struct lu_seq_range *space = &seq->lss_space;
206 LASSERT(range_is_sane(space));
209 * This is recovery case. Adjust super range if input range looks like
210 * it is allocated from new super.
213 CDEBUG(D_INFO, "%s: Input seq range: "
214 DRANGE"\n", seq->lss_name, PRANGE(in));
216 if (range_is_exhausted(space)) {
218 * Server cannot send empty range to client, this is why
219 * we check here that range from client is "newer" than
222 LASSERT(in->lsr_end > space->lsr_start);
225 * Start is set to end of last allocated, because it
226 * *is* already allocated so we take that into account
227 * and do not use for other allocations.
229 space->lsr_start = in->lsr_end;
232 * End is set to in->lsr_start + super sequence
233 * allocation unit. That is because in->lsr_start is
234 * first seq in new allocated range from controller
237 space->lsr_end = in->lsr_start + LUSTRE_SEQ_SUPER_WIDTH;
240 CERROR("%s: No sequence controller "
241 "is attached.\n", seq->lss_name);
246 * Let controller know that this is recovery and last
247 * obtained range from it was @space.
249 rc = seq_client_replay_super(seq->lss_cli, space, env);
252 CERROR("%s: Can't replay super-sequence, "
253 "rc %d\n", seq->lss_name, rc);
258 * Update super start by end from client's range. Super
259 * end should not be changed if range was not exhausted.
261 if (in->lsr_end > space->lsr_start)
262 space->lsr_start = in->lsr_end;
265 /* sending replay_super to update fld as only super sequence
266 * server can update fld.
267 * we are sending meta sequence to fld rather than super
268 * sequence, but fld server can handle range merging. */
270 in->lsr_mdt = space->lsr_mdt;
271 rc = seq_client_replay_super(seq->lss_cli, in, env);
274 CERROR("%s: Can't replay super-sequence, "
275 "rc %d\n", seq->lss_name, rc);
281 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
282 seq->lss_name, PRANGE(space));
285 * XXX: Avoid cascading RPCs using kind of async preallocation
286 * when meta-sequence is close to exhausting.
288 if (range_is_exhausted(space)) {
290 CERROR("%s: No sequence controller "
291 "is attached.\n", seq->lss_name);
295 rc = seq_client_alloc_super(seq->lss_cli, env);
297 CERROR("%s: Can't allocate super-sequence, "
298 "rc %d\n", seq->lss_name, rc);
302 /* Saving new range to allocation space. */
303 *space = seq->lss_cli->lcs_space;
304 LASSERT(range_is_sane(space));
307 range_alloc(out, space, seq->lss_width);
310 th = seq_store_trans_start(seq, env, SEQ_TXN_STORE_CREDITS);
314 rc = seq_store_write(seq, env, th);
316 CERROR("%s: Can't write space data, rc %d\n",
321 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
322 DRANGE"\n", seq->lss_name, PRANGE(out));
325 seq_store_trans_stop(seq, env, th);
329 int seq_server_alloc_meta(struct lu_server_seq *seq,
330 struct lu_seq_range *in,
331 struct lu_seq_range *out,
332 const struct lu_env *env)
338 rc = __seq_server_alloc_meta(seq, in, out, env);
343 EXPORT_SYMBOL(seq_server_alloc_meta);
345 static int seq_server_handle(struct lu_site *site,
346 const struct lu_env *env,
347 __u32 opc, struct lu_seq_range *in,
348 struct lu_seq_range *out)
351 struct md_site *mite;
354 mite = lu_site2md(site);
357 if (!mite->ms_server_seq) {
358 CERROR("Sequence server is not "
362 rc = seq_server_alloc_meta(mite->ms_server_seq,
365 case SEQ_ALLOC_SUPER:
366 if (!mite->ms_control_seq) {
367 CERROR("Sequence controller is not "
371 rc = seq_server_alloc_super(mite->ms_control_seq,
382 static int seq_req_handle(struct ptlrpc_request *req,
383 const struct lu_env *env,
384 struct seq_thread_info *info)
386 struct lu_seq_range *out, *in = NULL, *tmp;
387 struct lu_site *site;
392 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
393 LASSERT(site != NULL);
395 rc = req_capsule_server_pack(info->sti_pill);
397 RETURN(err_serious(rc));
399 opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
401 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
403 RETURN(err_serious(-EPROTO));
405 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
407 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
409 LASSERT(!range_is_zero(in) && range_is_sane(in));
411 /* seq client passed mdt id, we need to pass that using out
414 out->lsr_mdt = tmp->lsr_mdt;
415 rc = seq_server_handle(site, env, *opc, in, out);
417 rc = err_serious(-EPROTO);
422 /* context key constructor/destructor: seq_key_init, seq_key_fini */
423 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
425 /* context key: seq_thread_key */
426 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
428 static void seq_thread_info_init(struct ptlrpc_request *req,
429 struct seq_thread_info *info)
431 info->sti_pill = &req->rq_pill;
432 /* Init request capsule */
433 req_capsule_init(info->sti_pill, req, RCL_SERVER);
434 req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
437 static void seq_thread_info_fini(struct seq_thread_info *info)
439 req_capsule_fini(info->sti_pill);
442 static int seq_handle(struct ptlrpc_request *req)
444 const struct lu_env *env;
445 struct seq_thread_info *info;
448 env = req->rq_svc_thread->t_env;
449 LASSERT(env != NULL);
451 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
452 LASSERT(info != NULL);
454 seq_thread_info_init(req, info);
455 rc = seq_req_handle(req, env, info);
456 seq_thread_info_fini(info);
462 * Entry point for handling FLD RPCs called from MDT.
464 int seq_query(struct com_thread_info *info)
466 return seq_handle(info->cti_pill->rc_req);
468 EXPORT_SYMBOL(seq_query);
470 static void seq_server_proc_fini(struct lu_server_seq *seq);
473 static int seq_server_proc_init(struct lu_server_seq *seq)
478 seq->lss_proc_dir = lprocfs_register(seq->lss_name,
481 if (IS_ERR(seq->lss_proc_dir)) {
482 rc = PTR_ERR(seq->lss_proc_dir);
486 rc = lprocfs_add_vars(seq->lss_proc_dir,
487 seq_server_proc_list, seq);
489 CERROR("%s: Can't init sequence manager "
490 "proc, rc %d\n", seq->lss_name, rc);
491 GOTO(out_cleanup, rc);
497 seq_server_proc_fini(seq);
501 static void seq_server_proc_fini(struct lu_server_seq *seq)
504 if (seq->lss_proc_dir != NULL) {
505 if (!IS_ERR(seq->lss_proc_dir))
506 lprocfs_remove(&seq->lss_proc_dir);
507 seq->lss_proc_dir = NULL;
512 static int seq_server_proc_init(struct lu_server_seq *seq)
517 static void seq_server_proc_fini(struct lu_server_seq *seq)
523 int seq_server_init(struct lu_server_seq *seq,
524 struct dt_device *dev,
526 enum lu_mgr_type type,
528 const struct lu_env *env)
531 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
534 LASSERT(dev != NULL);
535 LASSERT(prefix != NULL);
538 seq->lss_type = type;
540 range_init(&seq->lss_space);
541 sema_init(&seq->lss_sem, 1);
543 seq->lss_width = is_srv ?
544 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
546 snprintf(seq->lss_name, sizeof(seq->lss_name),
547 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
549 rc = seq_store_init(seq, env, dev);
552 /* Request backing store for saved sequence info. */
553 rc = seq_store_read(seq, env);
554 if (rc == -ENODATA) {
556 /* Nothing is read, init by default value. */
557 seq->lss_space = is_srv ?
558 LUSTRE_SEQ_ZERO_RANGE:
559 LUSTRE_SEQ_SPACE_RANGE;
561 seq->lss_space.lsr_mdt = ms->ms_node_id;
562 CDEBUG(D_INFO, "%s: No data found "
563 "on store. Initialize space\n",
566 th = seq_store_trans_start(seq, env, SEQ_TXN_STORE_CREDITS);
570 /* Save default controller value to store. */
571 rc = seq_store_write(seq, env, th);
573 CERROR("%s: Can't write space data, "
574 "rc %d\n", seq->lss_name, rc);
576 seq_store_trans_stop(seq, env, th);
578 CERROR("%s: Can't read space data, rc %d\n",
584 LASSERT(range_is_sane(&seq->lss_space));
586 LASSERT(!range_is_zero(&seq->lss_space) &&
587 range_is_sane(&seq->lss_space));
590 rc = seq_server_proc_init(seq);
597 seq_server_fini(seq, env);
600 EXPORT_SYMBOL(seq_server_init);
602 void seq_server_fini(struct lu_server_seq *seq,
603 const struct lu_env *env)
607 seq_server_proc_fini(seq);
608 seq_store_fini(seq, env);
612 EXPORT_SYMBOL(seq_server_fini);
614 cfs_proc_dir_entry_t *seq_type_proc_dir = NULL;
616 static struct lu_local_obj_desc llod_seq_srv = {
617 .llod_name = LUSTRE_SEQ_SRV_NAME,
618 .llod_oid = FID_SEQ_SRV_OID,
622 static struct lu_local_obj_desc llod_seq_ctl = {
623 .llod_name = LUSTRE_SEQ_CTL_NAME,
624 .llod_oid = FID_SEQ_CTL_OID,
628 static int __init fid_mod_init(void)
630 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
633 if (IS_ERR(seq_type_proc_dir))
634 return PTR_ERR(seq_type_proc_dir);
636 llo_local_obj_register(&llod_seq_srv);
637 llo_local_obj_register(&llod_seq_ctl);
639 LU_CONTEXT_KEY_INIT(&seq_thread_key);
640 lu_context_key_register(&seq_thread_key);
644 static void __exit fid_mod_exit(void)
646 llo_local_obj_unregister(&llod_seq_srv);
647 llo_local_obj_unregister(&llod_seq_ctl);
649 lu_context_key_degister(&seq_thread_key);
650 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
651 lprocfs_remove(&seq_type_proc_dir);
652 seq_type_proc_dir = NULL;
656 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
657 MODULE_DESCRIPTION("Lustre FID Module");
658 MODULE_LICENSE("GPL");
660 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);