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_request.c
34 * Lustre Sequence Manager
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FID
41 #include <linux/err.h>
42 #include <linux/module.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre_fid.h>
48 #include <lustre_mdc.h>
49 #include "fid_internal.h"
51 struct dentry *seq_debugfs_dir;
53 static int seq_client_rpc(struct lu_client_seq *seq,
54 struct lu_seq_range *output, __u32 opc,
57 struct obd_export *exp = seq->lcs_exp;
58 struct ptlrpc_request *req;
59 struct lu_seq_range *out, *in;
61 unsigned int debug_mask;
65 LASSERT(exp != NULL && !IS_ERR(exp));
66 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
67 LUSTRE_MDS_VERSION, SEQ_QUERY);
71 /* Init operation code */
72 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
75 /* Zero out input range, this is not recovery yet. */
76 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
77 lu_seq_range_init(in);
79 ptlrpc_request_set_replen(req);
81 in->lsr_index = seq->lcs_space.lsr_index;
82 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
83 fld_range_set_mdt(in);
85 fld_range_set_ost(in);
87 if (opc == SEQ_ALLOC_SUPER) {
88 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
89 req->rq_reply_portal = MDC_REPLY_PORTAL;
90 /* During allocating super sequence for data object,
91 * the current thread might hold the export of MDT0(MDT0
92 * precreating objects on this OST), and it will send the
93 * request to MDT0 here, so we can not keep resending the
94 * request here, otherwise if MDT0 is failed(umounted),
95 * it can not release the export of MDT0 */
96 if (seq->lcs_type == LUSTRE_SEQ_DATA)
97 req->rq_no_delay = req->rq_no_resend = 1;
98 debug_mask = D_CONSOLE;
100 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
101 req->rq_reply_portal = MDC_REPLY_PORTAL;
102 req->rq_request_portal = SEQ_METADATA_PORTAL;
104 req->rq_reply_portal = OSC_REPLY_PORTAL;
105 req->rq_request_portal = SEQ_DATA_PORTAL;
111 /* Allow seq client RPC during recovery time. */
112 req->rq_allow_replay = 1;
114 ptlrpc_at_set_req_timeout(req);
116 rc = ptlrpc_queue_wait(req);
121 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
124 if (!lu_seq_range_is_sane(output)) {
125 CERROR("%s: Invalid range received from server: "
126 DRANGE"\n", seq->lcs_name, PRANGE(output));
127 GOTO(out_req, rc = -EINVAL);
130 if (lu_seq_range_is_exhausted(output)) {
131 CERROR("%s: Range received from server is exhausted: "
132 DRANGE"]\n", seq->lcs_name, PRANGE(output));
133 GOTO(out_req, rc = -EINVAL);
136 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
137 seq->lcs_name, opcname, PRANGE(output));
141 ptlrpc_req_finished(req);
145 /* Request sequence-controller node to allocate new super-sequence. */
146 int seq_client_alloc_super(struct lu_client_seq *seq,
147 const struct lu_env *env)
152 mutex_lock(&seq->lcs_mutex);
155 #ifdef HAVE_SEQ_SERVER
156 LASSERT(env != NULL);
157 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
163 /* Check whether the connection to seq controller has been
164 * setup (lcs_exp != NULL) */
165 if (seq->lcs_exp == NULL) {
166 mutex_unlock(&seq->lcs_mutex);
167 RETURN(-EINPROGRESS);
170 rc = seq_client_rpc(seq, &seq->lcs_space,
171 SEQ_ALLOC_SUPER, "super");
173 mutex_unlock(&seq->lcs_mutex);
177 /* Request sequence-controller node to allocate new meta-sequence. */
178 static int seq_client_alloc_meta(const struct lu_env *env,
179 struct lu_client_seq *seq)
185 #ifdef HAVE_SEQ_SERVER
186 LASSERT(env != NULL);
187 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
193 /* If meta server return -EINPROGRESS or EAGAIN,
194 * it means meta server might not be ready to
195 * allocate super sequence from sequence controller
197 rc = seq_client_rpc(seq, &seq->lcs_space,
198 SEQ_ALLOC_META, "meta");
199 if (rc == -EINPROGRESS || rc == -EAGAIN) {
200 wait_queue_head_t waitq;
201 struct l_wait_info lwi;
203 /* MDT0 is not ready, let's wait for 2
204 * seconds and retry. */
205 init_waitqueue_head(&waitq);
206 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL,
208 l_wait_event(waitq, 0, &lwi);
210 } while (rc == -EINPROGRESS || rc == -EAGAIN);
216 /* Allocate new sequence for client. */
217 static int seq_client_alloc_seq(const struct lu_env *env,
218 struct lu_client_seq *seq, u64 *seqnr)
223 LASSERT(lu_seq_range_is_sane(&seq->lcs_space));
225 if (lu_seq_range_is_exhausted(&seq->lcs_space)) {
226 rc = seq_client_alloc_meta(env, seq);
228 if (rc != -EINPROGRESS)
229 CERROR("%s: Can't allocate new meta-sequence,"
230 "rc = %d\n", seq->lcs_name, rc);
233 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
234 seq->lcs_name, PRANGE(&seq->lcs_space));
240 LASSERT(!lu_seq_range_is_exhausted(&seq->lcs_space));
241 *seqnr = seq->lcs_space.lsr_start;
242 seq->lcs_space.lsr_start += 1;
244 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
250 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
251 wait_queue_entry_t *link)
253 if (seq->lcs_update) {
254 add_wait_queue(&seq->lcs_waitq, link);
255 set_current_state(TASK_UNINTERRUPTIBLE);
256 mutex_unlock(&seq->lcs_mutex);
260 mutex_lock(&seq->lcs_mutex);
261 remove_wait_queue(&seq->lcs_waitq, link);
262 set_current_state(TASK_RUNNING);
267 mutex_unlock(&seq->lcs_mutex);
272 static void seq_fid_alloc_fini(struct lu_client_seq *seq, __u64 seqnr,
275 LASSERT(seq->lcs_update == 1);
277 mutex_lock(&seq->lcs_mutex);
279 CDEBUG(D_INFO, "%s: New sequence [0x%16.16llx]\n",
280 seq->lcs_name, seqnr);
282 seq->lcs_fid.f_seq = seqnr;
284 /* Since the caller require the whole seq,
285 * so marked this seq to be used */
286 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
288 LUSTRE_METADATA_SEQ_MAX_WIDTH;
290 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
292 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
294 seq->lcs_fid.f_ver = 0;
298 wake_up_all(&seq->lcs_waitq);
302 * Allocate the whole non-used seq to the caller.
304 * \param[in] env pointer to the thread context
305 * \param[in,out] seq pointer to the client sequence manager
306 * \param[out] seqnr to hold the new allocated sequence
308 * \retval 0 for new sequence allocated.
309 * \retval Negative error number on failure.
311 int seq_client_get_seq(const struct lu_env *env,
312 struct lu_client_seq *seq, u64 *seqnr)
314 wait_queue_entry_t link;
317 LASSERT(seqnr != NULL);
319 mutex_lock(&seq->lcs_mutex);
320 init_waitqueue_entry(&link, current);
322 /* To guarantee that we can get a whole non-used sequence. */
323 while (seq_fid_alloc_prep(seq, &link) != 0);
325 rc = seq_client_alloc_seq(env, seq, seqnr);
326 seq_fid_alloc_fini(seq, rc ? 0 : *seqnr, true);
328 CERROR("%s: Can't allocate new sequence: rc = %d\n",
330 mutex_unlock(&seq->lcs_mutex);
334 EXPORT_SYMBOL(seq_client_get_seq);
337 * Allocate new fid on passed client @seq and save it to @fid.
339 * \param[in] env pointer to the thread context
340 * \param[in,out] seq pointer to the client sequence manager
341 * \param[out] fid to hold the new allocated fid
343 * \retval 1 for notify the caller that sequence switch
344 * is performed to allow it to setup FLD for it.
345 * \retval 0 for new FID allocated in current sequence.
346 * \retval Negative error number on failure.
348 int seq_client_alloc_fid(const struct lu_env *env,
349 struct lu_client_seq *seq, struct lu_fid *fid)
351 wait_queue_entry_t link;
355 LASSERT(seq != NULL);
356 LASSERT(fid != NULL);
358 init_waitqueue_entry(&link, current);
359 mutex_lock(&seq->lcs_mutex);
361 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
362 seq->lcs_fid.f_oid = seq->lcs_width;
367 if (unlikely(!fid_is_zero(&seq->lcs_fid) &&
368 fid_oid(&seq->lcs_fid) < seq->lcs_width)) {
369 /* Just bump last allocated fid and return to caller. */
370 seq->lcs_fid.f_oid++;
375 /* Release seq::lcs_mutex via seq_fid_alloc_prep() to avoid
376 * deadlock during seq_client_alloc_seq(). */
377 rc = seq_fid_alloc_prep(seq, &link);
381 rc = seq_client_alloc_seq(env, seq, &seqnr);
382 /* Re-take seq::lcs_mutex via seq_fid_alloc_fini(). */
383 seq_fid_alloc_fini(seq, rc ? 0 : seqnr, false);
385 if (rc != -EINPROGRESS)
386 CERROR("%s: Can't allocate new sequence: "
387 "rc = %d\n", seq->lcs_name, rc);
388 mutex_unlock(&seq->lcs_mutex);
398 mutex_unlock(&seq->lcs_mutex);
400 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
404 EXPORT_SYMBOL(seq_client_alloc_fid);
407 * Finish the current sequence due to disconnect.
408 * See mdc_import_event()
410 void seq_client_flush(struct lu_client_seq *seq)
412 wait_queue_entry_t link;
414 LASSERT(seq != NULL);
415 init_waitqueue_entry(&link, current);
416 mutex_lock(&seq->lcs_mutex);
418 while (seq->lcs_update) {
419 add_wait_queue(&seq->lcs_waitq, &link);
420 set_current_state(TASK_UNINTERRUPTIBLE);
421 mutex_unlock(&seq->lcs_mutex);
425 mutex_lock(&seq->lcs_mutex);
426 remove_wait_queue(&seq->lcs_waitq, &link);
427 set_current_state(TASK_RUNNING);
430 fid_zero(&seq->lcs_fid);
432 * this id shld not be used for seq range allocation.
433 * set to -1 for dgb check.
436 seq->lcs_space.lsr_index = -1;
438 lu_seq_range_init(&seq->lcs_space);
439 mutex_unlock(&seq->lcs_mutex);
441 EXPORT_SYMBOL(seq_client_flush);
443 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
445 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
446 ldebugfs_remove(&seq->lcs_debugfs_entry);
449 static int seq_client_debugfs_init(struct lu_client_seq *seq)
453 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
456 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
457 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
458 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
460 seq->lcs_debugfs_entry = NULL;
464 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
465 seq_client_debugfs_list, seq);
467 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
469 GOTO(out_cleanup, rc);
475 seq_client_debugfs_fini(seq);
479 void seq_client_fini(struct lu_client_seq *seq)
483 seq_client_debugfs_fini(seq);
485 if (seq->lcs_exp != NULL) {
486 class_export_put(seq->lcs_exp);
493 EXPORT_SYMBOL(seq_client_fini);
495 int seq_client_init(struct lu_client_seq *seq,
496 struct obd_export *exp,
497 enum lu_cli_type type,
499 struct lu_server_seq *srv)
504 LASSERT(seq != NULL);
505 LASSERT(prefix != NULL);
508 seq->lcs_type = type;
510 mutex_init(&seq->lcs_mutex);
511 if (type == LUSTRE_SEQ_METADATA)
512 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
514 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
516 init_waitqueue_head(&seq->lcs_waitq);
517 /* Make sure that things are clear before work is started. */
518 seq_client_flush(seq);
521 seq->lcs_exp = class_export_get(exp);
523 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
526 rc = seq_client_debugfs_init(seq);
528 seq_client_fini(seq);
531 EXPORT_SYMBOL(seq_client_init);
533 int client_fid_init(struct obd_device *obd,
534 struct obd_export *exp, enum lu_cli_type type)
536 struct client_obd *cli = &obd->u.cli;
541 down_write(&cli->cl_seq_rwsem);
542 OBD_ALLOC_PTR(cli->cl_seq);
544 GOTO(out, rc = -ENOMEM);
546 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
548 GOTO(out, rc = -ENOMEM);
550 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
552 /* Init client side sequence-manager */
553 rc = seq_client_init(cli->cl_seq, exp, type, prefix, NULL);
554 OBD_FREE(prefix, MAX_OBD_NAME + 5);
559 if (rc && cli->cl_seq) {
560 OBD_FREE_PTR(cli->cl_seq);
563 up_write(&cli->cl_seq_rwsem);
567 EXPORT_SYMBOL(client_fid_init);
569 int client_fid_fini(struct obd_device *obd)
571 struct client_obd *cli = &obd->u.cli;
574 down_write(&cli->cl_seq_rwsem);
576 seq_client_fini(cli->cl_seq);
577 OBD_FREE_PTR(cli->cl_seq);
580 up_write(&cli->cl_seq_rwsem);
584 EXPORT_SYMBOL(client_fid_fini);
586 static int __init fid_init(void)
588 #ifdef HAVE_SERVER_SUPPORT
589 int rc = fid_server_mod_init();
594 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
597 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
600 static void __exit fid_exit(void)
602 # ifdef HAVE_SERVER_SUPPORT
603 fid_server_mod_exit();
605 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
606 ldebugfs_remove(&seq_debugfs_dir);
609 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
610 MODULE_DESCRIPTION("Lustre File IDentifier");
611 MODULE_VERSION(LUSTRE_VERSION_STRING);
612 MODULE_LICENSE("GPL");
614 module_init(fid_init);
615 module_exit(fid_exit);