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, 2015, 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 "../../include/linux/libcfs/libcfs.h"
42 #include <linux/module.h>
44 #include "../include/obd.h"
45 #include "../include/obd_class.h"
46 #include "../include/obd_support.h"
47 #include "../include/lustre_fid.h"
49 #include "../include/lustre_mdc.h"
50 #include "fid_internal.h"
52 static struct dentry *seq_debugfs_dir;
54 static int seq_client_rpc(struct lu_client_seq *seq,
55 struct lu_seq_range *output, __u32 opc,
58 struct obd_export *exp = seq->lcs_exp;
59 struct ptlrpc_request *req;
60 struct lu_seq_range *out, *in;
62 unsigned int debug_mask;
65 LASSERT(exp && !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);
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
97 if (seq->lcs_type == LUSTRE_SEQ_DATA) {
99 req->rq_no_resend = 1;
101 debug_mask = D_CONSOLE;
103 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
104 req->rq_reply_portal = MDC_REPLY_PORTAL;
105 req->rq_request_portal = SEQ_METADATA_PORTAL;
107 req->rq_reply_portal = OSC_REPLY_PORTAL;
108 req->rq_request_portal = SEQ_DATA_PORTAL;
113 ptlrpc_at_set_req_timeout(req);
115 if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
116 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
117 rc = ptlrpc_queue_wait(req);
118 if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
119 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
123 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
126 if (!range_is_sane(output)) {
127 CERROR("%s: Invalid range received from server: "
128 DRANGE "\n", seq->lcs_name, PRANGE(output));
133 if (range_is_exhausted(output)) {
134 CERROR("%s: Range received from server is exhausted: "
135 DRANGE "]\n", seq->lcs_name, PRANGE(output));
140 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence " DRANGE "]\n",
141 seq->lcs_name, opcname, PRANGE(output));
144 ptlrpc_req_finished(req);
148 /* Request sequence-controller node to allocate new meta-sequence. */
149 static int seq_client_alloc_meta(const struct lu_env *env,
150 struct lu_client_seq *seq)
155 /* If meta server return -EINPROGRESS or EAGAIN,
156 * it means meta server might not be ready to
157 * allocate super sequence from sequence controller
160 rc = seq_client_rpc(seq, &seq->lcs_space,
161 SEQ_ALLOC_META, "meta");
162 } while (rc == -EINPROGRESS || rc == -EAGAIN);
167 /* Allocate new sequence for client. */
168 static int seq_client_alloc_seq(const struct lu_env *env,
169 struct lu_client_seq *seq, u64 *seqnr)
173 LASSERT(range_is_sane(&seq->lcs_space));
175 if (range_is_exhausted(&seq->lcs_space)) {
176 rc = seq_client_alloc_meta(env, seq);
178 CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
182 CDEBUG(D_INFO, "%s: New range - " DRANGE "\n",
183 seq->lcs_name, PRANGE(&seq->lcs_space));
188 LASSERT(!range_is_exhausted(&seq->lcs_space));
189 *seqnr = seq->lcs_space.lsr_start;
190 seq->lcs_space.lsr_start += 1;
192 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
198 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
201 if (seq->lcs_update) {
202 add_wait_queue(&seq->lcs_waitq, link);
203 set_current_state(TASK_UNINTERRUPTIBLE);
204 mutex_unlock(&seq->lcs_mutex);
208 mutex_lock(&seq->lcs_mutex);
209 remove_wait_queue(&seq->lcs_waitq, link);
210 set_current_state(TASK_RUNNING);
214 mutex_unlock(&seq->lcs_mutex);
218 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
220 LASSERT(seq->lcs_update == 1);
221 mutex_lock(&seq->lcs_mutex);
223 wake_up(&seq->lcs_waitq);
226 /* Allocate new fid on passed client @seq and save it to @fid. */
227 int seq_client_alloc_fid(const struct lu_env *env,
228 struct lu_client_seq *seq, struct lu_fid *fid)
236 init_waitqueue_entry(&link, current);
237 mutex_lock(&seq->lcs_mutex);
239 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
240 seq->lcs_fid.f_oid = seq->lcs_width;
245 if (!fid_is_zero(&seq->lcs_fid) &&
246 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
247 /* Just bump last allocated fid and return to caller. */
248 seq->lcs_fid.f_oid += 1;
253 rc = seq_fid_alloc_prep(seq, &link);
257 rc = seq_client_alloc_seq(env, seq, &seqnr);
259 CERROR("%s: Can't allocate new sequence, rc %d\n",
261 seq_fid_alloc_fini(seq);
262 mutex_unlock(&seq->lcs_mutex);
266 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16Lx]\n",
267 seq->lcs_name, seqnr);
269 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
270 seq->lcs_fid.f_seq = seqnr;
271 seq->lcs_fid.f_ver = 0;
274 * Inform caller that sequence switch is performed to allow it
275 * to setup FLD for it.
279 seq_fid_alloc_fini(seq);
284 mutex_unlock(&seq->lcs_mutex);
286 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
289 EXPORT_SYMBOL(seq_client_alloc_fid);
292 * Finish the current sequence due to disconnect.
293 * See mdc_import_event()
295 void seq_client_flush(struct lu_client_seq *seq)
300 init_waitqueue_entry(&link, current);
301 mutex_lock(&seq->lcs_mutex);
303 while (seq->lcs_update) {
304 add_wait_queue(&seq->lcs_waitq, &link);
305 set_current_state(TASK_UNINTERRUPTIBLE);
306 mutex_unlock(&seq->lcs_mutex);
310 mutex_lock(&seq->lcs_mutex);
311 remove_wait_queue(&seq->lcs_waitq, &link);
312 set_current_state(TASK_RUNNING);
315 fid_zero(&seq->lcs_fid);
317 * this id shld not be used for seq range allocation.
318 * set to -1 for dgb check.
321 seq->lcs_space.lsr_index = -1;
323 range_init(&seq->lcs_space);
324 mutex_unlock(&seq->lcs_mutex);
326 EXPORT_SYMBOL(seq_client_flush);
328 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
330 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
331 ldebugfs_remove(&seq->lcs_debugfs_entry);
334 static int seq_client_debugfs_init(struct lu_client_seq *seq)
338 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
342 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
343 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
344 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
346 seq->lcs_debugfs_entry = NULL;
350 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
351 seq_client_debugfs_list, seq);
353 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
361 seq_client_debugfs_fini(seq);
365 static void seq_client_fini(struct lu_client_seq *seq)
367 seq_client_debugfs_fini(seq);
370 class_export_put(seq->lcs_exp);
375 static int seq_client_init(struct lu_client_seq *seq,
376 struct obd_export *exp,
377 enum lu_cli_type type,
385 seq->lcs_type = type;
387 mutex_init(&seq->lcs_mutex);
388 if (type == LUSTRE_SEQ_METADATA)
389 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
391 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
393 init_waitqueue_head(&seq->lcs_waitq);
394 /* Make sure that things are clear before work is started. */
395 seq_client_flush(seq);
397 seq->lcs_exp = class_export_get(exp);
399 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
402 rc = seq_client_debugfs_init(seq);
404 seq_client_fini(seq);
408 int client_fid_init(struct obd_device *obd,
409 struct obd_export *exp, enum lu_cli_type type)
411 struct client_obd *cli = &obd->u.cli;
415 cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
419 prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
425 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
427 /* Init client side sequence-manager */
428 rc = seq_client_init(cli->cl_seq, exp, type, prefix);
439 EXPORT_SYMBOL(client_fid_init);
441 int client_fid_fini(struct obd_device *obd)
443 struct client_obd *cli = &obd->u.cli;
446 seq_client_fini(cli->cl_seq);
453 EXPORT_SYMBOL(client_fid_fini);
455 static int __init fid_init(void)
457 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
460 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
463 static void __exit fid_exit(void)
465 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
466 ldebugfs_remove(&seq_debugfs_dir);
469 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
470 MODULE_DESCRIPTION("Lustre File IDentifier");
471 MODULE_VERSION(LUSTRE_VERSION_STRING);
472 MODULE_LICENSE("GPL");
474 module_init(fid_init);
475 module_exit(fid_exit);