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, 2012, Whamcloud, Inc.
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
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <dt_object.h>
55 #include <md_object.h>
56 #include <obd_support.h>
57 #include <lustre_req_layout.h>
58 #include <lustre_fid.h>
60 #include <lustre_mdc.h>
61 #include "fid_internal.h"
63 static int seq_client_rpc(struct lu_client_seq *seq,
64 struct lu_seq_range *output, __u32 opc,
67 struct obd_export *exp = seq->lcs_exp;
68 struct ptlrpc_request *req;
69 struct lu_seq_range *out, *in;
74 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
75 LUSTRE_MDS_VERSION, SEQ_QUERY);
79 /* Init operation code */
80 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
83 /* Zero out input range, this is not recovery yet. */
84 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
87 ptlrpc_request_set_replen(req);
89 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
90 req->rq_request_portal = SEQ_METADATA_PORTAL;
91 in->lsr_flags = LU_SEQ_RANGE_MDT;
93 LASSERTF(seq->lcs_type == LUSTRE_SEQ_DATA,
94 "unknown lcs_type %u\n", seq->lcs_type);
95 req->rq_request_portal = SEQ_DATA_PORTAL;
96 in->lsr_flags = LU_SEQ_RANGE_OST;
99 if (opc == SEQ_ALLOC_SUPER) {
100 /* Update index field of *in, it is required for
101 * FLD update on super sequence allocator node. */
102 in->lsr_index = seq->lcs_space.lsr_index;
103 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
105 LASSERTF(opc == SEQ_ALLOC_META,
106 "unknown opcode %u\n, opc", opc);
109 ptlrpc_at_set_req_timeout(req);
111 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
112 rc = ptlrpc_queue_wait(req);
113 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
121 if (!range_is_sane(output)) {
122 CERROR("%s: Invalid range received from server: "
123 DRANGE"\n", seq->lcs_name, PRANGE(output));
124 GOTO(out_req, rc = -EINVAL);
127 if (range_is_exhausted(output)) {
128 CERROR("%s: Range received from server is exhausted: "
129 DRANGE"]\n", seq->lcs_name, PRANGE(output));
130 GOTO(out_req, rc = -EINVAL);
133 CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
134 seq->lcs_name, opcname, PRANGE(output));
138 ptlrpc_req_finished(req);
142 /* Request sequence-controller node to allocate new super-sequence. */
143 int seq_client_alloc_super(struct lu_client_seq *seq,
144 const struct lu_env *env)
149 cfs_mutex_lock(&seq->lcs_mutex);
153 LASSERT(env != NULL);
154 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
158 rc = seq_client_rpc(seq, &seq->lcs_space,
159 SEQ_ALLOC_SUPER, "super");
163 cfs_mutex_unlock(&seq->lcs_mutex);
167 /* Request sequence-controller node to allocate new meta-sequence. */
168 static int seq_client_alloc_meta(const struct lu_env *env,
169 struct lu_client_seq *seq)
176 LASSERT(env != NULL);
177 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
180 rc = seq_client_rpc(seq, &seq->lcs_space,
181 SEQ_ALLOC_META, "meta");
188 /* Allocate new sequence for client. */
189 static int seq_client_alloc_seq(const struct lu_env *env,
190 struct lu_client_seq *seq, seqno_t *seqnr)
195 LASSERT(range_is_sane(&seq->lcs_space));
197 if (range_is_exhausted(&seq->lcs_space)) {
198 rc = seq_client_alloc_meta(env, seq);
200 CERROR("%s: Can't allocate new meta-sequence,"
201 "rc %d\n", seq->lcs_name, rc);
204 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
205 seq->lcs_name, PRANGE(&seq->lcs_space));
211 LASSERT(!range_is_exhausted(&seq->lcs_space));
212 *seqnr = seq->lcs_space.lsr_start;
213 seq->lcs_space.lsr_start += 1;
215 CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
221 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
222 cfs_waitlink_t *link)
224 if (seq->lcs_update) {
225 cfs_waitq_add(&seq->lcs_waitq, link);
226 cfs_set_current_state(CFS_TASK_UNINT);
227 cfs_mutex_unlock(&seq->lcs_mutex);
229 cfs_waitq_wait(link, CFS_TASK_UNINT);
231 cfs_mutex_lock(&seq->lcs_mutex);
232 cfs_waitq_del(&seq->lcs_waitq, link);
233 cfs_set_current_state(CFS_TASK_RUNNING);
237 cfs_mutex_unlock(&seq->lcs_mutex);
241 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
243 LASSERT(seq->lcs_update == 1);
244 cfs_mutex_lock(&seq->lcs_mutex);
246 cfs_waitq_signal(&seq->lcs_waitq);
249 /* Allocate the whole seq to the caller*/
250 int seq_client_get_seq(const struct lu_env *env,
251 struct lu_client_seq *seq, seqno_t *seqnr)
256 LASSERT(seqnr != NULL);
257 cfs_mutex_lock(&seq->lcs_mutex);
258 cfs_waitlink_init(&link);
261 rc = seq_fid_alloc_prep(seq, &link);
266 rc = seq_client_alloc_seq(env, seq, seqnr);
268 CERROR("%s: Can't allocate new sequence, "
269 "rc %d\n", seq->lcs_name, rc);
270 seq_fid_alloc_fini(seq);
271 cfs_mutex_unlock(&seq->lcs_mutex);
275 CDEBUG(D_INFO, "%s: allocate sequence "
276 "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
278 /*Since the caller require the whole seq,
279 *so marked this seq to be used*/
280 seq->lcs_fid.f_oid = LUSTRE_SEQ_MAX_WIDTH;
281 seq->lcs_fid.f_seq = *seqnr;
282 seq->lcs_fid.f_ver = 0;
285 * Inform caller that sequence switch is performed to allow it
286 * to setup FLD for it.
288 seq_fid_alloc_fini(seq);
289 cfs_mutex_unlock(&seq->lcs_mutex);
293 EXPORT_SYMBOL(seq_client_get_seq);
295 /* Allocate new fid on passed client @seq and save it to @fid. */
296 int seq_client_alloc_fid(const struct lu_env *env,
297 struct lu_client_seq *seq, struct lu_fid *fid)
303 LASSERT(seq != NULL);
304 LASSERT(fid != NULL);
306 cfs_waitlink_init(&link);
307 cfs_mutex_lock(&seq->lcs_mutex);
312 if (!fid_is_zero(&seq->lcs_fid) &&
313 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
314 /* Just bump last allocated fid and return to caller. */
315 seq->lcs_fid.f_oid += 1;
320 rc = seq_fid_alloc_prep(seq, &link);
324 rc = seq_client_alloc_seq(env, seq, &seqnr);
326 CERROR("%s: Can't allocate new sequence, "
327 "rc %d\n", seq->lcs_name, rc);
328 seq_fid_alloc_fini(seq);
329 cfs_mutex_unlock(&seq->lcs_mutex);
333 CDEBUG(D_INFO, "%s: Switch to sequence "
334 "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
336 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
337 seq->lcs_fid.f_seq = seqnr;
338 seq->lcs_fid.f_ver = 0;
341 * Inform caller that sequence switch is performed to allow it
342 * to setup FLD for it.
346 seq_fid_alloc_fini(seq);
351 cfs_mutex_unlock(&seq->lcs_mutex);
353 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
356 EXPORT_SYMBOL(seq_client_alloc_fid);
359 * Finish the current sequence due to disconnect.
360 * See mdc_import_event()
362 void seq_client_flush(struct lu_client_seq *seq)
366 LASSERT(seq != NULL);
367 cfs_waitlink_init(&link);
368 cfs_mutex_lock(&seq->lcs_mutex);
370 while (seq->lcs_update) {
371 cfs_waitq_add(&seq->lcs_waitq, &link);
372 cfs_set_current_state(CFS_TASK_UNINT);
373 cfs_mutex_unlock(&seq->lcs_mutex);
375 cfs_waitq_wait(&link, CFS_TASK_UNINT);
377 cfs_mutex_lock(&seq->lcs_mutex);
378 cfs_waitq_del(&seq->lcs_waitq, &link);
379 cfs_set_current_state(CFS_TASK_RUNNING);
382 fid_zero(&seq->lcs_fid);
384 * this id shld not be used for seq range allocation.
385 * set to -1 for dgb check.
388 seq->lcs_space.lsr_index = -1;
390 range_init(&seq->lcs_space);
391 cfs_mutex_unlock(&seq->lcs_mutex);
393 EXPORT_SYMBOL(seq_client_flush);
395 static void seq_client_proc_fini(struct lu_client_seq *seq);
398 static int seq_client_proc_init(struct lu_client_seq *seq)
403 seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
407 if (IS_ERR(seq->lcs_proc_dir)) {
408 CERROR("%s: LProcFS failed in seq-init\n",
410 rc = PTR_ERR(seq->lcs_proc_dir);
414 rc = lprocfs_add_vars(seq->lcs_proc_dir,
415 seq_client_proc_list, seq);
417 CERROR("%s: Can't init sequence manager "
418 "proc, rc %d\n", seq->lcs_name, rc);
419 GOTO(out_cleanup, rc);
425 seq_client_proc_fini(seq);
429 static void seq_client_proc_fini(struct lu_client_seq *seq)
432 if (seq->lcs_proc_dir) {
433 if (!IS_ERR(seq->lcs_proc_dir))
434 lprocfs_remove(&seq->lcs_proc_dir);
435 seq->lcs_proc_dir = NULL;
440 static int seq_client_proc_init(struct lu_client_seq *seq)
445 static void seq_client_proc_fini(struct lu_client_seq *seq)
451 int seq_client_init(struct lu_client_seq *seq,
452 struct obd_export *exp,
453 enum lu_cli_type type,
455 struct lu_server_seq *srv)
460 LASSERT(seq != NULL);
461 LASSERT(prefix != NULL);
465 seq->lcs_type = type;
466 cfs_mutex_init(&seq->lcs_mutex);
467 seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
468 cfs_waitq_init(&seq->lcs_waitq);
470 /* Make sure that things are clear before work is started. */
471 seq_client_flush(seq);
474 LASSERT(seq->lcs_srv != NULL);
476 LASSERT(seq->lcs_exp != NULL);
477 seq->lcs_exp = class_export_get(seq->lcs_exp);
480 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
483 rc = seq_client_proc_init(seq);
485 seq_client_fini(seq);
488 EXPORT_SYMBOL(seq_client_init);
490 void seq_client_fini(struct lu_client_seq *seq)
494 seq_client_proc_fini(seq);
496 if (seq->lcs_exp != NULL) {
497 class_export_put(seq->lcs_exp);
504 EXPORT_SYMBOL(seq_client_fini);