1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/fid/fid_request.c
40 * Lustre Sequence Manager
42 * Author: Yury Umanets <umka@clusterfs.com>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_FID
51 # include <libcfs/libcfs.h>
52 # include <linux/module.h>
53 #else /* __KERNEL__ */
54 # include <liblustre.h>
58 #include <obd_class.h>
59 #include <dt_object.h>
60 #include <md_object.h>
61 #include <obd_support.h>
62 #include <lustre_req_layout.h>
63 #include <lustre_fid.h>
65 #include <lustre_mdc.h>
66 #include "fid_internal.h"
68 static int seq_client_rpc(struct lu_client_seq *seq,
69 struct lu_seq_range *output, __u32 opc,
72 struct obd_export *exp = seq->lcs_exp;
73 struct ptlrpc_request *req;
74 struct lu_seq_range *out, *in;
79 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
80 LUSTRE_MDS_VERSION, SEQ_QUERY);
84 /* Init operation code */
85 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
88 /* Zero out input range, this is not recovery yet. */
89 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
92 ptlrpc_request_set_replen(req);
94 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
95 req->rq_request_portal = SEQ_METADATA_PORTAL;
96 in->lsr_flags = LU_SEQ_RANGE_MDT;
98 LASSERTF(seq->lcs_type == LUSTRE_SEQ_DATA,
99 "unknown lcs_type %u\n", seq->lcs_type);
100 req->rq_request_portal = SEQ_DATA_PORTAL;
101 in->lsr_flags = LU_SEQ_RANGE_OST;
104 if (opc == SEQ_ALLOC_SUPER) {
105 /* Update index field of *in, it is required for
106 * FLD update on super sequence allocator node. */
107 in->lsr_index = seq->lcs_space.lsr_index;
108 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
110 LASSERTF(opc == SEQ_ALLOC_META,
111 "unknown opcode %u\n, opc", opc);
114 ptlrpc_at_set_req_timeout(req);
116 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
117 rc = ptlrpc_queue_wait(req);
118 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));
129 GOTO(out_req, rc = -EINVAL);
132 if (range_is_exhausted(output)) {
133 CERROR("%s: Range received from server is exhausted: "
134 DRANGE"]\n", seq->lcs_name, PRANGE(output));
135 GOTO(out_req, rc = -EINVAL);
138 CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
139 seq->lcs_name, opcname, PRANGE(output));
143 ptlrpc_req_finished(req);
147 /* Request sequence-controller node to allocate new super-sequence. */
148 int seq_client_alloc_super(struct lu_client_seq *seq,
149 const struct lu_env *env)
154 cfs_mutex_lock(&seq->lcs_mutex);
158 LASSERT(env != NULL);
159 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
163 rc = seq_client_rpc(seq, &seq->lcs_space,
164 SEQ_ALLOC_SUPER, "super");
168 cfs_mutex_unlock(&seq->lcs_mutex);
172 /* Request sequence-controller node to allocate new meta-sequence. */
173 static int seq_client_alloc_meta(const struct lu_env *env,
174 struct lu_client_seq *seq)
181 LASSERT(env != NULL);
182 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
185 rc = seq_client_rpc(seq, &seq->lcs_space,
186 SEQ_ALLOC_META, "meta");
193 /* Allocate new sequence for client. */
194 static int seq_client_alloc_seq(const struct lu_env *env,
195 struct lu_client_seq *seq, seqno_t *seqnr)
200 LASSERT(range_is_sane(&seq->lcs_space));
202 if (range_is_exhausted(&seq->lcs_space)) {
203 rc = seq_client_alloc_meta(env, seq);
205 CERROR("%s: Can't allocate new meta-sequence,"
206 "rc %d\n", seq->lcs_name, rc);
209 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
210 seq->lcs_name, PRANGE(&seq->lcs_space));
216 LASSERT(!range_is_exhausted(&seq->lcs_space));
217 *seqnr = seq->lcs_space.lsr_start;
218 seq->lcs_space.lsr_start += 1;
220 CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
226 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
227 cfs_waitlink_t *link)
229 if (seq->lcs_update) {
230 cfs_waitq_add(&seq->lcs_waitq, link);
231 cfs_set_current_state(CFS_TASK_UNINT);
232 cfs_mutex_unlock(&seq->lcs_mutex);
234 cfs_waitq_wait(link, CFS_TASK_UNINT);
236 cfs_mutex_lock(&seq->lcs_mutex);
237 cfs_waitq_del(&seq->lcs_waitq, link);
238 cfs_set_current_state(CFS_TASK_RUNNING);
242 cfs_mutex_unlock(&seq->lcs_mutex);
246 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
248 LASSERT(seq->lcs_update == 1);
249 cfs_mutex_lock(&seq->lcs_mutex);
251 cfs_waitq_signal(&seq->lcs_waitq);
254 /* Allocate the whole seq to the caller*/
255 int seq_client_get_seq(const struct lu_env *env,
256 struct lu_client_seq *seq, seqno_t *seqnr)
261 LASSERT(seqnr != NULL);
262 cfs_mutex_lock(&seq->lcs_mutex);
263 cfs_waitlink_init(&link);
266 rc = seq_fid_alloc_prep(seq, &link);
271 rc = seq_client_alloc_seq(env, seq, seqnr);
273 CERROR("%s: Can't allocate new sequence, "
274 "rc %d\n", seq->lcs_name, rc);
275 seq_fid_alloc_fini(seq);
276 cfs_mutex_unlock(&seq->lcs_mutex);
280 CDEBUG(D_INFO, "%s: allocate sequence "
281 "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
283 /*Since the caller require the whole seq,
284 *so marked this seq to be used*/
285 seq->lcs_fid.f_oid = LUSTRE_SEQ_MAX_WIDTH;
286 seq->lcs_fid.f_seq = *seqnr;
287 seq->lcs_fid.f_ver = 0;
290 * Inform caller that sequence switch is performed to allow it
291 * to setup FLD for it.
293 seq_fid_alloc_fini(seq);
294 cfs_mutex_unlock(&seq->lcs_mutex);
298 EXPORT_SYMBOL(seq_client_get_seq);
300 /* Allocate new fid on passed client @seq and save it to @fid. */
301 int seq_client_alloc_fid(const struct lu_env *env,
302 struct lu_client_seq *seq, struct lu_fid *fid)
308 LASSERT(seq != NULL);
309 LASSERT(fid != NULL);
311 cfs_waitlink_init(&link);
312 cfs_mutex_lock(&seq->lcs_mutex);
317 if (!fid_is_zero(&seq->lcs_fid) &&
318 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
319 /* Just bump last allocated fid and return to caller. */
320 seq->lcs_fid.f_oid += 1;
325 rc = seq_fid_alloc_prep(seq, &link);
329 rc = seq_client_alloc_seq(env, seq, &seqnr);
331 CERROR("%s: Can't allocate new sequence, "
332 "rc %d\n", seq->lcs_name, rc);
333 seq_fid_alloc_fini(seq);
334 cfs_mutex_unlock(&seq->lcs_mutex);
338 CDEBUG(D_INFO, "%s: Switch to sequence "
339 "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
341 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
342 seq->lcs_fid.f_seq = seqnr;
343 seq->lcs_fid.f_ver = 0;
346 * Inform caller that sequence switch is performed to allow it
347 * to setup FLD for it.
351 seq_fid_alloc_fini(seq);
356 cfs_mutex_unlock(&seq->lcs_mutex);
358 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
361 EXPORT_SYMBOL(seq_client_alloc_fid);
364 * Finish the current sequence due to disconnect.
365 * See mdc_import_event()
367 void seq_client_flush(struct lu_client_seq *seq)
371 LASSERT(seq != NULL);
372 cfs_waitlink_init(&link);
373 cfs_mutex_lock(&seq->lcs_mutex);
375 while (seq->lcs_update) {
376 cfs_waitq_add(&seq->lcs_waitq, &link);
377 cfs_set_current_state(CFS_TASK_UNINT);
378 cfs_mutex_unlock(&seq->lcs_mutex);
380 cfs_waitq_wait(&link, CFS_TASK_UNINT);
382 cfs_mutex_lock(&seq->lcs_mutex);
383 cfs_waitq_del(&seq->lcs_waitq, &link);
384 cfs_set_current_state(CFS_TASK_RUNNING);
387 fid_zero(&seq->lcs_fid);
389 * this id shld not be used for seq range allocation.
390 * set to -1 for dgb check.
393 seq->lcs_space.lsr_index = -1;
395 range_init(&seq->lcs_space);
396 cfs_mutex_unlock(&seq->lcs_mutex);
398 EXPORT_SYMBOL(seq_client_flush);
400 static void seq_client_proc_fini(struct lu_client_seq *seq);
403 static int seq_client_proc_init(struct lu_client_seq *seq)
408 seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
412 if (IS_ERR(seq->lcs_proc_dir)) {
413 CERROR("%s: LProcFS failed in seq-init\n",
415 rc = PTR_ERR(seq->lcs_proc_dir);
419 rc = lprocfs_add_vars(seq->lcs_proc_dir,
420 seq_client_proc_list, seq);
422 CERROR("%s: Can't init sequence manager "
423 "proc, rc %d\n", seq->lcs_name, rc);
424 GOTO(out_cleanup, rc);
430 seq_client_proc_fini(seq);
434 static void seq_client_proc_fini(struct lu_client_seq *seq)
437 if (seq->lcs_proc_dir) {
438 if (!IS_ERR(seq->lcs_proc_dir))
439 lprocfs_remove(&seq->lcs_proc_dir);
440 seq->lcs_proc_dir = NULL;
445 static int seq_client_proc_init(struct lu_client_seq *seq)
450 static void seq_client_proc_fini(struct lu_client_seq *seq)
456 int seq_client_init(struct lu_client_seq *seq,
457 struct obd_export *exp,
458 enum lu_cli_type type,
460 struct lu_server_seq *srv)
465 LASSERT(seq != NULL);
466 LASSERT(prefix != NULL);
470 seq->lcs_type = type;
471 cfs_mutex_init(&seq->lcs_mutex);
472 seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
473 cfs_waitq_init(&seq->lcs_waitq);
475 /* Make sure that things are clear before work is started. */
476 seq_client_flush(seq);
479 LASSERT(seq->lcs_srv != NULL);
481 LASSERT(seq->lcs_exp != NULL);
482 seq->lcs_exp = class_export_get(seq->lcs_exp);
485 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
488 rc = seq_client_proc_init(seq);
490 seq_client_fini(seq);
493 EXPORT_SYMBOL(seq_client_init);
495 void seq_client_fini(struct lu_client_seq *seq)
499 seq_client_proc_fini(seq);
501 if (seq->lcs_exp != NULL) {
502 class_export_put(seq->lcs_exp);
509 EXPORT_SYMBOL(seq_client_fini);