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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_FID
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <dt_object.h>
58 #include <md_object.h>
59 #include <obd_support.h>
60 #include <lustre_req_layout.h>
61 #include <lustre_fid.h>
63 #include <lustre_mdc.h>
64 #include "fid_internal.h"
66 static int seq_client_rpc(struct lu_client_seq *seq,
67 struct lu_seq_range *output, __u32 opc,
70 struct obd_export *exp = seq->lcs_exp;
71 struct ptlrpc_request *req;
72 struct lu_seq_range *out, *in;
77 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
78 LUSTRE_MDS_VERSION, SEQ_QUERY);
82 /* Init operation code */
83 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
86 /* Zero out input range, this is not recovery yet. */
87 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
90 ptlrpc_request_set_replen(req);
92 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
93 req->rq_request_portal = (opc == SEQ_ALLOC_SUPER) ?
94 SEQ_CONTROLLER_PORTAL : SEQ_METADATA_PORTAL;
95 /* update mdt field of *in, it is required for fld update
96 * on super sequence allocator node. */
97 if (opc == SEQ_ALLOC_SUPER)
98 in->lsr_mdt = seq->lcs_space.lsr_mdt;
100 LASSERT(opc == SEQ_ALLOC_META);
101 req->rq_request_portal = SEQ_DATA_PORTAL;
103 ptlrpc_at_set_req_timeout(req);
105 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
106 rc = ptlrpc_queue_wait(req);
107 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
112 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
115 if (!range_is_sane(output)) {
116 CERROR("%s: Invalid range received from server: "
117 DRANGE"\n", seq->lcs_name, PRANGE(output));
118 GOTO(out_req, rc = -EINVAL);
121 if (range_is_exhausted(output)) {
122 CERROR("%s: Range received from server is exhausted: "
123 DRANGE"]\n", seq->lcs_name, PRANGE(output));
124 GOTO(out_req, rc = -EINVAL);
127 CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
128 seq->lcs_name, opcname, PRANGE(output));
132 ptlrpc_req_finished(req);
136 /* Request sequence-controller node to allocate new super-sequence. */
137 int seq_client_alloc_super(struct lu_client_seq *seq,
138 const struct lu_env *env)
143 cfs_down(&seq->lcs_sem);
147 LASSERT(env != NULL);
148 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
152 rc = seq_client_rpc(seq, &seq->lcs_space,
153 SEQ_ALLOC_SUPER, "super");
157 cfs_up(&seq->lcs_sem);
161 /* Request sequence-controller node to allocate new meta-sequence. */
162 static int seq_client_alloc_meta(struct lu_client_seq *seq,
163 const struct lu_env *env)
170 LASSERT(env != NULL);
171 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
174 rc = seq_client_rpc(seq, &seq->lcs_space,
175 SEQ_ALLOC_META, "meta");
182 /* Allocate new sequence for client. */
183 static int seq_client_alloc_seq(struct lu_client_seq *seq, seqno_t *seqnr)
188 LASSERT(range_is_sane(&seq->lcs_space));
190 if (range_is_exhausted(&seq->lcs_space)) {
191 rc = seq_client_alloc_meta(seq, NULL);
193 CERROR("%s: Can't allocate new meta-sequence, "
194 "rc %d\n", seq->lcs_name, rc);
197 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
198 seq->lcs_name, PRANGE(&seq->lcs_space));
204 LASSERT(!range_is_exhausted(&seq->lcs_space));
205 *seqnr = seq->lcs_space.lsr_start;
206 seq->lcs_space.lsr_start += 1;
208 CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
214 /* Allocate new fid on passed client @seq and save it to @fid. */
215 int seq_client_alloc_fid(struct lu_client_seq *seq, struct lu_fid *fid)
220 LASSERT(seq != NULL);
221 LASSERT(fid != NULL);
223 cfs_down(&seq->lcs_sem);
225 if (fid_is_zero(&seq->lcs_fid) ||
226 fid_oid(&seq->lcs_fid) >= seq->lcs_width)
230 rc = seq_client_alloc_seq(seq, &seqnr);
232 CERROR("%s: Can't allocate new sequence, "
233 "rc %d\n", seq->lcs_name, rc);
234 cfs_up(&seq->lcs_sem);
238 CDEBUG(D_INFO, "%s: Switch to sequence "
239 "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
241 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
242 seq->lcs_fid.f_seq = seqnr;
243 seq->lcs_fid.f_ver = 0;
246 * Inform caller that sequence switch is performed to allow it
247 * to setup FLD for it.
251 /* Just bump last allocated fid and return to caller. */
252 seq->lcs_fid.f_oid += 1;
257 cfs_up(&seq->lcs_sem);
259 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
262 EXPORT_SYMBOL(seq_client_alloc_fid);
265 * Finish the current sequence due to disconnect.
266 * See mdc_import_event()
268 void seq_client_flush(struct lu_client_seq *seq)
270 LASSERT(seq != NULL);
271 cfs_down(&seq->lcs_sem);
272 fid_zero(&seq->lcs_fid);
274 * this id shld not be used for seq range allocation.
275 * set to -1 for dgb check.
278 seq->lcs_space.lsr_mdt = -1;
280 range_init(&seq->lcs_space);
281 cfs_up(&seq->lcs_sem);
283 EXPORT_SYMBOL(seq_client_flush);
285 static void seq_client_proc_fini(struct lu_client_seq *seq);
288 static int seq_client_proc_init(struct lu_client_seq *seq)
293 seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
297 if (IS_ERR(seq->lcs_proc_dir)) {
298 CERROR("%s: LProcFS failed in seq-init\n",
300 rc = PTR_ERR(seq->lcs_proc_dir);
304 rc = lprocfs_add_vars(seq->lcs_proc_dir,
305 seq_client_proc_list, seq);
307 CERROR("%s: Can't init sequence manager "
308 "proc, rc %d\n", seq->lcs_name, rc);
309 GOTO(out_cleanup, rc);
315 seq_client_proc_fini(seq);
319 static void seq_client_proc_fini(struct lu_client_seq *seq)
322 if (seq->lcs_proc_dir) {
323 if (!IS_ERR(seq->lcs_proc_dir))
324 lprocfs_remove(&seq->lcs_proc_dir);
325 seq->lcs_proc_dir = NULL;
330 static int seq_client_proc_init(struct lu_client_seq *seq)
335 static void seq_client_proc_fini(struct lu_client_seq *seq)
341 int seq_client_init(struct lu_client_seq *seq,
342 struct obd_export *exp,
343 enum lu_cli_type type,
345 struct lu_server_seq *srv)
350 LASSERT(seq != NULL);
351 LASSERT(prefix != NULL);
355 seq->lcs_type = type;
356 cfs_sema_init(&seq->lcs_sem, 1);
357 seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
359 /* Make sure that things are clear before work is started. */
360 seq_client_flush(seq);
363 LASSERT(seq->lcs_srv != NULL);
365 LASSERT(seq->lcs_exp != NULL);
366 seq->lcs_exp = class_export_get(seq->lcs_exp);
369 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
372 rc = seq_client_proc_init(seq);
374 seq_client_fini(seq);
377 EXPORT_SYMBOL(seq_client_init);
379 void seq_client_fini(struct lu_client_seq *seq)
383 seq_client_proc_fini(seq);
385 if (seq->lcs_exp != NULL) {
386 class_export_put(seq->lcs_exp);
393 EXPORT_SYMBOL(seq_client_fini);