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/
31 * lustre/fid/fid_handler.c
33 * Lustre Sequence Manager
35 * Author: Yury Umanets <umka@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_FID
40 #include <libcfs/libcfs.h>
41 #include <linux/module.h>
43 #include <obd_class.h>
44 #include <dt_object.h>
45 #include <obd_support.h>
46 #include <lustre_req_layout.h>
47 #include <lustre_fid.h>
48 #include "fid_internal.h"
50 /* Assigns client to sequence controller node. */
51 int seq_server_set_cli(const struct lu_env *env, struct lu_server_seq *seq,
52 struct lu_client_seq *cli)
58 * Ask client for new range, assign that range to ->seq_space and write
59 * seq state to backing store should be atomic.
61 mutex_lock(&seq->lss_mutex);
64 CDEBUG(D_INFO, "%s: Detached sequence client\n", seq->lss_name);
70 CDEBUG(D_HA, "%s: Sequence controller is already assigned\n",
72 GOTO(out_up, rc = -EEXIST);
75 CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
76 seq->lss_name, cli->lcs_name);
79 cli->lcs_space.lsr_index = seq->lss_site->ss_node_id;
82 mutex_unlock(&seq->lss_mutex);
85 EXPORT_SYMBOL(seq_server_set_cli);
87 * allocate \a w units of sequence from range \a from.
89 static inline void range_alloc(struct lu_seq_range *to,
90 struct lu_seq_range *from,
93 width = min(lu_seq_range_space(from), width);
94 to->lsr_start = from->lsr_start;
95 to->lsr_end = from->lsr_start + width;
96 from->lsr_start += width;
100 * On controller node, allocate new super sequence for regular sequence server.
101 * As this super sequence controller, this node suppose to maintain fld
103 * \a out range always has currect mds node number of requester.
106 static int __seq_server_alloc_super(struct lu_server_seq *seq,
107 struct lu_seq_range *out,
108 const struct lu_env *env)
110 struct lu_seq_range *space = &seq->lss_space;
114 LASSERT(lu_seq_range_is_sane(space));
116 if (lu_seq_range_is_exhausted(space)) {
117 CERROR("%s: Sequences space is exhausted\n",
121 range_alloc(out, space, seq->lss_width);
124 rc = seq_store_update(env, seq, out, 1 /* sync */);
126 LCONSOLE_INFO("%s: super-sequence allocation rc = %d " DRANGE"\n",
127 seq->lss_name, rc, PRANGE(out));
132 int seq_server_alloc_super(struct lu_server_seq *seq,
133 struct lu_seq_range *out,
134 const struct lu_env *env)
139 mutex_lock(&seq->lss_mutex);
140 rc = __seq_server_alloc_super(seq, out, env);
141 mutex_unlock(&seq->lss_mutex);
146 int seq_server_alloc_spec(struct lu_server_seq *seq,
147 struct lu_seq_range *spec,
148 const struct lu_env *env)
150 struct lu_seq_range *space = &seq->lss_space;
155 * In some cases (like recovery after a disaster)
156 * we may need to allocate sequences manually
157 * Notice some sequences can be lost if requested
158 * range doesn't start at the beginning of current
159 * free space. Also notice it's not possible now
160 * to allocate sequences out of natural order.
162 if (spec->lsr_start >= spec->lsr_end)
164 if (spec->lsr_flags != LU_SEQ_RANGE_MDT &&
165 spec->lsr_flags != LU_SEQ_RANGE_OST)
168 mutex_lock(&seq->lss_mutex);
169 if (spec->lsr_start >= space->lsr_start) {
170 space->lsr_start = spec->lsr_end;
171 rc = seq_store_update(env, seq, spec, 1 /* sync */);
173 LCONSOLE_INFO("%s: "DRANGE" sequences allocated: rc = %d \n",
174 seq->lss_name, PRANGE(spec), rc);
176 mutex_unlock(&seq->lss_mutex);
181 static int __seq_set_init(const struct lu_env *env,
182 struct lu_server_seq *seq)
184 struct lu_seq_range *space = &seq->lss_space;
187 range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
188 range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
190 rc = seq_store_update(env, seq, NULL, 1);
196 * This function implements new seq allocation algorithm using async
197 * updates to seq file on disk. ref bug 18857 for details.
198 * there are four variable to keep track of this process
200 * lss_space; - available lss_space
201 * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
202 * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
205 * when lss_lowater_set reaches the end it is replaced with hiwater one and
206 * a write operation is initiated to allocate new hiwater range.
207 * if last seq write opearion is still not committed, current operation is
208 * flaged as sync write op.
210 static int range_alloc_set(const struct lu_env *env,
211 struct lu_seq_range *out,
212 struct lu_server_seq *seq)
214 struct lu_seq_range *space = &seq->lss_space;
215 struct lu_seq_range *loset = &seq->lss_lowater_set;
216 struct lu_seq_range *hiset = &seq->lss_hiwater_set;
219 if (lu_seq_range_is_zero(loset))
220 __seq_set_init(env, seq);
222 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
223 loset->lsr_start = loset->lsr_end;
225 if (lu_seq_range_is_exhausted(loset)) {
226 /* reached high water mark. */
227 struct lu_device *dev = seq->lss_site->ss_lu->ls_top_dev;
228 int obd_num_clients = dev->ld_obd->obd_num_exports;
231 /* calculate new seq width based on number of clients */
232 set_sz = max(seq->lss_set_width,
233 obd_num_clients * seq->lss_width);
234 set_sz = min(lu_seq_range_space(space), set_sz);
236 /* Switch to hiwater range now */
238 /* allocate new hiwater range */
239 range_alloc(hiset, space, set_sz);
241 /* update ondisk seq with new *space */
242 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
245 LASSERTF(!lu_seq_range_is_exhausted(loset) ||
246 lu_seq_range_is_sane(loset),
247 DRANGE"\n", PRANGE(loset));
250 range_alloc(out, loset, seq->lss_width);
256 * Check if the sequence server has sequence avaible
258 * Check if the sequence server has sequence avaible, if not, then
259 * allocating super sequence from sequence manager (MDT0).
261 * \param[in] env execution environment
262 * \param[in] seq server sequence
264 * \retval negative errno if allocating new sequence fails
265 * \retval 0 if there is enough sequence or allocating
266 * new sequence succeeds
268 int seq_server_check_and_alloc_super(const struct lu_env *env,
269 struct lu_server_seq *seq)
271 struct lu_seq_range *space = &seq->lss_space;
276 /* Check if available space ends and allocate new super seq */
277 if (lu_seq_range_is_exhausted(space)) {
279 CERROR("%s: No sequence controller is attached.\n",
284 rc = seq_client_alloc_super(seq->lss_cli, env);
287 "%s: Can't allocate super-sequence: rc = %d\n",
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) {
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 to allocate super-sequence: rc = %d\n",
333 CERROR("%s: Allocated super-sequence failed: rc = %d\n",
339 rc = range_alloc_set(env, out, seq);
341 CERROR("%s: Allocated meta-sequence failed: rc = %d\n",
346 CDEBUG(D_INFO, "%s: Allocated meta-sequence " DRANGE"\n",
347 seq->lss_name, PRANGE(out));
352 int seq_server_alloc_meta(struct lu_server_seq *seq,
353 struct lu_seq_range *out,
354 const struct lu_env *env)
359 mutex_lock(&seq->lss_mutex);
360 rc = __seq_server_alloc_meta(seq, out, env);
361 mutex_unlock(&seq->lss_mutex);
365 EXPORT_SYMBOL(seq_server_alloc_meta);
367 static int seq_server_handle(struct lu_site *site,
368 const struct lu_env *env,
369 __u32 opc, struct lu_seq_range *out)
372 struct seq_server_site *ss_site;
373 struct dt_device *dev;
376 ss_site = lu_site2seq(site);
380 if (!ss_site->ss_server_seq) {
382 CERROR("Sequence server is not initialized: rc = %d\n",
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) {
396 CERROR("Sequence controller is not initialized: rc = %d\n",
401 dev = lu2dt_dev(ss_site->ss_control_seq->lss_obj->do_lu.lo_dev);
405 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
415 static int seq_handler(struct tgt_session_info *tsi)
417 struct lu_seq_range *out, *tmp;
418 struct lu_site *site;
424 LASSERT(!(lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY));
425 site = tsi->tsi_exp->exp_obd->obd_lu_dev->ld_site;
426 LASSERT(site != NULL);
428 opc = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_OPC);
430 out = req_capsule_server_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
432 RETURN(err_serious(-EPROTO));
434 tmp = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
437 * seq client passed mdt id, we need to pass that using out
440 out->lsr_index = tmp->lsr_index;
441 out->lsr_flags = tmp->lsr_flags;
442 rc = seq_server_handle(site, tsi->tsi_env, *opc, out);
444 rc = err_serious(-EPROTO);
450 struct tgt_handler seq_handlers[] = {
451 TGT_SEQ_HDL(HAS_REPLY, SEQ_QUERY, seq_handler),
453 EXPORT_SYMBOL(seq_handlers);
455 /* context key constructor/destructor: seq_key_init, seq_key_fini */
456 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
458 /* context key: seq_thread_key */
459 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
461 static void seq_server_debugfs_fini(struct lu_server_seq *seq)
463 debugfs_remove_recursive(seq->lss_debugfs_entry);
466 static void seq_server_debugfs_init(struct lu_server_seq *seq)
470 seq->lss_debugfs_entry = debugfs_create_dir(seq->lss_name,
473 ldebugfs_add_vars(seq->lss_debugfs_entry,
474 seq_server_debugfs_list, seq);
476 if (seq->lss_type == LUSTRE_SEQ_CONTROLLER)
477 debugfs_create_file("fldb", 0644, seq->lss_debugfs_entry,
478 seq, &seq_fld_debugfs_seq_fops);
481 int seq_server_init(const struct lu_env *env,
482 struct lu_server_seq *seq,
483 struct dt_device *dev,
485 enum lu_mgr_type type,
486 struct seq_server_site *ss)
488 int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
491 LASSERT(dev != NULL);
492 LASSERT(prefix != NULL);
494 LASSERT(ss->ss_lu != NULL);
497 * Check all lu_fid fields are converted in fid_cpu_to_le() and friends
498 * and that there is no padding added by compiler to the struct.
503 BUILD_BUG_ON(sizeof(tst) != sizeof(tst.f_seq) +
504 sizeof(tst.f_oid) + sizeof(tst.f_ver));
508 seq->lss_type = type;
510 lu_seq_range_init(&seq->lss_space);
512 lu_seq_range_init(&seq->lss_lowater_set);
513 lu_seq_range_init(&seq->lss_hiwater_set);
514 seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
516 mutex_init(&seq->lss_mutex);
518 seq->lss_width = is_srv ?
519 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
521 snprintf(seq->lss_name, sizeof(seq->lss_name),
522 "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
524 rc = seq_store_init(seq, env, dev);
527 /* Request backing store for saved sequence info. */
528 rc = seq_store_read(seq, env);
529 if (rc == -ENODATA) {
531 /* Nothing is read, init by default value. */
532 seq->lss_space = is_srv ?
533 LUSTRE_SEQ_ZERO_RANGE :
534 LUSTRE_SEQ_SPACE_RANGE;
536 seq->lss_space.lsr_index = ss->ss_node_id;
537 LCONSOLE_INFO("%s: No data found on store. Initialize space: rc = %d\n",
540 rc = seq_store_update(env, seq, NULL, 0);
542 CERROR("%s: Can't write space data: rc = %d\n",
546 CERROR("%s: Can't read space data: rc = %d\n",
552 LASSERT(lu_seq_range_is_sane(&seq->lss_space));
554 LASSERT(!lu_seq_range_is_zero(&seq->lss_space) &&
555 lu_seq_range_is_sane(&seq->lss_space));
558 seq_server_debugfs_init(seq);
563 seq_server_fini(seq, env);
566 EXPORT_SYMBOL(seq_server_init);
568 void seq_server_fini(struct lu_server_seq *seq,
569 const struct lu_env *env)
573 seq_server_debugfs_fini(seq);
574 seq_store_fini(seq, env);
578 EXPORT_SYMBOL(seq_server_fini);
580 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
585 if (ss->ss_server_seq) {
586 seq_server_fini(ss->ss_server_seq, env);
587 OBD_FREE_PTR(ss->ss_server_seq);
588 ss->ss_server_seq = NULL;
591 if (ss->ss_control_seq) {
592 seq_server_fini(ss->ss_control_seq, env);
593 OBD_FREE_PTR(ss->ss_control_seq);
594 ss->ss_control_seq = NULL;
597 if (ss->ss_client_seq) {
598 seq_client_fini(ss->ss_client_seq);
599 OBD_FREE_PTR(ss->ss_client_seq);
600 ss->ss_client_seq = NULL;
605 EXPORT_SYMBOL(seq_site_fini);
607 int fid_server_mod_init(void)
609 LU_CONTEXT_KEY_INIT(&seq_thread_key);
610 return lu_context_key_register(&seq_thread_key);
613 void fid_server_mod_exit(void)
615 lu_context_key_degister(&seq_thread_key);