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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fid/fid_request.c
38 * Lustre Sequence Manager
40 * Author: Yury Umanets <umka@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_FID
45 #include "../../include/linux/libcfs/libcfs.h"
46 #include <linux/module.h>
48 #include "../include/obd.h"
49 #include "../include/obd_class.h"
50 #include "../include/obd_support.h"
51 #include "../include/lustre_fid.h"
53 #include "../include/lustre_mdc.h"
54 #include "fid_internal.h"
56 static struct dentry *seq_debugfs_dir;
58 static int seq_client_rpc(struct lu_client_seq *seq,
59 struct lu_seq_range *output, __u32 opc,
62 struct obd_export *exp = seq->lcs_exp;
63 struct ptlrpc_request *req;
64 struct lu_seq_range *out, *in;
66 unsigned int debug_mask;
69 LASSERT(exp && !IS_ERR(exp));
70 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
71 LUSTRE_MDS_VERSION, SEQ_QUERY);
75 /* Init operation code */
76 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
79 /* Zero out input range, this is not recovery yet. */
80 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
83 ptlrpc_request_set_replen(req);
85 in->lsr_index = seq->lcs_space.lsr_index;
86 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
87 fld_range_set_mdt(in);
89 fld_range_set_ost(in);
91 if (opc == SEQ_ALLOC_SUPER) {
92 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
93 req->rq_reply_portal = MDC_REPLY_PORTAL;
94 /* During allocating super sequence for data object,
95 * the current thread might hold the export of MDT0(MDT0
96 * precreating objects on this OST), and it will send the
97 * request to MDT0 here, so we can not keep resending the
98 * request here, otherwise if MDT0 is failed(umounted),
99 * it can not release the export of MDT0
101 if (seq->lcs_type == LUSTRE_SEQ_DATA)
102 req->rq_no_delay = req->rq_no_resend = 1;
103 debug_mask = D_CONSOLE;
105 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
106 req->rq_reply_portal = MDC_REPLY_PORTAL;
107 req->rq_request_portal = SEQ_METADATA_PORTAL;
109 req->rq_reply_portal = OSC_REPLY_PORTAL;
110 req->rq_request_portal = SEQ_DATA_PORTAL;
115 ptlrpc_at_set_req_timeout(req);
117 if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
118 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
119 rc = ptlrpc_queue_wait(req);
120 if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
121 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
125 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
128 if (!range_is_sane(output)) {
129 CERROR("%s: Invalid range received from server: "
130 DRANGE"\n", seq->lcs_name, PRANGE(output));
135 if (range_is_exhausted(output)) {
136 CERROR("%s: Range received from server is exhausted: "
137 DRANGE"]\n", seq->lcs_name, PRANGE(output));
142 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
143 seq->lcs_name, opcname, PRANGE(output));
146 ptlrpc_req_finished(req);
150 /* Request sequence-controller node to allocate new meta-sequence. */
151 static int seq_client_alloc_meta(const struct lu_env *env,
152 struct lu_client_seq *seq)
157 /* If meta server return -EINPROGRESS or EAGAIN,
158 * it means meta server might not be ready to
159 * allocate super sequence from sequence controller
162 rc = seq_client_rpc(seq, &seq->lcs_space,
163 SEQ_ALLOC_META, "meta");
164 } while (rc == -EINPROGRESS || rc == -EAGAIN);
169 /* Allocate new sequence for client. */
170 static int seq_client_alloc_seq(const struct lu_env *env,
171 struct lu_client_seq *seq, u64 *seqnr)
175 LASSERT(range_is_sane(&seq->lcs_space));
177 if (range_is_exhausted(&seq->lcs_space)) {
178 rc = seq_client_alloc_meta(env, seq);
180 CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
184 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
185 seq->lcs_name, PRANGE(&seq->lcs_space));
190 LASSERT(!range_is_exhausted(&seq->lcs_space));
191 *seqnr = seq->lcs_space.lsr_start;
192 seq->lcs_space.lsr_start += 1;
194 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
200 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
203 if (seq->lcs_update) {
204 add_wait_queue(&seq->lcs_waitq, link);
205 set_current_state(TASK_UNINTERRUPTIBLE);
206 mutex_unlock(&seq->lcs_mutex);
210 mutex_lock(&seq->lcs_mutex);
211 remove_wait_queue(&seq->lcs_waitq, link);
212 set_current_state(TASK_RUNNING);
216 mutex_unlock(&seq->lcs_mutex);
220 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
222 LASSERT(seq->lcs_update == 1);
223 mutex_lock(&seq->lcs_mutex);
225 wake_up(&seq->lcs_waitq);
228 /* Allocate new fid on passed client @seq and save it to @fid. */
229 int seq_client_alloc_fid(const struct lu_env *env,
230 struct lu_client_seq *seq, struct lu_fid *fid)
238 init_waitqueue_entry(&link, current);
239 mutex_lock(&seq->lcs_mutex);
241 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
242 seq->lcs_fid.f_oid = seq->lcs_width;
247 if (!fid_is_zero(&seq->lcs_fid) &&
248 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
249 /* Just bump last allocated fid and return to caller. */
250 seq->lcs_fid.f_oid += 1;
255 rc = seq_fid_alloc_prep(seq, &link);
259 rc = seq_client_alloc_seq(env, seq, &seqnr);
261 CERROR("%s: Can't allocate new sequence, rc %d\n",
263 seq_fid_alloc_fini(seq);
264 mutex_unlock(&seq->lcs_mutex);
268 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16Lx]\n",
269 seq->lcs_name, seqnr);
271 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
272 seq->lcs_fid.f_seq = seqnr;
273 seq->lcs_fid.f_ver = 0;
276 * Inform caller that sequence switch is performed to allow it
277 * to setup FLD for it.
281 seq_fid_alloc_fini(seq);
286 mutex_unlock(&seq->lcs_mutex);
288 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
291 EXPORT_SYMBOL(seq_client_alloc_fid);
294 * Finish the current sequence due to disconnect.
295 * See mdc_import_event()
297 void seq_client_flush(struct lu_client_seq *seq)
302 init_waitqueue_entry(&link, current);
303 mutex_lock(&seq->lcs_mutex);
305 while (seq->lcs_update) {
306 add_wait_queue(&seq->lcs_waitq, &link);
307 set_current_state(TASK_UNINTERRUPTIBLE);
308 mutex_unlock(&seq->lcs_mutex);
312 mutex_lock(&seq->lcs_mutex);
313 remove_wait_queue(&seq->lcs_waitq, &link);
314 set_current_state(TASK_RUNNING);
317 fid_zero(&seq->lcs_fid);
319 * this id shld not be used for seq range allocation.
320 * set to -1 for dgb check.
323 seq->lcs_space.lsr_index = -1;
325 range_init(&seq->lcs_space);
326 mutex_unlock(&seq->lcs_mutex);
328 EXPORT_SYMBOL(seq_client_flush);
330 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
332 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
333 ldebugfs_remove(&seq->lcs_debugfs_entry);
336 static int seq_client_debugfs_init(struct lu_client_seq *seq)
340 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
344 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
345 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
346 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
348 seq->lcs_debugfs_entry = NULL;
352 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
353 seq_client_debugfs_list, seq);
355 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
363 seq_client_debugfs_fini(seq);
367 static void seq_client_fini(struct lu_client_seq *seq)
369 seq_client_debugfs_fini(seq);
372 class_export_put(seq->lcs_exp);
377 static int seq_client_init(struct lu_client_seq *seq,
378 struct obd_export *exp,
379 enum lu_cli_type type,
387 seq->lcs_type = type;
389 mutex_init(&seq->lcs_mutex);
390 if (type == LUSTRE_SEQ_METADATA)
391 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
393 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
395 init_waitqueue_head(&seq->lcs_waitq);
396 /* Make sure that things are clear before work is started. */
397 seq_client_flush(seq);
399 seq->lcs_exp = class_export_get(exp);
401 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
404 rc = seq_client_debugfs_init(seq);
406 seq_client_fini(seq);
410 int client_fid_init(struct obd_device *obd,
411 struct obd_export *exp, enum lu_cli_type type)
413 struct client_obd *cli = &obd->u.cli;
417 cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
421 prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
427 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
429 /* Init client side sequence-manager */
430 rc = seq_client_init(cli->cl_seq, exp, type, prefix);
441 EXPORT_SYMBOL(client_fid_init);
443 int client_fid_fini(struct obd_device *obd)
445 struct client_obd *cli = &obd->u.cli;
448 seq_client_fini(cli->cl_seq);
455 EXPORT_SYMBOL(client_fid_fini);
457 static int __init fid_init(void)
459 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
462 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
465 static void __exit fid_exit(void)
467 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
468 ldebugfs_remove(&seq_debugfs_dir);
471 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
472 MODULE_DESCRIPTION("Lustre File IDentifier");
473 MODULE_VERSION(LUSTRE_VERSION_STRING);
474 MODULE_LICENSE("GPL");
476 module_init(fid_init);
477 module_exit(fid_exit);