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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/fid/fid_request.c
34 * Lustre Sequence Manager
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FID
41 #include "../../include/linux/libcfs/libcfs.h"
42 #include <linux/module.h>
44 #include "../include/obd.h"
45 #include "../include/obd_class.h"
46 #include "../include/obd_support.h"
47 #include "../include/lustre_fid.h"
49 #include "../include/lustre_mdc.h"
50 #include "fid_internal.h"
52 static struct dentry *seq_debugfs_dir;
54 static int seq_client_rpc(struct lu_client_seq *seq,
55 struct lu_seq_range *output, __u32 opc,
58 struct obd_export *exp = seq->lcs_exp;
59 struct ptlrpc_request *req;
60 struct lu_seq_range *out, *in;
62 unsigned int debug_mask;
65 LASSERT(exp && !IS_ERR(exp));
66 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
67 LUSTRE_MDS_VERSION, SEQ_QUERY);
71 /* Init operation code */
72 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
75 /* Zero out input range, this is not recovery yet. */
76 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
77 lu_seq_range_init(in);
79 ptlrpc_request_set_replen(req);
81 in->lsr_index = seq->lcs_space.lsr_index;
82 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
83 fld_range_set_mdt(in);
85 fld_range_set_ost(in);
87 if (opc == SEQ_ALLOC_SUPER) {
88 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
89 req->rq_reply_portal = MDC_REPLY_PORTAL;
90 /* During allocating super sequence for data object,
91 * the current thread might hold the export of MDT0(MDT0
92 * precreating objects on this OST), and it will send the
93 * request to MDT0 here, so we can not keep resending the
94 * request here, otherwise if MDT0 is failed(umounted),
95 * it can not release the export of MDT0
97 if (seq->lcs_type == LUSTRE_SEQ_DATA) {
99 req->rq_no_resend = 1;
101 debug_mask = D_CONSOLE;
103 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
104 req->rq_reply_portal = MDC_REPLY_PORTAL;
105 req->rq_request_portal = SEQ_METADATA_PORTAL;
107 req->rq_reply_portal = OSC_REPLY_PORTAL;
108 req->rq_request_portal = SEQ_DATA_PORTAL;
113 ptlrpc_at_set_req_timeout(req);
115 rc = ptlrpc_queue_wait(req);
119 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
122 if (!lu_seq_range_is_sane(output)) {
123 CERROR("%s: Invalid range received from server: "
124 DRANGE "\n", seq->lcs_name, PRANGE(output));
129 if (lu_seq_range_is_exhausted(output)) {
130 CERROR("%s: Range received from server is exhausted: "
131 DRANGE "]\n", seq->lcs_name, PRANGE(output));
136 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence " DRANGE "]\n",
137 seq->lcs_name, opcname, PRANGE(output));
140 ptlrpc_req_finished(req);
144 /* Request sequence-controller node to allocate new meta-sequence. */
145 static int seq_client_alloc_meta(const struct lu_env *env,
146 struct lu_client_seq *seq)
151 /* If meta server return -EINPROGRESS or EAGAIN,
152 * it means meta server might not be ready to
153 * allocate super sequence from sequence controller
156 rc = seq_client_rpc(seq, &seq->lcs_space,
157 SEQ_ALLOC_META, "meta");
158 } while (rc == -EINPROGRESS || rc == -EAGAIN);
163 /* Allocate new sequence for client. */
164 static int seq_client_alloc_seq(const struct lu_env *env,
165 struct lu_client_seq *seq, u64 *seqnr)
169 LASSERT(lu_seq_range_is_sane(&seq->lcs_space));
171 if (lu_seq_range_is_exhausted(&seq->lcs_space)) {
172 rc = seq_client_alloc_meta(env, seq);
174 CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
178 CDEBUG(D_INFO, "%s: New range - " DRANGE "\n",
179 seq->lcs_name, PRANGE(&seq->lcs_space));
184 LASSERT(!lu_seq_range_is_exhausted(&seq->lcs_space));
185 *seqnr = seq->lcs_space.lsr_start;
186 seq->lcs_space.lsr_start += 1;
188 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
194 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
195 wait_queue_entry_t *link)
197 if (seq->lcs_update) {
198 add_wait_queue(&seq->lcs_waitq, link);
199 set_current_state(TASK_UNINTERRUPTIBLE);
200 mutex_unlock(&seq->lcs_mutex);
204 mutex_lock(&seq->lcs_mutex);
205 remove_wait_queue(&seq->lcs_waitq, link);
206 set_current_state(TASK_RUNNING);
210 mutex_unlock(&seq->lcs_mutex);
214 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
216 LASSERT(seq->lcs_update == 1);
217 mutex_lock(&seq->lcs_mutex);
219 wake_up(&seq->lcs_waitq);
222 /* Allocate new fid on passed client @seq and save it to @fid. */
223 int seq_client_alloc_fid(const struct lu_env *env,
224 struct lu_client_seq *seq, struct lu_fid *fid)
226 wait_queue_entry_t link;
232 init_waitqueue_entry(&link, current);
233 mutex_lock(&seq->lcs_mutex);
235 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
236 seq->lcs_fid.f_oid = seq->lcs_width;
241 if (!fid_is_zero(&seq->lcs_fid) &&
242 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
243 /* Just bump last allocated fid and return to caller. */
244 seq->lcs_fid.f_oid += 1;
249 rc = seq_fid_alloc_prep(seq, &link);
253 rc = seq_client_alloc_seq(env, seq, &seqnr);
255 CERROR("%s: Can't allocate new sequence, rc %d\n",
257 seq_fid_alloc_fini(seq);
258 mutex_unlock(&seq->lcs_mutex);
262 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16llx]\n",
263 seq->lcs_name, seqnr);
265 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
266 seq->lcs_fid.f_seq = seqnr;
267 seq->lcs_fid.f_ver = 0;
270 * Inform caller that sequence switch is performed to allow it
271 * to setup FLD for it.
275 seq_fid_alloc_fini(seq);
280 mutex_unlock(&seq->lcs_mutex);
282 CDEBUG(D_INFO, "%s: Allocated FID " DFID "\n", seq->lcs_name, PFID(fid));
285 EXPORT_SYMBOL(seq_client_alloc_fid);
288 * Finish the current sequence due to disconnect.
289 * See mdc_import_event()
291 void seq_client_flush(struct lu_client_seq *seq)
293 wait_queue_entry_t link;
296 init_waitqueue_entry(&link, current);
297 mutex_lock(&seq->lcs_mutex);
299 while (seq->lcs_update) {
300 add_wait_queue(&seq->lcs_waitq, &link);
301 set_current_state(TASK_UNINTERRUPTIBLE);
302 mutex_unlock(&seq->lcs_mutex);
306 mutex_lock(&seq->lcs_mutex);
307 remove_wait_queue(&seq->lcs_waitq, &link);
308 set_current_state(TASK_RUNNING);
311 fid_zero(&seq->lcs_fid);
313 * this id shld not be used for seq range allocation.
314 * set to -1 for dgb check.
317 seq->lcs_space.lsr_index = -1;
319 lu_seq_range_init(&seq->lcs_space);
320 mutex_unlock(&seq->lcs_mutex);
322 EXPORT_SYMBOL(seq_client_flush);
324 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
326 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
327 ldebugfs_remove(&seq->lcs_debugfs_entry);
330 static int seq_client_debugfs_init(struct lu_client_seq *seq)
334 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
338 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
339 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
340 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
342 seq->lcs_debugfs_entry = NULL;
346 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
347 seq_client_debugfs_list, seq);
349 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
357 seq_client_debugfs_fini(seq);
361 static void seq_client_fini(struct lu_client_seq *seq)
363 seq_client_debugfs_fini(seq);
366 class_export_put(seq->lcs_exp);
371 static int seq_client_init(struct lu_client_seq *seq,
372 struct obd_export *exp,
373 enum lu_cli_type type,
381 seq->lcs_type = type;
383 mutex_init(&seq->lcs_mutex);
384 if (type == LUSTRE_SEQ_METADATA)
385 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
387 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
389 init_waitqueue_head(&seq->lcs_waitq);
390 /* Make sure that things are clear before work is started. */
391 seq_client_flush(seq);
393 seq->lcs_exp = class_export_get(exp);
395 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
398 rc = seq_client_debugfs_init(seq);
400 seq_client_fini(seq);
404 int client_fid_init(struct obd_device *obd,
405 struct obd_export *exp, enum lu_cli_type type)
407 struct client_obd *cli = &obd->u.cli;
411 cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
415 prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
421 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
423 /* Init client side sequence-manager */
424 rc = seq_client_init(cli->cl_seq, exp, type, prefix);
435 EXPORT_SYMBOL(client_fid_init);
437 int client_fid_fini(struct obd_device *obd)
439 struct client_obd *cli = &obd->u.cli;
442 seq_client_fini(cli->cl_seq);
449 EXPORT_SYMBOL(client_fid_fini);
451 static int __init fid_init(void)
453 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
456 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
459 static void __exit fid_exit(void)
461 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
462 ldebugfs_remove(&seq_debugfs_dir);
465 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
466 MODULE_DESCRIPTION("Lustre File IDentifier");
467 MODULE_VERSION(LUSTRE_VERSION_STRING);
468 MODULE_LICENSE("GPL");
470 module_init(fid_init);
471 module_exit(fid_exit);