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, 2013, 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 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
70 LUSTRE_MDS_VERSION, SEQ_QUERY);
74 /* Init operation code */
75 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
78 /* Zero out input range, this is not recovery yet. */
79 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
82 ptlrpc_request_set_replen(req);
84 in->lsr_index = seq->lcs_space.lsr_index;
85 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
86 fld_range_set_mdt(in);
88 fld_range_set_ost(in);
90 if (opc == SEQ_ALLOC_SUPER) {
91 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
92 req->rq_reply_portal = MDC_REPLY_PORTAL;
93 /* During allocating super sequence for data object,
94 * the current thread might hold the export of MDT0(MDT0
95 * precreating objects on this OST), and it will send the
96 * request to MDT0 here, so we can not keep resending the
97 * request here, otherwise if MDT0 is failed(umounted),
98 * it can not release the export of MDT0 */
99 if (seq->lcs_type == LUSTRE_SEQ_DATA)
100 req->rq_no_delay = req->rq_no_resend = 1;
101 debug_mask = D_CONSOLE;
103 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
104 req->rq_request_portal = SEQ_METADATA_PORTAL;
106 req->rq_request_portal = SEQ_DATA_PORTAL;
110 ptlrpc_at_set_req_timeout(req);
112 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
113 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
114 rc = ptlrpc_queue_wait(req);
115 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
116 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
120 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
123 if (!range_is_sane(output)) {
124 CERROR("%s: Invalid range received from server: "
125 DRANGE"\n", seq->lcs_name, PRANGE(output));
130 if (range_is_exhausted(output)) {
131 CERROR("%s: Range received from server is exhausted: "
132 DRANGE"]\n", seq->lcs_name, PRANGE(output));
137 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
138 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)
151 mutex_lock(&seq->lcs_mutex);
153 /* Check whether the connection to seq controller has been
154 * setup (lcs_exp != NULL) */
156 mutex_unlock(&seq->lcs_mutex);
160 rc = seq_client_rpc(seq, &seq->lcs_space,
161 SEQ_ALLOC_SUPER, "super");
162 mutex_unlock(&seq->lcs_mutex);
166 /* Request sequence-controller node to allocate new meta-sequence. */
167 static int seq_client_alloc_meta(const struct lu_env *env,
168 struct lu_client_seq *seq)
173 /* If meta server return -EINPROGRESS or EAGAIN,
174 * it means meta server might not be ready to
175 * allocate super sequence from sequence controller
177 rc = seq_client_rpc(seq, &seq->lcs_space,
178 SEQ_ALLOC_META, "meta");
179 } while (rc == -EINPROGRESS || rc == -EAGAIN);
184 /* Allocate new sequence for client. */
185 static int seq_client_alloc_seq(const struct lu_env *env,
186 struct lu_client_seq *seq, u64 *seqnr)
190 LASSERT(range_is_sane(&seq->lcs_space));
192 if (range_is_exhausted(&seq->lcs_space)) {
193 rc = seq_client_alloc_meta(env, seq);
195 CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
199 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
200 seq->lcs_name, PRANGE(&seq->lcs_space));
205 LASSERT(!range_is_exhausted(&seq->lcs_space));
206 *seqnr = seq->lcs_space.lsr_start;
207 seq->lcs_space.lsr_start += 1;
209 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
215 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
218 if (seq->lcs_update) {
219 add_wait_queue(&seq->lcs_waitq, link);
220 set_current_state(TASK_UNINTERRUPTIBLE);
221 mutex_unlock(&seq->lcs_mutex);
225 mutex_lock(&seq->lcs_mutex);
226 remove_wait_queue(&seq->lcs_waitq, link);
227 set_current_state(TASK_RUNNING);
231 mutex_unlock(&seq->lcs_mutex);
235 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
237 LASSERT(seq->lcs_update == 1);
238 mutex_lock(&seq->lcs_mutex);
240 wake_up(&seq->lcs_waitq);
243 /* Allocate new fid on passed client @seq and save it to @fid. */
244 int seq_client_alloc_fid(const struct lu_env *env,
245 struct lu_client_seq *seq, struct lu_fid *fid)
250 LASSERT(seq != NULL);
251 LASSERT(fid != NULL);
253 init_waitqueue_entry(&link, current);
254 mutex_lock(&seq->lcs_mutex);
256 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
257 seq->lcs_fid.f_oid = seq->lcs_width;
262 if (!fid_is_zero(&seq->lcs_fid) &&
263 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
264 /* Just bump last allocated fid and return to caller. */
265 seq->lcs_fid.f_oid += 1;
270 rc = seq_fid_alloc_prep(seq, &link);
274 rc = seq_client_alloc_seq(env, seq, &seqnr);
276 CERROR("%s: Can't allocate new sequence, rc %d\n",
278 seq_fid_alloc_fini(seq);
279 mutex_unlock(&seq->lcs_mutex);
283 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16Lx]\n",
284 seq->lcs_name, seqnr);
286 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
287 seq->lcs_fid.f_seq = seqnr;
288 seq->lcs_fid.f_ver = 0;
291 * Inform caller that sequence switch is performed to allow it
292 * to setup FLD for it.
296 seq_fid_alloc_fini(seq);
301 mutex_unlock(&seq->lcs_mutex);
303 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
306 EXPORT_SYMBOL(seq_client_alloc_fid);
309 * Finish the current sequence due to disconnect.
310 * See mdc_import_event()
312 void seq_client_flush(struct lu_client_seq *seq)
316 LASSERT(seq != NULL);
317 init_waitqueue_entry(&link, current);
318 mutex_lock(&seq->lcs_mutex);
320 while (seq->lcs_update) {
321 add_wait_queue(&seq->lcs_waitq, &link);
322 set_current_state(TASK_UNINTERRUPTIBLE);
323 mutex_unlock(&seq->lcs_mutex);
327 mutex_lock(&seq->lcs_mutex);
328 remove_wait_queue(&seq->lcs_waitq, &link);
329 set_current_state(TASK_RUNNING);
332 fid_zero(&seq->lcs_fid);
334 * this id shld not be used for seq range allocation.
335 * set to -1 for dgb check.
338 seq->lcs_space.lsr_index = -1;
340 range_init(&seq->lcs_space);
341 mutex_unlock(&seq->lcs_mutex);
343 EXPORT_SYMBOL(seq_client_flush);
345 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
347 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
348 ldebugfs_remove(&seq->lcs_debugfs_entry);
351 static int seq_client_debugfs_init(struct lu_client_seq *seq)
355 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
359 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
360 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
361 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
363 seq->lcs_debugfs_entry = NULL;
367 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
368 seq_client_debugfs_list, seq);
370 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
378 seq_client_debugfs_fini(seq);
382 static void seq_client_fini(struct lu_client_seq *seq)
384 seq_client_debugfs_fini(seq);
387 class_export_put(seq->lcs_exp);
392 static int seq_client_init(struct lu_client_seq *seq,
393 struct obd_export *exp,
394 enum lu_cli_type type,
399 LASSERT(seq != NULL);
400 LASSERT(prefix != NULL);
402 seq->lcs_type = type;
404 mutex_init(&seq->lcs_mutex);
405 if (type == LUSTRE_SEQ_METADATA)
406 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
408 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
410 init_waitqueue_head(&seq->lcs_waitq);
411 /* Make sure that things are clear before work is started. */
412 seq_client_flush(seq);
414 seq->lcs_exp = class_export_get(exp);
416 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
419 rc = seq_client_debugfs_init(seq);
421 seq_client_fini(seq);
425 int client_fid_init(struct obd_device *obd,
426 struct obd_export *exp, enum lu_cli_type type)
428 struct client_obd *cli = &obd->u.cli;
432 cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
436 prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
442 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
444 /* Init client side sequence-manager */
445 rc = seq_client_init(cli->cl_seq, exp, type, prefix);
456 EXPORT_SYMBOL(client_fid_init);
458 int client_fid_fini(struct obd_device *obd)
460 struct client_obd *cli = &obd->u.cli;
462 if (cli->cl_seq != NULL) {
463 seq_client_fini(cli->cl_seq);
470 EXPORT_SYMBOL(client_fid_fini);
472 static int __init fid_mod_init(void)
474 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
477 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
480 static void __exit fid_mod_exit(void)
482 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
483 ldebugfs_remove(&seq_debugfs_dir);
486 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
487 MODULE_DESCRIPTION("Lustre FID Module");
488 MODULE_LICENSE("GPL");
489 MODULE_VERSION("0.1.0");
491 module_init(fid_mod_init);
492 module_exit(fid_mod_exit);