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 (c) 2007, 2010, Oracle and/or its affiliates. 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 <lu_target.h>
58 #include <dt_object.h>
59 #include <md_object.h>
60 #include <obd_support.h>
61 #include <lustre_req_layout.h>
62 #include <lustre_fid.h>
63 #include "fid_internal.h"
66 /* Assigns client to sequence controller node. */
67 int seq_server_set_cli(struct lu_server_seq *seq,
68 struct lu_client_seq *cli,
69 const struct lu_env *env)
75 * Ask client for new range, assign that range to ->seq_space and write
76 * seq state to backing store should be atomic.
78 cfs_down(&seq->lss_sem);
81 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
82 seq->lss_name, cli->lcs_name);
87 if (seq->lss_cli != NULL) {
88 CERROR("%s: Sequence controller is already "
89 "assigned\n", seq->lss_name);
90 GOTO(out_up, rc = -EINVAL);
93 CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
94 seq->lss_name, cli->lcs_name);
97 cli->lcs_space.lsr_index = seq->lss_site->ms_node_id;
100 cfs_up(&seq->lss_sem);
103 EXPORT_SYMBOL(seq_server_set_cli);
105 * allocate \a w units of sequence from range \a from.
107 static inline void range_alloc(struct lu_seq_range *to,
108 struct lu_seq_range *from,
111 width = min(range_space(from), width);
112 to->lsr_start = from->lsr_start;
113 to->lsr_end = from->lsr_start + width;
114 from->lsr_start += width;
118 * On controller node, allocate new super sequence for regular sequence server.
119 * As this super sequence controller, this node suppose to maintain fld
121 * \a out range always has currect mds node number of requester.
124 static int __seq_server_alloc_super(struct lu_server_seq *seq,
125 struct lu_seq_range *out,
126 const struct lu_env *env)
128 struct lu_seq_range *space = &seq->lss_space;
132 LASSERT(range_is_sane(space));
134 if (range_is_exhausted(space)) {
135 CERROR("%s: Sequences space is exhausted\n",
139 range_alloc(out, space, seq->lss_width);
142 rc = seq_store_update(env, seq, out, 1 /* sync */);
144 CDEBUG(D_INFO, "%s: super-sequence allocation rc = %d "
145 DRANGE"\n", seq->lss_name, rc, PRANGE(out));
150 int seq_server_alloc_super(struct lu_server_seq *seq,
151 struct lu_seq_range *out,
152 const struct lu_env *env)
157 cfs_down(&seq->lss_sem);
158 rc = __seq_server_alloc_super(seq, out, env);
159 cfs_up(&seq->lss_sem);
164 static int __seq_set_init(const struct lu_env *env,
165 struct lu_server_seq *seq)
167 struct lu_seq_range *space = &seq->lss_space;
170 range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
171 range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
173 rc = seq_store_update(env, seq, NULL, 1);
179 * This function implements new seq allocation algorithm using async
180 * updates to seq file on disk. ref bug 18857 for details.
181 * there are four variable to keep track of this process
183 * lss_space; - available lss_space
184 * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
185 * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
188 * when lss_lowater_set reaches the end it is replaced with hiwater one and
189 * a write operation is initiated to allocate new hiwater range.
190 * if last seq write opearion is still not commited, current operation is
191 * flaged as sync write op.
193 static int range_alloc_set(const struct lu_env *env,
194 struct lu_seq_range *out,
195 struct lu_server_seq *seq)
197 struct lu_seq_range *space = &seq->lss_space;
198 struct lu_seq_range *loset = &seq->lss_lowater_set;
199 struct lu_seq_range *hiset = &seq->lss_hiwater_set;
202 if (range_is_zero(loset))
203 __seq_set_init(env, seq);
205 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
206 loset->lsr_start = loset->lsr_end;
208 if (range_is_exhausted(loset)) {
209 /* reached high water mark. */
210 struct lu_device *dev = seq->lss_site->ms_lu.ls_top_dev;
211 int obd_num_clients = dev->ld_obd->obd_num_exports;
214 /* calculate new seq width based on number of clients */
215 set_sz = max(seq->lss_set_width,
216 obd_num_clients * seq->lss_width);
217 set_sz = min(range_space(space), set_sz);
219 /* Switch to hiwater range now */
221 /* allocate new hiwater range */
222 range_alloc(hiset, space, set_sz);
224 /* update ondisk seq with new *space */
225 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
228 LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
229 DRANGE"\n", PRANGE(loset));
232 range_alloc(out, loset, seq->lss_width);
237 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
238 struct lu_seq_range *out,
239 const struct lu_env *env)
241 struct lu_seq_range *space = &seq->lss_space;
246 LASSERT(range_is_sane(space));
248 /* Check if available space ends and allocate new super seq */
249 if (range_is_exhausted(space)) {
251 CERROR("%s: No sequence controller is attached.\n",
256 rc = seq_client_alloc_super(seq->lss_cli, env);
258 CERROR("%s: Can't allocate super-sequence, rc %d\n",
263 /* Saving new range to allocation space. */
264 *space = seq->lss_cli->lcs_space;
265 LASSERT(range_is_sane(space));
268 rc = range_alloc_set(env, out, seq);
270 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
271 DRANGE"\n", seq->lss_name, PRANGE(out));
277 int seq_server_alloc_meta(struct lu_server_seq *seq,
278 struct lu_seq_range *out,
279 const struct lu_env *env)
284 cfs_down(&seq->lss_sem);
285 rc = __seq_server_alloc_meta(seq, out, env);
286 cfs_up(&seq->lss_sem);
290 EXPORT_SYMBOL(seq_server_alloc_meta);
292 static int seq_server_handle(struct lu_site *site,
293 const struct lu_env *env,
294 __u32 opc, struct lu_seq_range *out)
297 struct md_site *mite;
300 mite = lu_site2md(site);
303 if (!mite->ms_server_seq) {
304 CERROR("Sequence server is not "
308 rc = seq_server_alloc_meta(mite->ms_server_seq, out, env);
310 case SEQ_ALLOC_SUPER:
311 if (!mite->ms_control_seq) {
312 CERROR("Sequence controller is not "
316 rc = seq_server_alloc_super(mite->ms_control_seq, out, env);
326 static int seq_req_handle(struct ptlrpc_request *req,
327 const struct lu_env *env,
328 struct seq_thread_info *info)
330 struct lu_seq_range *out, *tmp;
331 struct lu_site *site;
336 LASSERT(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY));
337 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
338 LASSERT(site != NULL);
340 rc = req_capsule_server_pack(info->sti_pill);
342 RETURN(err_serious(rc));
344 opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
346 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
348 RETURN(err_serious(-EPROTO));
350 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
352 /* seq client passed mdt id, we need to pass that using out
355 out->lsr_index = tmp->lsr_index;
356 out->lsr_flags = tmp->lsr_flags;
357 rc = seq_server_handle(site, env, *opc, out);
359 rc = err_serious(-EPROTO);
364 /* context key constructor/destructor: seq_key_init, seq_key_fini */
365 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
367 /* context key: seq_thread_key */
368 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
370 static void seq_thread_info_init(struct ptlrpc_request *req,
371 struct seq_thread_info *info)
373 info->sti_pill = &req->rq_pill;
374 /* Init request capsule */
375 req_capsule_init(info->sti_pill, req, RCL_SERVER);
376 req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
379 static void seq_thread_info_fini(struct seq_thread_info *info)
381 req_capsule_fini(info->sti_pill);
384 static int seq_handle(struct ptlrpc_request *req)
386 const struct lu_env *env;
387 struct seq_thread_info *info;
390 env = req->rq_svc_thread->t_env;
391 LASSERT(env != NULL);
393 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
394 LASSERT(info != NULL);
396 seq_thread_info_init(req, info);
397 rc = seq_req_handle(req, env, info);
398 /* XXX: we don't need replay but MDT assign transno in any case,
399 * remove it manually before reply*/
400 lustre_msg_set_transno(req->rq_repmsg, 0);
401 seq_thread_info_fini(info);
407 * Entry point for handling FLD RPCs called from MDT.
409 int seq_query(struct com_thread_info *info)
411 return seq_handle(info->cti_pill->rc_req);
413 EXPORT_SYMBOL(seq_query);
415 static void seq_server_proc_fini(struct lu_server_seq *seq);
418 static int seq_server_proc_init(struct lu_server_seq *seq)
423 seq->lss_proc_dir = lprocfs_register(seq->lss_name,
426 if (IS_ERR(seq->lss_proc_dir)) {
427 rc = PTR_ERR(seq->lss_proc_dir);
431 rc = lprocfs_add_vars(seq->lss_proc_dir,
432 seq_server_proc_list, seq);
434 CERROR("%s: Can't init sequence manager "
435 "proc, rc %d\n", seq->lss_name, rc);
436 GOTO(out_cleanup, rc);
442 seq_server_proc_fini(seq);
446 static void seq_server_proc_fini(struct lu_server_seq *seq)
449 if (seq->lss_proc_dir != NULL) {
450 if (!IS_ERR(seq->lss_proc_dir))
451 lprocfs_remove(&seq->lss_proc_dir);
452 seq->lss_proc_dir = NULL;
457 static int seq_server_proc_init(struct lu_server_seq *seq)
462 static void seq_server_proc_fini(struct lu_server_seq *seq)
469 int seq_server_init(struct lu_server_seq *seq,
470 struct dt_device *dev,
472 enum lu_mgr_type type,
474 const struct lu_env *env)
476 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
479 LASSERT(dev != NULL);
480 LASSERT(prefix != NULL);
483 seq->lss_type = type;
485 range_init(&seq->lss_space);
487 range_init(&seq->lss_lowater_set);
488 range_init(&seq->lss_hiwater_set);
489 seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
491 cfs_sema_init(&seq->lss_sem, 1);
493 seq->lss_width = is_srv ?
494 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
496 snprintf(seq->lss_name, sizeof(seq->lss_name),
497 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
499 rc = seq_store_init(seq, env, dev);
502 /* Request backing store for saved sequence info. */
503 rc = seq_store_read(seq, env);
504 if (rc == -ENODATA) {
506 /* Nothing is read, init by default value. */
507 seq->lss_space = is_srv ?
508 LUSTRE_SEQ_ZERO_RANGE:
509 LUSTRE_SEQ_SPACE_RANGE;
511 seq->lss_space.lsr_index = ms->ms_node_id;
512 CDEBUG(D_INFO, "%s: No data found "
513 "on store. Initialize space\n",
516 rc = seq_store_update(env, seq, NULL, 0);
518 CERROR("%s: Can't write space data, "
519 "rc %d\n", seq->lss_name, rc);
522 CERROR("%s: Can't read space data, rc %d\n",
528 LASSERT(range_is_sane(&seq->lss_space));
530 LASSERT(!range_is_zero(&seq->lss_space) &&
531 range_is_sane(&seq->lss_space));
534 rc = seq_server_proc_init(seq);
541 seq_server_fini(seq, env);
544 EXPORT_SYMBOL(seq_server_init);
546 void seq_server_fini(struct lu_server_seq *seq,
547 const struct lu_env *env)
551 seq_server_proc_fini(seq);
552 seq_store_fini(seq, env);
556 EXPORT_SYMBOL(seq_server_fini);
558 cfs_proc_dir_entry_t *seq_type_proc_dir = NULL;
560 static struct lu_local_obj_desc llod_seq_srv = {
561 .llod_name = LUSTRE_SEQ_SRV_NAME,
562 .llod_oid = FID_SEQ_SRV_OID,
566 static struct lu_local_obj_desc llod_seq_ctl = {
567 .llod_name = LUSTRE_SEQ_CTL_NAME,
568 .llod_oid = FID_SEQ_CTL_OID,
572 static int __init fid_mod_init(void)
574 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
577 if (IS_ERR(seq_type_proc_dir))
578 return PTR_ERR(seq_type_proc_dir);
580 llo_local_obj_register(&llod_seq_srv);
581 llo_local_obj_register(&llod_seq_ctl);
583 LU_CONTEXT_KEY_INIT(&seq_thread_key);
584 lu_context_key_register(&seq_thread_key);
588 static void __exit fid_mod_exit(void)
590 llo_local_obj_unregister(&llod_seq_srv);
591 llo_local_obj_unregister(&llod_seq_ctl);
593 lu_context_key_degister(&seq_thread_key);
594 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
595 lprocfs_remove(&seq_type_proc_dir);
596 seq_type_proc_dir = NULL;
600 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
601 MODULE_DESCRIPTION("Lustre FID Module");
602 MODULE_LICENSE("GPL");
604 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);