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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/fid/fid_handler.c
34 * Lustre Sequence Manager
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FID
41 #include <libcfs/libcfs.h>
42 #include <linux/module.h>
44 #include <obd_class.h>
45 #include <dt_object.h>
46 #include <obd_support.h>
47 #include <lustre_req_layout.h>
48 #include <lustre_fid.h>
49 #include "fid_internal.h"
51 /* Assigns client to sequence controller node. */
52 int seq_server_set_cli(const struct lu_env *env, struct lu_server_seq *seq,
53 struct lu_client_seq *cli)
59 * Ask client for new range, assign that range to ->seq_space and write
60 * seq state to backing store should be atomic.
62 mutex_lock(&seq->lss_mutex);
65 CDEBUG(D_INFO, "%s: Detached sequence client\n", seq->lss_name);
70 if (seq->lss_cli != NULL) {
71 CDEBUG(D_HA, "%s: Sequence controller is already "
72 "assigned\n", seq->lss_name);
73 GOTO(out_up, rc = -EEXIST);
76 CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
77 seq->lss_name, cli->lcs_name);
80 cli->lcs_space.lsr_index = seq->lss_site->ss_node_id;
83 mutex_unlock(&seq->lss_mutex);
86 EXPORT_SYMBOL(seq_server_set_cli);
88 * allocate \a w units of sequence from range \a from.
90 static inline void range_alloc(struct lu_seq_range *to,
91 struct lu_seq_range *from,
94 width = min(lu_seq_range_space(from), width);
95 to->lsr_start = from->lsr_start;
96 to->lsr_end = from->lsr_start + width;
97 from->lsr_start += width;
101 * On controller node, allocate new super sequence for regular sequence server.
102 * As this super sequence controller, this node suppose to maintain fld
104 * \a out range always has currect mds node number of requester.
107 static int __seq_server_alloc_super(struct lu_server_seq *seq,
108 struct lu_seq_range *out,
109 const struct lu_env *env)
111 struct lu_seq_range *space = &seq->lss_space;
115 LASSERT(lu_seq_range_is_sane(space));
117 if (lu_seq_range_is_exhausted(space)) {
118 CERROR("%s: Sequences space is exhausted\n",
122 range_alloc(out, space, seq->lss_width);
125 rc = seq_store_update(env, seq, out, 1 /* sync */);
127 LCONSOLE_INFO("%s: super-sequence allocation rc = %d " DRANGE"\n",
128 seq->lss_name, rc, PRANGE(out));
133 int seq_server_alloc_super(struct lu_server_seq *seq,
134 struct lu_seq_range *out,
135 const struct lu_env *env)
140 mutex_lock(&seq->lss_mutex);
141 rc = __seq_server_alloc_super(seq, out, env);
142 mutex_unlock(&seq->lss_mutex);
147 int seq_server_alloc_spec(struct lu_server_seq *seq,
148 struct lu_seq_range *spec,
149 const struct lu_env *env)
151 struct lu_seq_range *space = &seq->lss_space;
156 * In some cases (like recovery after a disaster)
157 * we may need to allocate sequences manually
158 * Notice some sequences can be lost if requested
159 * range doesn't start at the beginning of current
160 * free space. Also notice it's not possible now
161 * to allocate sequences out of natural order.
163 if (spec->lsr_start >= spec->lsr_end)
165 if (spec->lsr_flags != LU_SEQ_RANGE_MDT &&
166 spec->lsr_flags != LU_SEQ_RANGE_OST)
169 mutex_lock(&seq->lss_mutex);
170 if (spec->lsr_start >= space->lsr_start) {
171 space->lsr_start = spec->lsr_end;
172 rc = seq_store_update(env, seq, spec, 1 /* sync */);
174 LCONSOLE_INFO("%s: "DRANGE" sequences allocated: rc = %d \n",
175 seq->lss_name, PRANGE(spec), rc);
177 mutex_unlock(&seq->lss_mutex);
182 static int __seq_set_init(const struct lu_env *env,
183 struct lu_server_seq *seq)
185 struct lu_seq_range *space = &seq->lss_space;
188 range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
189 range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
191 rc = seq_store_update(env, seq, NULL, 1);
197 * This function implements new seq allocation algorithm using async
198 * updates to seq file on disk. ref bug 18857 for details.
199 * there are four variable to keep track of this process
201 * lss_space; - available lss_space
202 * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
203 * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
206 * when lss_lowater_set reaches the end it is replaced with hiwater one and
207 * a write operation is initiated to allocate new hiwater range.
208 * if last seq write opearion is still not committed, current operation is
209 * flaged as sync write op.
211 static int range_alloc_set(const struct lu_env *env,
212 struct lu_seq_range *out,
213 struct lu_server_seq *seq)
215 struct lu_seq_range *space = &seq->lss_space;
216 struct lu_seq_range *loset = &seq->lss_lowater_set;
217 struct lu_seq_range *hiset = &seq->lss_hiwater_set;
220 if (lu_seq_range_is_zero(loset))
221 __seq_set_init(env, seq);
223 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
224 loset->lsr_start = loset->lsr_end;
226 if (lu_seq_range_is_exhausted(loset)) {
227 /* reached high water mark. */
228 struct lu_device *dev = seq->lss_site->ss_lu->ls_top_dev;
229 int obd_num_clients = dev->ld_obd->obd_num_exports;
232 /* calculate new seq width based on number of clients */
233 set_sz = max(seq->lss_set_width,
234 obd_num_clients * seq->lss_width);
235 set_sz = min(lu_seq_range_space(space), set_sz);
237 /* Switch to hiwater range now */
239 /* allocate new hiwater range */
240 range_alloc(hiset, space, set_sz);
242 /* update ondisk seq with new *space */
243 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
246 LASSERTF(!lu_seq_range_is_exhausted(loset) ||
247 lu_seq_range_is_sane(loset),
248 DRANGE"\n", PRANGE(loset));
251 range_alloc(out, loset, seq->lss_width);
257 * Check if the sequence server has sequence avaible
259 * Check if the sequence server has sequence avaible, if not, then
260 * allocating super sequence from sequence manager (MDT0).
262 * \param[in] env execution environment
263 * \param[in] seq server sequence
265 * \retval negative errno if allocating new sequence fails
266 * \retval 0 if there is enough sequence or allocating
267 * new sequence succeeds
269 int seq_server_check_and_alloc_super(const struct lu_env *env,
270 struct lu_server_seq *seq)
272 struct lu_seq_range *space = &seq->lss_space;
277 /* Check if available space ends and allocate new super seq */
278 if (lu_seq_range_is_exhausted(space)) {
280 CERROR("%s: No sequence controller is attached.\n",
285 rc = seq_client_alloc_super(seq->lss_cli, env);
287 CDEBUG(D_HA, "%s: Can't allocate super-sequence:"
288 " rc %d\n", seq->lss_name, rc);
292 /* Saving new range to allocation space. */
293 *space = seq->lss_cli->lcs_space;
294 LASSERT(lu_seq_range_is_sane(space));
295 if (seq->lss_cli->lcs_srv == NULL) {
296 struct lu_server_fld *fld;
298 /* Insert it to the local FLDB */
299 fld = seq->lss_site->ss_server_fld;
300 mutex_lock(&fld->lsf_lock);
301 rc = fld_insert_entry(env, fld, space);
302 mutex_unlock(&fld->lsf_lock);
306 if (lu_seq_range_is_zero(&seq->lss_lowater_set))
307 __seq_set_init(env, seq);
311 EXPORT_SYMBOL(seq_server_check_and_alloc_super);
313 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
314 struct lu_seq_range *out,
315 const struct lu_env *env)
317 struct lu_seq_range *space = &seq->lss_space;
322 LASSERT(lu_seq_range_is_sane(space));
324 rc = seq_server_check_and_alloc_super(env, seq);
326 if (rc == -EINPROGRESS) {
329 if (printed++ % 8 == 0)
330 LCONSOLE_INFO("%s: Waiting to contact MDT0000 "
331 "to allocate super-sequence\n",
334 CERROR("%s: Allocated super-sequence failed: rc = %d\n",
340 rc = range_alloc_set(env, out, seq);
342 CERROR("%s: Allocated meta-sequence failed: rc = %d\n",
347 CDEBUG(D_INFO, "%s: Allocated meta-sequence " DRANGE"\n",
348 seq->lss_name, PRANGE(out));
353 int seq_server_alloc_meta(struct lu_server_seq *seq,
354 struct lu_seq_range *out,
355 const struct lu_env *env)
360 mutex_lock(&seq->lss_mutex);
361 rc = __seq_server_alloc_meta(seq, out, env);
362 mutex_unlock(&seq->lss_mutex);
366 EXPORT_SYMBOL(seq_server_alloc_meta);
368 static int seq_server_handle(struct lu_site *site,
369 const struct lu_env *env,
370 __u32 opc, struct lu_seq_range *out)
373 struct seq_server_site *ss_site;
374 struct dt_device *dev;
377 ss_site = lu_site2seq(site);
381 if (!ss_site->ss_server_seq) {
382 CERROR("Sequence server is not "
387 dev = lu2dt_dev(ss_site->ss_server_seq->lss_obj->do_lu.lo_dev);
391 rc = seq_server_alloc_meta(ss_site->ss_server_seq, out, env);
393 case SEQ_ALLOC_SUPER:
394 if (!ss_site->ss_control_seq) {
395 CERROR("Sequence controller is not "
400 dev = lu2dt_dev(ss_site->ss_control_seq->lss_obj->do_lu.lo_dev);
404 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
414 static int seq_handler(struct tgt_session_info *tsi)
416 struct lu_seq_range *out, *tmp;
417 struct lu_site *site;
423 LASSERT(!(lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY));
424 site = tsi->tsi_exp->exp_obd->obd_lu_dev->ld_site;
425 LASSERT(site != NULL);
427 opc = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_OPC);
429 out = req_capsule_server_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
431 RETURN(err_serious(-EPROTO));
433 tmp = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
435 /* seq client passed mdt id, we need to pass that using out
438 out->lsr_index = tmp->lsr_index;
439 out->lsr_flags = tmp->lsr_flags;
440 rc = seq_server_handle(site, tsi->tsi_env, *opc, out);
442 rc = err_serious(-EPROTO);
448 struct tgt_handler seq_handlers[] = {
449 TGT_SEQ_HDL(HAS_REPLY, SEQ_QUERY, seq_handler),
451 EXPORT_SYMBOL(seq_handlers);
453 /* context key constructor/destructor: seq_key_init, seq_key_fini */
454 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
456 /* context key: seq_thread_key */
457 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
459 extern const struct file_operations seq_fld_debugfs_seq_fops;
461 static void seq_server_debugfs_fini(struct lu_server_seq *seq)
463 if (!IS_ERR_OR_NULL(seq->lss_debugfs_entry))
464 ldebugfs_remove(&seq->lss_debugfs_entry);
467 static int seq_server_debugfs_init(struct lu_server_seq *seq)
472 seq->lss_debugfs_entry = ldebugfs_register(seq->lss_name,
475 if (IS_ERR_OR_NULL(seq->lss_debugfs_entry)) {
476 rc = seq->lss_debugfs_entry ? PTR_ERR(seq->lss_debugfs_entry)
478 seq->lss_debugfs_entry = NULL;
482 rc = ldebugfs_add_vars(seq->lss_debugfs_entry,
483 seq_server_debugfs_list, seq);
485 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
487 GOTO(out_cleanup, rc);
490 if (seq->lss_type == LUSTRE_SEQ_CONTROLLER) {
491 rc = ldebugfs_seq_create(seq->lss_debugfs_entry, "fldb", 0644,
492 &seq_fld_debugfs_seq_fops, seq);
494 CERROR("%s: Can't create fldb for sequence manager debugfs: rc = %d\n",
496 GOTO(out_cleanup, rc);
503 seq_server_debugfs_fini(seq);
507 int seq_server_init(const struct lu_env *env,
508 struct lu_server_seq *seq,
509 struct dt_device *dev,
511 enum lu_mgr_type type,
512 struct seq_server_site *ss)
514 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
517 LASSERT(dev != NULL);
518 LASSERT(prefix != NULL);
520 LASSERT(ss->ss_lu != NULL);
522 /* A compile-time check for FIDs that used to be in lustre_idl.h
523 * but is moved here to remove CLASSERT/LASSERT in that header.
524 * Check all lu_fid fields are converted in fid_cpu_to_le() and friends
525 * and that there is no padding added by compiler to the struct. */
529 CLASSERT(sizeof(tst) == sizeof(tst.f_seq) +
530 sizeof(tst.f_oid) + sizeof(tst.f_ver));
534 seq->lss_type = type;
536 lu_seq_range_init(&seq->lss_space);
538 lu_seq_range_init(&seq->lss_lowater_set);
539 lu_seq_range_init(&seq->lss_hiwater_set);
540 seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
542 mutex_init(&seq->lss_mutex);
544 seq->lss_width = is_srv ?
545 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
547 snprintf(seq->lss_name, sizeof(seq->lss_name),
548 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
550 rc = seq_store_init(seq, env, dev);
553 /* Request backing store for saved sequence info. */
554 rc = seq_store_read(seq, env);
555 if (rc == -ENODATA) {
557 /* Nothing is read, init by default value. */
558 seq->lss_space = is_srv ?
559 LUSTRE_SEQ_ZERO_RANGE:
560 LUSTRE_SEQ_SPACE_RANGE;
562 seq->lss_space.lsr_index = ss->ss_node_id;
563 LCONSOLE_INFO("%s: No data found "
564 "on store. Initialize space\n",
567 rc = seq_store_update(env, seq, NULL, 0);
569 CERROR("%s: Can't write space data, "
570 "rc %d\n", seq->lss_name, rc);
573 CERROR("%s: Can't read space data, rc %d\n",
579 LASSERT(lu_seq_range_is_sane(&seq->lss_space));
581 LASSERT(!lu_seq_range_is_zero(&seq->lss_space) &&
582 lu_seq_range_is_sane(&seq->lss_space));
585 rc = seq_server_debugfs_init(seq);
592 seq_server_fini(seq, env);
595 EXPORT_SYMBOL(seq_server_init);
597 void seq_server_fini(struct lu_server_seq *seq,
598 const struct lu_env *env)
602 seq_server_debugfs_fini(seq);
603 seq_store_fini(seq, env);
607 EXPORT_SYMBOL(seq_server_fini);
609 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
614 if (ss->ss_server_seq) {
615 seq_server_fini(ss->ss_server_seq, env);
616 OBD_FREE_PTR(ss->ss_server_seq);
617 ss->ss_server_seq = NULL;
620 if (ss->ss_control_seq) {
621 seq_server_fini(ss->ss_control_seq, env);
622 OBD_FREE_PTR(ss->ss_control_seq);
623 ss->ss_control_seq = NULL;
626 if (ss->ss_client_seq) {
627 seq_client_fini(ss->ss_client_seq);
628 OBD_FREE_PTR(ss->ss_client_seq);
629 ss->ss_client_seq = NULL;
634 EXPORT_SYMBOL(seq_site_fini);
636 int fid_server_mod_init(void)
638 LU_CONTEXT_KEY_INIT(&seq_thread_key);
639 return lu_context_key_register(&seq_thread_key);
642 void fid_server_mod_exit(void)
644 lu_context_key_degister(&seq_thread_key);