4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
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>
43 #define DEBUG_SUBSYSTEM S_FID
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <dt_object.h>
55 #include <md_object.h>
56 #include <obd_support.h>
57 #include <lustre_req_layout.h>
58 #include <lustre_fid.h>
59 #include "fid_internal.h"
62 /* Assigns client to sequence controller node. */
63 int seq_server_set_cli(struct lu_server_seq *seq,
64 struct lu_client_seq *cli,
65 const struct lu_env *env)
71 * Ask client for new range, assign that range to ->seq_space and write
72 * seq state to backing store should be atomic.
74 cfs_mutex_lock(&seq->lss_mutex);
77 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
78 seq->lss_name, cli->lcs_name);
83 if (seq->lss_cli != NULL) {
84 CERROR("%s: Sequence controller is already "
85 "assigned\n", seq->lss_name);
86 GOTO(out_up, rc = -EINVAL);
89 CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
90 seq->lss_name, cli->lcs_name);
93 cli->lcs_space.lsr_index = seq->lss_site->ms_node_id;
96 cfs_mutex_unlock(&seq->lss_mutex);
99 EXPORT_SYMBOL(seq_server_set_cli);
101 * allocate \a w units of sequence from range \a from.
103 static inline void range_alloc(struct lu_seq_range *to,
104 struct lu_seq_range *from,
107 width = min(range_space(from), width);
108 to->lsr_start = from->lsr_start;
109 to->lsr_end = from->lsr_start + width;
110 from->lsr_start += width;
114 * On controller node, allocate new super sequence for regular sequence server.
115 * As this super sequence controller, this node suppose to maintain fld
117 * \a out range always has currect mds node number of requester.
120 static int __seq_server_alloc_super(struct lu_server_seq *seq,
121 struct lu_seq_range *out,
122 const struct lu_env *env)
124 struct lu_seq_range *space = &seq->lss_space;
128 LASSERT(range_is_sane(space));
130 if (range_is_exhausted(space)) {
131 CERROR("%s: Sequences space is exhausted\n",
135 range_alloc(out, space, seq->lss_width);
138 rc = seq_store_update(env, seq, out, 1 /* sync */);
140 CDEBUG(D_INFO, "%s: super-sequence allocation rc = %d "
141 DRANGE"\n", seq->lss_name, rc, PRANGE(out));
146 int seq_server_alloc_super(struct lu_server_seq *seq,
147 struct lu_seq_range *out,
148 const struct lu_env *env)
153 cfs_mutex_lock(&seq->lss_mutex);
154 rc = __seq_server_alloc_super(seq, out, env);
155 cfs_mutex_unlock(&seq->lss_mutex);
160 static int __seq_set_init(const struct lu_env *env,
161 struct lu_server_seq *seq)
163 struct lu_seq_range *space = &seq->lss_space;
166 range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
167 range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
169 rc = seq_store_update(env, seq, NULL, 1);
175 * This function implements new seq allocation algorithm using async
176 * updates to seq file on disk. ref bug 18857 for details.
177 * there are four variable to keep track of this process
179 * lss_space; - available lss_space
180 * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
181 * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
184 * when lss_lowater_set reaches the end it is replaced with hiwater one and
185 * a write operation is initiated to allocate new hiwater range.
186 * if last seq write opearion is still not commited, current operation is
187 * flaged as sync write op.
189 static int range_alloc_set(const struct lu_env *env,
190 struct lu_seq_range *out,
191 struct lu_server_seq *seq)
193 struct lu_seq_range *space = &seq->lss_space;
194 struct lu_seq_range *loset = &seq->lss_lowater_set;
195 struct lu_seq_range *hiset = &seq->lss_hiwater_set;
198 if (range_is_zero(loset))
199 __seq_set_init(env, seq);
201 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
202 loset->lsr_start = loset->lsr_end;
204 if (range_is_exhausted(loset)) {
205 /* reached high water mark. */
206 struct lu_device *dev = seq->lss_site->ms_lu.ls_top_dev;
207 int obd_num_clients = dev->ld_obd->obd_num_exports;
210 /* calculate new seq width based on number of clients */
211 set_sz = max(seq->lss_set_width,
212 obd_num_clients * seq->lss_width);
213 set_sz = min(range_space(space), set_sz);
215 /* Switch to hiwater range now */
217 /* allocate new hiwater range */
218 range_alloc(hiset, space, set_sz);
220 /* update ondisk seq with new *space */
221 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
224 LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
225 DRANGE"\n", PRANGE(loset));
228 range_alloc(out, loset, seq->lss_width);
233 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
234 struct lu_seq_range *out,
235 const struct lu_env *env)
237 struct lu_seq_range *space = &seq->lss_space;
242 LASSERT(range_is_sane(space));
244 /* Check if available space ends and allocate new super seq */
245 if (range_is_exhausted(space)) {
247 CERROR("%s: No sequence controller is attached.\n",
252 rc = seq_client_alloc_super(seq->lss_cli, env);
254 CERROR("%s: Can't allocate super-sequence, rc %d\n",
259 /* Saving new range to allocation space. */
260 *space = seq->lss_cli->lcs_space;
261 LASSERT(range_is_sane(space));
264 rc = range_alloc_set(env, out, seq);
266 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
267 DRANGE"\n", seq->lss_name, PRANGE(out));
273 int seq_server_alloc_meta(struct lu_server_seq *seq,
274 struct lu_seq_range *out,
275 const struct lu_env *env)
280 cfs_mutex_lock(&seq->lss_mutex);
281 rc = __seq_server_alloc_meta(seq, out, env);
282 cfs_mutex_unlock(&seq->lss_mutex);
286 EXPORT_SYMBOL(seq_server_alloc_meta);
288 static int seq_server_handle(struct lu_site *site,
289 const struct lu_env *env,
290 __u32 opc, struct lu_seq_range *out)
293 struct md_site *mite;
296 mite = lu_site2md(site);
299 if (!mite->ms_server_seq) {
300 CERROR("Sequence server is not "
304 rc = seq_server_alloc_meta(mite->ms_server_seq, out, env);
306 case SEQ_ALLOC_SUPER:
307 if (!mite->ms_control_seq) {
308 CERROR("Sequence controller is not "
312 rc = seq_server_alloc_super(mite->ms_control_seq, out, env);
322 static int seq_req_handle(struct ptlrpc_request *req,
323 const struct lu_env *env,
324 struct seq_thread_info *info)
326 struct lu_seq_range *out, *tmp;
327 struct lu_site *site;
332 LASSERT(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY));
333 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
334 LASSERT(site != NULL);
336 rc = req_capsule_server_pack(info->sti_pill);
338 RETURN(err_serious(rc));
340 opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
342 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
344 RETURN(err_serious(-EPROTO));
346 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
348 /* seq client passed mdt id, we need to pass that using out
351 out->lsr_index = tmp->lsr_index;
352 out->lsr_flags = tmp->lsr_flags;
353 rc = seq_server_handle(site, env, *opc, out);
355 rc = err_serious(-EPROTO);
360 /* context key constructor/destructor: seq_key_init, seq_key_fini */
361 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
363 /* context key: seq_thread_key */
364 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
366 static void seq_thread_info_init(struct ptlrpc_request *req,
367 struct seq_thread_info *info)
369 info->sti_pill = &req->rq_pill;
370 /* Init request capsule */
371 req_capsule_init(info->sti_pill, req, RCL_SERVER);
372 req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
375 static void seq_thread_info_fini(struct seq_thread_info *info)
377 req_capsule_fini(info->sti_pill);
380 static int seq_handle(struct ptlrpc_request *req)
382 const struct lu_env *env;
383 struct seq_thread_info *info;
386 env = req->rq_svc_thread->t_env;
387 LASSERT(env != NULL);
389 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
390 LASSERT(info != NULL);
392 seq_thread_info_init(req, info);
393 rc = seq_req_handle(req, env, info);
394 /* XXX: we don't need replay but MDT assign transno in any case,
395 * remove it manually before reply*/
396 lustre_msg_set_transno(req->rq_repmsg, 0);
397 seq_thread_info_fini(info);
403 * Entry point for handling FLD RPCs called from MDT.
405 int seq_query(struct com_thread_info *info)
407 return seq_handle(info->cti_pill->rc_req);
409 EXPORT_SYMBOL(seq_query);
411 static void seq_server_proc_fini(struct lu_server_seq *seq);
414 static int seq_server_proc_init(struct lu_server_seq *seq)
419 seq->lss_proc_dir = lprocfs_register(seq->lss_name,
422 if (IS_ERR(seq->lss_proc_dir)) {
423 rc = PTR_ERR(seq->lss_proc_dir);
427 rc = lprocfs_add_vars(seq->lss_proc_dir,
428 seq_server_proc_list, seq);
430 CERROR("%s: Can't init sequence manager "
431 "proc, rc %d\n", seq->lss_name, rc);
432 GOTO(out_cleanup, rc);
438 seq_server_proc_fini(seq);
442 static void seq_server_proc_fini(struct lu_server_seq *seq)
445 if (seq->lss_proc_dir != NULL) {
446 if (!IS_ERR(seq->lss_proc_dir))
447 lprocfs_remove(&seq->lss_proc_dir);
448 seq->lss_proc_dir = NULL;
453 static int seq_server_proc_init(struct lu_server_seq *seq)
458 static void seq_server_proc_fini(struct lu_server_seq *seq)
465 int seq_server_init(struct lu_server_seq *seq,
466 struct dt_device *dev,
468 enum lu_mgr_type type,
470 const struct lu_env *env)
472 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
475 LASSERT(dev != NULL);
476 LASSERT(prefix != NULL);
479 seq->lss_type = type;
481 range_init(&seq->lss_space);
483 range_init(&seq->lss_lowater_set);
484 range_init(&seq->lss_hiwater_set);
485 seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
487 cfs_mutex_init(&seq->lss_mutex);
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);
498 /* Request backing store for saved sequence info. */
499 rc = seq_store_read(seq, env);
500 if (rc == -ENODATA) {
502 /* Nothing is read, init by default value. */
503 seq->lss_space = is_srv ?
504 LUSTRE_SEQ_ZERO_RANGE:
505 LUSTRE_SEQ_SPACE_RANGE;
507 seq->lss_space.lsr_index = ms->ms_node_id;
508 CDEBUG(D_INFO, "%s: No data found "
509 "on store. Initialize space\n",
512 rc = seq_store_update(env, seq, NULL, 0);
514 CERROR("%s: Can't write space data, "
515 "rc %d\n", seq->lss_name, rc);
518 CERROR("%s: Can't read space data, rc %d\n",
524 LASSERT(range_is_sane(&seq->lss_space));
526 LASSERT(!range_is_zero(&seq->lss_space) &&
527 range_is_sane(&seq->lss_space));
530 rc = seq_server_proc_init(seq);
537 seq_server_fini(seq, env);
540 EXPORT_SYMBOL(seq_server_init);
542 void seq_server_fini(struct lu_server_seq *seq,
543 const struct lu_env *env)
547 seq_server_proc_fini(seq);
548 seq_store_fini(seq, env);
552 EXPORT_SYMBOL(seq_server_fini);
554 cfs_proc_dir_entry_t *seq_type_proc_dir = NULL;
556 static struct lu_local_obj_desc llod_seq_srv = {
557 .llod_name = LUSTRE_SEQ_SRV_NAME,
558 .llod_oid = FID_SEQ_SRV_OID,
562 static struct lu_local_obj_desc llod_seq_ctl = {
563 .llod_name = LUSTRE_SEQ_CTL_NAME,
564 .llod_oid = FID_SEQ_CTL_OID,
568 static int __init fid_mod_init(void)
570 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
573 if (IS_ERR(seq_type_proc_dir))
574 return PTR_ERR(seq_type_proc_dir);
576 llo_local_obj_register(&llod_seq_srv);
577 llo_local_obj_register(&llod_seq_ctl);
579 LU_CONTEXT_KEY_INIT(&seq_thread_key);
580 lu_context_key_register(&seq_thread_key);
584 static void __exit fid_mod_exit(void)
586 llo_local_obj_unregister(&llod_seq_srv);
587 llo_local_obj_unregister(&llod_seq_ctl);
589 lu_context_key_degister(&seq_thread_key);
590 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
591 lprocfs_remove(&seq_type_proc_dir);
592 seq_type_proc_dir = NULL;
596 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
597 MODULE_DESCRIPTION("Lustre FID Module");
598 MODULE_LICENSE("GPL");
600 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);