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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fld/fld_index.c
38 * Author: WangDi <wangdi@clusterfs.com>
39 * Author: Yury Umanets <umka@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_FLD
47 # include <libcfs/libcfs.h>
48 # include <linux/module.h>
49 # include <linux/jbd.h>
50 #else /* __KERNEL__ */
51 # include <liblustre.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
60 #include <dt_object.h>
61 #include <md_object.h>
62 #include <lustre_mdc.h>
63 #include <lustre_fid.h>
64 #include <lustre_fld.h>
65 #include "fld_internal.h"
67 const char fld_index_name[] = "fld";
69 static const struct lu_seq_range IGIF_FLD_RANGE = {
71 .lsr_end = FID_SEQ_IDIF,
73 .lsr_flags = LU_SEQ_RANGE_MDT
76 const struct dt_index_features fld_index_features = {
77 .dif_flags = DT_IND_UPDATE,
78 .dif_keysize_min = sizeof(seqno_t),
79 .dif_keysize_max = sizeof(seqno_t),
80 .dif_recsize_min = sizeof(struct lu_seq_range),
81 .dif_recsize_max = sizeof(struct lu_seq_range),
85 extern struct lu_context_key fld_thread_key;
87 static struct dt_key *fld_key(const struct lu_env *env,
90 struct fld_thread_info *info;
93 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
94 LASSERT(info != NULL);
96 info->fti_key = cpu_to_be64(seq);
97 RETURN((void *)&info->fti_key);
100 static struct dt_rec *fld_rec(const struct lu_env *env,
101 const struct lu_seq_range *range)
103 struct fld_thread_info *info;
104 struct lu_seq_range *rec;
107 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
108 LASSERT(info != NULL);
109 rec = &info->fti_rec;
111 range_cpu_to_be(rec, range);
115 struct thandle* fld_trans_start(struct lu_server_fld *fld,
116 const struct lu_env *env, int credit)
118 struct fld_thread_info *info;
119 struct dt_device *dt_dev;
122 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
123 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
124 p = &info->fti_txn_param;
125 txn_param_init(p, credit);
127 return dt_dev->dd_ops->dt_trans_start(env, dt_dev, p);
130 void fld_trans_stop(struct lu_server_fld *fld,
131 const struct lu_env *env, struct thandle* th)
133 struct dt_device *dt_dev;
135 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
136 dt_dev->dd_ops->dt_trans_stop(env, th);
140 * insert range in fld store.
142 * \param range range to be inserted
143 * \param th transaction for this operation as it could compound
150 int fld_index_create(struct lu_server_fld *fld,
151 const struct lu_env *env,
152 const struct lu_seq_range *range,
155 struct dt_object *dt_obj = fld->lsf_obj;
156 struct dt_device *dt_dev;
162 start = range->lsr_start;
163 LASSERT(range_is_sane(range));
164 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
166 rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
171 CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
172 fld->lsf_name, PRANGE(range), rc);
177 * delete range in fld store.
179 * \param range range to be deleted
180 * \param th transaction
186 int fld_index_delete(struct lu_server_fld *fld,
187 const struct lu_env *env,
188 struct lu_seq_range *range,
191 struct dt_object *dt_obj = fld->lsf_obj;
192 struct dt_device *dt_dev;
193 seqno_t seq = range->lsr_start;
198 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
199 rc = dt_obj->do_index_ops->dio_delete(env, dt_obj,
200 fld_key(env, seq), th,
203 CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
204 fld->lsf_name, PRANGE(range), rc);
210 * lookup range for a seq passed. note here we only care about the start/end,
211 * caller should handle the attached location data (flags, index).
213 * \param seq seq for lookup.
214 * \param range result of lookup.
216 * \retval 0 found, \a range is the matched range;
217 * \retval -ENOENT not found, \a range is the left-side range;
218 * \retval -ve other error;
221 int fld_index_lookup(struct lu_server_fld *fld,
222 const struct lu_env *env,
224 struct lu_seq_range *range)
226 struct dt_object *dt_obj = fld->lsf_obj;
227 struct lu_seq_range *fld_rec;
228 struct dt_key *key = fld_key(env, seq);
229 struct fld_thread_info *info;
234 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
235 fld_rec = &info->fti_rec;
237 rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
238 (struct dt_rec*) fld_rec,
242 range_be_to_cpu(fld_rec, fld_rec);
244 if (range_within(range, seq))
250 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
251 fld->lsf_name, seq, PRANGE(range), rc);
256 static int fld_insert_igif_fld(struct lu_server_fld *fld,
257 const struct lu_env *env)
263 th = fld_trans_start(fld, env, FLD_TXN_INDEX_INSERT_CREDITS);
267 rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
268 fld_trans_stop(fld, env, th);
274 int fld_index_init(struct lu_server_fld *fld,
275 const struct lu_env *env,
276 struct dt_device *dt)
278 struct dt_object *dt_obj;
283 dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
284 if (!IS_ERR(dt_obj)) {
285 fld->lsf_obj = dt_obj;
286 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
287 &fld_index_features);
289 LASSERT(dt_obj->do_index_ops != NULL);
290 rc = fld_insert_igif_fld(fld, env);
293 CERROR("insert igif in fld! = %d\n", rc);
294 lu_object_put(env, &dt_obj->do_lu);
298 CERROR("%s: File \"%s\" is not an index!\n",
299 fld->lsf_name, fld_index_name);
303 CERROR("%s: Can't find \"%s\" obj %d\n",
304 fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
305 rc = PTR_ERR(dt_obj);
311 void fld_index_fini(struct lu_server_fld *fld,
312 const struct lu_env *env)
315 if (fld->lsf_obj != NULL) {
316 if (!IS_ERR(fld->lsf_obj))
317 lu_object_put(env, &fld->lsf_obj->do_lu);