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>
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.
77 cfs_mutex_lock(&seq->lss_mutex);
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_index = seq->lss_site->ms_node_id;
99 cfs_mutex_unlock(&seq->lss_mutex);
102 EXPORT_SYMBOL(seq_server_set_cli);
104 * allocate \a w units of sequence from range \a from.
106 static inline void range_alloc(struct lu_seq_range *to,
107 struct lu_seq_range *from,
110 width = min(range_space(from), width);
111 to->lsr_start = from->lsr_start;
112 to->lsr_end = from->lsr_start + width;
113 from->lsr_start += width;
117 * On controller node, allocate new super sequence for regular sequence server.
118 * As this super sequence controller, this node suppose to maintain fld
120 * \a out range always has currect mds node number of requester.
123 static int __seq_server_alloc_super(struct lu_server_seq *seq,
124 struct lu_seq_range *out,
125 const struct lu_env *env)
127 struct lu_seq_range *space = &seq->lss_space;
131 LASSERT(range_is_sane(space));
133 if (range_is_exhausted(space)) {
134 CERROR("%s: Sequences space is exhausted\n",
138 range_alloc(out, space, seq->lss_width);
141 rc = seq_store_update(env, seq, out, 1 /* sync */);
143 CDEBUG(D_INFO, "%s: super-sequence allocation rc = %d "
144 DRANGE"\n", seq->lss_name, rc, PRANGE(out));
149 int seq_server_alloc_super(struct lu_server_seq *seq,
150 struct lu_seq_range *out,
151 const struct lu_env *env)
156 cfs_mutex_lock(&seq->lss_mutex);
157 rc = __seq_server_alloc_super(seq, out, env);
158 cfs_mutex_unlock(&seq->lss_mutex);
163 static int __seq_set_init(const struct lu_env *env,
164 struct lu_server_seq *seq)
166 struct lu_seq_range *space = &seq->lss_space;
169 range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
170 range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
172 rc = seq_store_update(env, seq, NULL, 1);
178 * This function implements new seq allocation algorithm using async
179 * updates to seq file on disk. ref bug 18857 for details.
180 * there are four variable to keep track of this process
182 * lss_space; - available lss_space
183 * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
184 * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
187 * when lss_lowater_set reaches the end it is replaced with hiwater one and
188 * a write operation is initiated to allocate new hiwater range.
189 * if last seq write opearion is still not commited, current operation is
190 * flaged as sync write op.
192 static int range_alloc_set(const struct lu_env *env,
193 struct lu_seq_range *out,
194 struct lu_server_seq *seq)
196 struct lu_seq_range *space = &seq->lss_space;
197 struct lu_seq_range *loset = &seq->lss_lowater_set;
198 struct lu_seq_range *hiset = &seq->lss_hiwater_set;
201 if (range_is_zero(loset))
202 __seq_set_init(env, seq);
204 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
205 loset->lsr_start = loset->lsr_end;
207 if (range_is_exhausted(loset)) {
208 /* reached high water mark. */
209 struct lu_device *dev = seq->lss_site->ms_lu.ls_top_dev;
210 int obd_num_clients = dev->ld_obd->obd_num_exports;
213 /* calculate new seq width based on number of clients */
214 set_sz = max(seq->lss_set_width,
215 obd_num_clients * seq->lss_width);
216 set_sz = min(range_space(space), set_sz);
218 /* Switch to hiwater range now */
220 /* allocate new hiwater range */
221 range_alloc(hiset, space, set_sz);
223 /* update ondisk seq with new *space */
224 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
227 LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
228 DRANGE"\n", PRANGE(loset));
231 range_alloc(out, loset, seq->lss_width);
236 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
237 struct lu_seq_range *out,
238 const struct lu_env *env)
240 struct lu_seq_range *space = &seq->lss_space;
245 LASSERT(range_is_sane(space));
247 /* Check if available space ends and allocate new super seq */
248 if (range_is_exhausted(space)) {
250 CERROR("%s: No sequence controller is attached.\n",
255 rc = seq_client_alloc_super(seq->lss_cli, env);
257 CERROR("%s: Can't allocate super-sequence, rc %d\n",
262 /* Saving new range to allocation space. */
263 *space = seq->lss_cli->lcs_space;
264 LASSERT(range_is_sane(space));
267 rc = range_alloc_set(env, out, seq);
269 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
270 DRANGE"\n", seq->lss_name, PRANGE(out));
276 int seq_server_alloc_meta(struct lu_server_seq *seq,
277 struct lu_seq_range *out,
278 const struct lu_env *env)
283 cfs_mutex_lock(&seq->lss_mutex);
284 rc = __seq_server_alloc_meta(seq, out, env);
285 cfs_mutex_unlock(&seq->lss_mutex);
289 EXPORT_SYMBOL(seq_server_alloc_meta);
291 static int seq_server_handle(struct lu_site *site,
292 const struct lu_env *env,
293 __u32 opc, struct lu_seq_range *out)
296 struct md_site *mite;
299 mite = lu_site2md(site);
302 if (!mite->ms_server_seq) {
303 CERROR("Sequence server is not "
307 rc = seq_server_alloc_meta(mite->ms_server_seq, out, env);
309 case SEQ_ALLOC_SUPER:
310 if (!mite->ms_control_seq) {
311 CERROR("Sequence controller is not "
315 rc = seq_server_alloc_super(mite->ms_control_seq, out, env);
325 static int seq_req_handle(struct ptlrpc_request *req,
326 const struct lu_env *env,
327 struct seq_thread_info *info)
329 struct lu_seq_range *out, *tmp;
330 struct lu_site *site;
335 LASSERT(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY));
336 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
337 LASSERT(site != NULL);
339 rc = req_capsule_server_pack(info->sti_pill);
341 RETURN(err_serious(rc));
343 opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
345 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
347 RETURN(err_serious(-EPROTO));
349 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
351 /* seq client passed mdt id, we need to pass that using out
354 out->lsr_index = tmp->lsr_index;
355 out->lsr_flags = tmp->lsr_flags;
356 rc = seq_server_handle(site, env, *opc, out);
358 rc = err_serious(-EPROTO);
363 /* context key constructor/destructor: seq_key_init, seq_key_fini */
364 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
366 /* context key: seq_thread_key */
367 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
369 static void seq_thread_info_init(struct ptlrpc_request *req,
370 struct seq_thread_info *info)
372 info->sti_pill = &req->rq_pill;
373 /* Init request capsule */
374 req_capsule_init(info->sti_pill, req, RCL_SERVER);
375 req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
378 static void seq_thread_info_fini(struct seq_thread_info *info)
380 req_capsule_fini(info->sti_pill);
383 static int seq_handle(struct ptlrpc_request *req)
385 const struct lu_env *env;
386 struct seq_thread_info *info;
389 env = req->rq_svc_thread->t_env;
390 LASSERT(env != NULL);
392 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
393 LASSERT(info != NULL);
395 seq_thread_info_init(req, info);
396 rc = seq_req_handle(req, env, info);
397 /* XXX: we don't need replay but MDT assign transno in any case,
398 * remove it manually before reply*/
399 lustre_msg_set_transno(req->rq_repmsg, 0);
400 seq_thread_info_fini(info);
406 * Entry point for handling FLD RPCs called from MDT.
408 int seq_query(struct com_thread_info *info)
410 return seq_handle(info->cti_pill->rc_req);
412 EXPORT_SYMBOL(seq_query);
414 static void seq_server_proc_fini(struct lu_server_seq *seq);
417 static int seq_server_proc_init(struct lu_server_seq *seq)
422 seq->lss_proc_dir = lprocfs_register(seq->lss_name,
425 if (IS_ERR(seq->lss_proc_dir)) {
426 rc = PTR_ERR(seq->lss_proc_dir);
430 rc = lprocfs_add_vars(seq->lss_proc_dir,
431 seq_server_proc_list, seq);
433 CERROR("%s: Can't init sequence manager "
434 "proc, rc %d\n", seq->lss_name, rc);
435 GOTO(out_cleanup, rc);
441 seq_server_proc_fini(seq);
445 static void seq_server_proc_fini(struct lu_server_seq *seq)
448 if (seq->lss_proc_dir != NULL) {
449 if (!IS_ERR(seq->lss_proc_dir))
450 lprocfs_remove(&seq->lss_proc_dir);
451 seq->lss_proc_dir = NULL;
456 static int seq_server_proc_init(struct lu_server_seq *seq)
461 static void seq_server_proc_fini(struct lu_server_seq *seq)
468 int seq_server_init(struct lu_server_seq *seq,
469 struct dt_device *dev,
471 enum lu_mgr_type type,
473 const struct lu_env *env)
475 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
478 LASSERT(dev != NULL);
479 LASSERT(prefix != NULL);
482 seq->lss_type = type;
484 range_init(&seq->lss_space);
486 range_init(&seq->lss_lowater_set);
487 range_init(&seq->lss_hiwater_set);
488 seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
490 cfs_mutex_init(&seq->lss_mutex);
492 seq->lss_width = is_srv ?
493 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
495 snprintf(seq->lss_name, sizeof(seq->lss_name),
496 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
498 rc = seq_store_init(seq, env, dev);
501 /* Request backing store for saved sequence info. */
502 rc = seq_store_read(seq, env);
503 if (rc == -ENODATA) {
505 /* Nothing is read, init by default value. */
506 seq->lss_space = is_srv ?
507 LUSTRE_SEQ_ZERO_RANGE:
508 LUSTRE_SEQ_SPACE_RANGE;
510 seq->lss_space.lsr_index = ms->ms_node_id;
511 CDEBUG(D_INFO, "%s: No data found "
512 "on store. Initialize space\n",
515 rc = seq_store_update(env, seq, NULL, 0);
517 CERROR("%s: Can't write space data, "
518 "rc %d\n", seq->lss_name, rc);
521 CERROR("%s: Can't read space data, rc %d\n",
527 LASSERT(range_is_sane(&seq->lss_space));
529 LASSERT(!range_is_zero(&seq->lss_space) &&
530 range_is_sane(&seq->lss_space));
533 rc = seq_server_proc_init(seq);
540 seq_server_fini(seq, env);
543 EXPORT_SYMBOL(seq_server_init);
545 void seq_server_fini(struct lu_server_seq *seq,
546 const struct lu_env *env)
550 seq_server_proc_fini(seq);
551 seq_store_fini(seq, env);
555 EXPORT_SYMBOL(seq_server_fini);
557 cfs_proc_dir_entry_t *seq_type_proc_dir = NULL;
559 static struct lu_local_obj_desc llod_seq_srv = {
560 .llod_name = LUSTRE_SEQ_SRV_NAME,
561 .llod_oid = FID_SEQ_SRV_OID,
565 static struct lu_local_obj_desc llod_seq_ctl = {
566 .llod_name = LUSTRE_SEQ_CTL_NAME,
567 .llod_oid = FID_SEQ_CTL_OID,
571 static int __init fid_mod_init(void)
573 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
576 if (IS_ERR(seq_type_proc_dir))
577 return PTR_ERR(seq_type_proc_dir);
579 llo_local_obj_register(&llod_seq_srv);
580 llo_local_obj_register(&llod_seq_ctl);
582 LU_CONTEXT_KEY_INIT(&seq_thread_key);
583 lu_context_key_register(&seq_thread_key);
587 static void __exit fid_mod_exit(void)
589 llo_local_obj_unregister(&llod_seq_srv);
590 llo_local_obj_unregister(&llod_seq_ctl);
592 lu_context_key_degister(&seq_thread_key);
593 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
594 lprocfs_remove(&seq_type_proc_dir);
595 seq_type_proc_dir = NULL;
599 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
600 MODULE_DESCRIPTION("Lustre FID Module");
601 MODULE_LICENSE("GPL");
603 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);