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, 2016, 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/module.h>
42 #include <libcfs/libcfs.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 static int seq_client_rpc(struct lu_client_seq *seq,
52 struct lu_seq_range *output, __u32 opc,
55 struct obd_export *exp = seq->lcs_exp;
56 struct ptlrpc_request *req;
57 struct lu_seq_range *out, *in;
59 unsigned int debug_mask;
63 LASSERT(exp != NULL && !IS_ERR(exp));
64 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
65 LUSTRE_MDS_VERSION, SEQ_QUERY);
69 /* Init operation code */
70 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
73 /* Zero out input range, this is not recovery yet. */
74 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
75 lu_seq_range_init(in);
77 ptlrpc_request_set_replen(req);
79 in->lsr_index = seq->lcs_space.lsr_index;
80 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
81 fld_range_set_mdt(in);
83 fld_range_set_ost(in);
85 if (opc == SEQ_ALLOC_SUPER) {
86 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
87 req->rq_reply_portal = MDC_REPLY_PORTAL;
88 /* During allocating super sequence for data object,
89 * the current thread might hold the export of MDT0(MDT0
90 * precreating objects on this OST), and it will send the
91 * request to MDT0 here, so we can not keep resending the
92 * request here, otherwise if MDT0 is failed(umounted),
93 * it can not release the export of MDT0 */
94 if (seq->lcs_type == LUSTRE_SEQ_DATA)
95 req->rq_no_delay = req->rq_no_resend = 1;
96 debug_mask = D_CONSOLE;
98 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
99 req->rq_reply_portal = MDC_REPLY_PORTAL;
100 req->rq_request_portal = SEQ_METADATA_PORTAL;
102 req->rq_reply_portal = OSC_REPLY_PORTAL;
103 req->rq_request_portal = SEQ_DATA_PORTAL;
109 /* Allow seq client RPC during recovery time. */
110 req->rq_allow_replay = 1;
112 ptlrpc_at_set_req_timeout(req);
114 rc = ptlrpc_queue_wait(req);
119 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
122 if (!lu_seq_range_is_sane(output)) {
123 CERROR("%s: Invalid range received from server: "
124 DRANGE"\n", seq->lcs_name, PRANGE(output));
125 GOTO(out_req, rc = -EINVAL);
128 if (lu_seq_range_is_exhausted(output)) {
129 CERROR("%s: Range received from server is exhausted: "
130 DRANGE"]\n", seq->lcs_name, PRANGE(output));
131 GOTO(out_req, rc = -EINVAL);
134 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
135 seq->lcs_name, opcname, PRANGE(output));
139 ptlrpc_req_finished(req);
143 /* Request sequence-controller node to allocate new super-sequence. */
144 int seq_client_alloc_super(struct lu_client_seq *seq,
145 const struct lu_env *env)
150 mutex_lock(&seq->lcs_mutex);
153 #ifdef HAVE_SEQ_SERVER
154 LASSERT(env != NULL);
155 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
161 /* Check whether the connection to seq controller has been
162 * setup (lcs_exp != NULL) */
163 if (seq->lcs_exp == NULL) {
164 mutex_unlock(&seq->lcs_mutex);
165 RETURN(-EINPROGRESS);
168 rc = seq_client_rpc(seq, &seq->lcs_space,
169 SEQ_ALLOC_SUPER, "super");
171 mutex_unlock(&seq->lcs_mutex);
175 /* Request sequence-controller node to allocate new meta-sequence. */
176 static int seq_client_alloc_meta(const struct lu_env *env,
177 struct lu_client_seq *seq)
183 #ifdef HAVE_SEQ_SERVER
184 LASSERT(env != NULL);
185 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
191 /* If meta server return -EINPROGRESS or EAGAIN,
192 * it means meta server might not be ready to
193 * allocate super sequence from sequence controller
195 rc = seq_client_rpc(seq, &seq->lcs_space,
196 SEQ_ALLOC_META, "meta");
197 if (rc == -EINPROGRESS || rc == -EAGAIN) {
198 wait_queue_head_t waitq;
199 struct l_wait_info lwi;
201 /* MDT0 is not ready, let's wait for 2
202 * seconds and retry. */
203 init_waitqueue_head(&waitq);
204 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL,
206 l_wait_event(waitq, 0, &lwi);
208 } while (rc == -EINPROGRESS || rc == -EAGAIN);
214 /* Allocate new sequence for client. */
215 static int seq_client_alloc_seq(const struct lu_env *env,
216 struct lu_client_seq *seq, u64 *seqnr)
221 LASSERT(lu_seq_range_is_sane(&seq->lcs_space));
223 if (lu_seq_range_is_exhausted(&seq->lcs_space)) {
224 rc = seq_client_alloc_meta(env, seq);
226 CERROR("%s: Can't allocate new meta-sequence,"
227 "rc %d\n", seq->lcs_name, rc);
230 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
231 seq->lcs_name, PRANGE(&seq->lcs_space));
237 LASSERT(!lu_seq_range_is_exhausted(&seq->lcs_space));
238 *seqnr = seq->lcs_space.lsr_start;
239 seq->lcs_space.lsr_start += 1;
241 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
247 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
250 if (seq->lcs_update) {
251 add_wait_queue(&seq->lcs_waitq, link);
252 set_current_state(TASK_UNINTERRUPTIBLE);
253 mutex_unlock(&seq->lcs_mutex);
257 mutex_lock(&seq->lcs_mutex);
258 remove_wait_queue(&seq->lcs_waitq, link);
259 set_current_state(TASK_RUNNING);
264 mutex_unlock(&seq->lcs_mutex);
269 static void seq_fid_alloc_fini(struct lu_client_seq *seq, __u64 seqnr,
272 LASSERT(seq->lcs_update == 1);
274 mutex_lock(&seq->lcs_mutex);
276 CDEBUG(D_INFO, "%s: New sequence [0x%16.16llx]\n",
277 seq->lcs_name, seqnr);
279 seq->lcs_fid.f_seq = seqnr;
281 /* Since the caller require the whole seq,
282 * so marked this seq to be used */
283 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
285 LUSTRE_METADATA_SEQ_MAX_WIDTH;
287 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
289 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
291 seq->lcs_fid.f_ver = 0;
295 wake_up_all(&seq->lcs_waitq);
299 * Allocate the whole non-used seq to the caller.
301 * \param[in] env pointer to the thread context
302 * \param[in,out] seq pointer to the client sequence manager
303 * \param[out] seqnr to hold the new allocated sequence
305 * \retval 0 for new sequence allocated.
306 * \retval Negative error number on failure.
308 int seq_client_get_seq(const struct lu_env *env,
309 struct lu_client_seq *seq, u64 *seqnr)
314 LASSERT(seqnr != NULL);
316 mutex_lock(&seq->lcs_mutex);
317 init_waitqueue_entry(&link, current);
319 /* To guarantee that we can get a whole non-used sequence. */
320 while (seq_fid_alloc_prep(seq, &link) != 0);
322 rc = seq_client_alloc_seq(env, seq, seqnr);
323 seq_fid_alloc_fini(seq, rc ? 0 : *seqnr, true);
325 CERROR("%s: Can't allocate new sequence: rc = %d\n",
327 mutex_unlock(&seq->lcs_mutex);
331 EXPORT_SYMBOL(seq_client_get_seq);
334 * Allocate new fid on passed client @seq and save it to @fid.
336 * \param[in] env pointer to the thread context
337 * \param[in,out] seq pointer to the client sequence manager
338 * \param[out] fid to hold the new allocated fid
340 * \retval 1 for notify the caller that sequence switch
341 * is performed to allow it to setup FLD for it.
342 * \retval 0 for new FID allocated in current sequence.
343 * \retval Negative error number on failure.
345 int seq_client_alloc_fid(const struct lu_env *env,
346 struct lu_client_seq *seq, struct lu_fid *fid)
352 LASSERT(seq != NULL);
353 LASSERT(fid != NULL);
355 init_waitqueue_entry(&link, current);
356 mutex_lock(&seq->lcs_mutex);
358 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
359 seq->lcs_fid.f_oid = seq->lcs_width;
364 if (unlikely(!fid_is_zero(&seq->lcs_fid) &&
365 fid_oid(&seq->lcs_fid) < seq->lcs_width)) {
366 /* Just bump last allocated fid and return to caller. */
367 seq->lcs_fid.f_oid++;
372 /* Release seq::lcs_mutex via seq_fid_alloc_prep() to avoid
373 * deadlock during seq_client_alloc_seq(). */
374 rc = seq_fid_alloc_prep(seq, &link);
378 rc = seq_client_alloc_seq(env, seq, &seqnr);
379 /* Re-take seq::lcs_mutex via seq_fid_alloc_fini(). */
380 seq_fid_alloc_fini(seq, rc ? 0 : seqnr, false);
382 CERROR("%s: Can't allocate new sequence: rc = %d\n",
384 mutex_unlock(&seq->lcs_mutex);
394 mutex_unlock(&seq->lcs_mutex);
396 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
400 EXPORT_SYMBOL(seq_client_alloc_fid);
403 * Finish the current sequence due to disconnect.
404 * See mdc_import_event()
406 void seq_client_flush(struct lu_client_seq *seq)
410 LASSERT(seq != NULL);
411 init_waitqueue_entry(&link, current);
412 mutex_lock(&seq->lcs_mutex);
414 while (seq->lcs_update) {
415 add_wait_queue(&seq->lcs_waitq, &link);
416 set_current_state(TASK_UNINTERRUPTIBLE);
417 mutex_unlock(&seq->lcs_mutex);
421 mutex_lock(&seq->lcs_mutex);
422 remove_wait_queue(&seq->lcs_waitq, &link);
423 set_current_state(TASK_RUNNING);
426 fid_zero(&seq->lcs_fid);
428 * this id shld not be used for seq range allocation.
429 * set to -1 for dgb check.
432 seq->lcs_space.lsr_index = -1;
434 lu_seq_range_init(&seq->lcs_space);
435 mutex_unlock(&seq->lcs_mutex);
437 EXPORT_SYMBOL(seq_client_flush);
439 static void seq_client_proc_fini(struct lu_client_seq *seq)
441 #ifdef CONFIG_PROC_FS
443 if (seq->lcs_proc_dir) {
444 if (!IS_ERR(seq->lcs_proc_dir))
445 lprocfs_remove(&seq->lcs_proc_dir);
446 seq->lcs_proc_dir = NULL;
449 #endif /* CONFIG_PROC_FS */
452 static int seq_client_proc_init(struct lu_client_seq *seq)
454 #ifdef CONFIG_PROC_FS
458 seq->lcs_proc_dir = lprocfs_register(seq->lcs_name, seq_type_proc_dir,
460 if (IS_ERR(seq->lcs_proc_dir)) {
461 CERROR("%s: LProcFS failed in seq-init\n",
463 rc = PTR_ERR(seq->lcs_proc_dir);
467 rc = lprocfs_add_vars(seq->lcs_proc_dir, seq_client_proc_list, seq);
469 CERROR("%s: Can't init sequence manager "
470 "proc, rc %d\n", seq->lcs_name, rc);
471 GOTO(out_cleanup, rc);
477 seq_client_proc_fini(seq);
480 #else /* !CONFIG_PROC_FS */
482 #endif /* CONFIG_PROC_FS */
485 int seq_client_init(struct lu_client_seq *seq,
486 struct obd_export *exp,
487 enum lu_cli_type type,
489 struct lu_server_seq *srv)
494 LASSERT(seq != NULL);
495 LASSERT(prefix != NULL);
498 seq->lcs_type = type;
500 mutex_init(&seq->lcs_mutex);
501 if (type == LUSTRE_SEQ_METADATA)
502 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
504 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
506 init_waitqueue_head(&seq->lcs_waitq);
507 /* Make sure that things are clear before work is started. */
508 seq_client_flush(seq);
511 seq->lcs_exp = class_export_get(exp);
513 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
516 rc = seq_client_proc_init(seq);
518 seq_client_fini(seq);
521 EXPORT_SYMBOL(seq_client_init);
523 void seq_client_fini(struct lu_client_seq *seq)
527 seq_client_proc_fini(seq);
529 if (seq->lcs_exp != NULL) {
530 class_export_put(seq->lcs_exp);
537 EXPORT_SYMBOL(seq_client_fini);
539 int client_fid_init(struct obd_device *obd,
540 struct obd_export *exp, enum lu_cli_type type)
542 struct client_obd *cli = &obd->u.cli;
547 down_write(&cli->cl_seq_rwsem);
548 OBD_ALLOC_PTR(cli->cl_seq);
550 GOTO(out, rc = -ENOMEM);
552 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
554 GOTO(out, rc = -ENOMEM);
556 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
558 /* Init client side sequence-manager */
559 rc = seq_client_init(cli->cl_seq, exp, type, prefix, NULL);
560 OBD_FREE(prefix, MAX_OBD_NAME + 5);
565 if (rc && cli->cl_seq) {
566 OBD_FREE_PTR(cli->cl_seq);
569 up_write(&cli->cl_seq_rwsem);
573 EXPORT_SYMBOL(client_fid_init);
575 int client_fid_fini(struct obd_device *obd)
577 struct client_obd *cli = &obd->u.cli;
580 down_write(&cli->cl_seq_rwsem);
582 seq_client_fini(cli->cl_seq);
583 OBD_FREE_PTR(cli->cl_seq);
586 up_write(&cli->cl_seq_rwsem);
590 EXPORT_SYMBOL(client_fid_fini);
592 struct proc_dir_entry *seq_type_proc_dir;
594 static int __init fid_init(void)
596 seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
599 if (IS_ERR(seq_type_proc_dir))
600 return PTR_ERR(seq_type_proc_dir);
602 # ifdef HAVE_SERVER_SUPPORT
603 fid_server_mod_init();
609 static void __exit fid_exit(void)
611 # ifdef HAVE_SERVER_SUPPORT
612 fid_server_mod_exit();
615 if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
616 lprocfs_remove(&seq_type_proc_dir);
617 seq_type_proc_dir = NULL;
621 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
622 MODULE_DESCRIPTION("Lustre File IDentifier");
623 MODULE_VERSION(LUSTRE_VERSION_STRING);
624 MODULE_LICENSE("GPL");
626 module_init(fid_init);
627 module_exit(fid_exit);