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/fld/fld_index.c
40 * Author: WangDi <wangdi@clusterfs.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_FLD
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 # include <linux/jbd.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
57 #include <obd_class.h>
58 #include <lustre_ver.h>
59 #include <obd_support.h>
60 #include <lprocfs_status.h>
62 #include <dt_object.h>
63 #include <md_object.h>
64 #include <lustre_mdc.h>
65 #include <lustre_fid.h>
66 #include <lustre_fld.h>
67 #include "fld_internal.h"
69 const char fld_index_name[] = "fld";
71 static const struct lu_seq_range IGIF_FLD_RANGE = {
73 .lsr_end = FID_SEQ_IDIF,
75 .lsr_flags = LU_SEQ_RANGE_MDT
78 const struct dt_index_features fld_index_features = {
79 .dif_flags = DT_IND_UPDATE,
80 .dif_keysize_min = sizeof(seqno_t),
81 .dif_keysize_max = sizeof(seqno_t),
82 .dif_recsize_min = sizeof(struct lu_seq_range),
83 .dif_recsize_max = sizeof(struct lu_seq_range),
87 extern struct lu_context_key fld_thread_key;
89 static struct dt_key *fld_key(const struct lu_env *env,
92 struct fld_thread_info *info;
95 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
96 LASSERT(info != NULL);
98 info->fti_key = cpu_to_be64(seq);
99 RETURN((void *)&info->fti_key);
102 static struct dt_rec *fld_rec(const struct lu_env *env,
103 const struct lu_seq_range *range)
105 struct fld_thread_info *info;
106 struct lu_seq_range *rec;
109 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
110 LASSERT(info != NULL);
111 rec = &info->fti_rec;
113 range_cpu_to_be(rec, range);
117 struct thandle *fld_trans_create(struct lu_server_fld *fld,
118 const struct lu_env *env)
120 struct dt_device *dt_dev;
122 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
124 return dt_dev->dd_ops->dt_trans_create(env, dt_dev);
127 int fld_trans_start(struct lu_server_fld *fld,
128 const struct lu_env *env, struct thandle *th)
130 struct dt_device *dt_dev;
132 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
134 return dt_dev->dd_ops->dt_trans_start(env, dt_dev, th);
137 void fld_trans_stop(struct lu_server_fld *fld,
138 const struct lu_env *env, struct thandle* th)
140 struct dt_device *dt_dev;
142 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
143 dt_dev->dd_ops->dt_trans_stop(env, th);
146 int fld_declare_index_create(struct lu_server_fld *fld,
147 const struct lu_env *env,
148 const struct lu_seq_range *range,
151 struct dt_object *dt_obj = fld->lsf_obj;
157 start = range->lsr_start;
158 LASSERT(range_is_sane(range));
160 rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
162 fld_key(env, start), th);
167 * insert range in fld store.
169 * \param range range to be inserted
170 * \param th transaction for this operation as it could compound
177 int fld_index_create(struct lu_server_fld *fld,
178 const struct lu_env *env,
179 const struct lu_seq_range *range,
182 struct dt_object *dt_obj = fld->lsf_obj;
188 start = range->lsr_start;
189 LASSERT(range_is_sane(range));
191 rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
196 CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
197 fld->lsf_name, PRANGE(range), rc);
202 * delete range in fld store.
204 * \param range range to be deleted
205 * \param th transaction
211 int fld_index_delete(struct lu_server_fld *fld,
212 const struct lu_env *env,
213 struct lu_seq_range *range,
216 struct dt_object *dt_obj = fld->lsf_obj;
217 seqno_t seq = range->lsr_start;
222 rc = dt_obj->do_index_ops->dio_delete(env, dt_obj, fld_key(env, seq),
225 CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
226 fld->lsf_name, PRANGE(range), rc);
232 * lookup range for a seq passed. note here we only care about the start/end,
233 * caller should handle the attached location data (flags, index).
235 * \param seq seq for lookup.
236 * \param range result of lookup.
238 * \retval 0 found, \a range is the matched range;
239 * \retval -ENOENT not found, \a range is the left-side range;
240 * \retval -ve other error;
243 int fld_index_lookup(struct lu_server_fld *fld,
244 const struct lu_env *env,
246 struct lu_seq_range *range)
248 struct dt_object *dt_obj = fld->lsf_obj;
249 struct lu_seq_range *fld_rec;
250 struct dt_key *key = fld_key(env, seq);
251 struct fld_thread_info *info;
256 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
257 fld_rec = &info->fti_rec;
259 rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
260 (struct dt_rec*) fld_rec,
264 range_be_to_cpu(fld_rec, fld_rec);
266 if (range_within(range, seq))
272 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
273 fld->lsf_name, seq, PRANGE(range), rc);
278 static int fld_insert_igif_fld(struct lu_server_fld *fld,
279 const struct lu_env *env)
285 /* FLD_TXN_INDEX_INSERT_CREDITS */
286 th = fld_trans_create(fld, env);
289 rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
291 fld_trans_stop(fld, env, th);
294 rc = fld_trans_start(fld, env, th);
296 fld_trans_stop(fld, env, th);
300 rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
301 fld_trans_stop(fld, env, th);
307 int fld_index_init(struct lu_server_fld *fld,
308 const struct lu_env *env,
309 struct dt_device *dt)
311 struct dt_object *dt_obj;
316 dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
317 if (!IS_ERR(dt_obj)) {
318 fld->lsf_obj = dt_obj;
319 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
320 &fld_index_features);
322 LASSERT(dt_obj->do_index_ops != NULL);
323 rc = fld_insert_igif_fld(fld, env);
326 CERROR("insert igif in fld! = %d\n", rc);
327 lu_object_put(env, &dt_obj->do_lu);
331 CERROR("%s: File \"%s\" is not an index!\n",
332 fld->lsf_name, fld_index_name);
336 CERROR("%s: Can't find \"%s\" obj %d\n",
337 fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
338 rc = PTR_ERR(dt_obj);
344 void fld_index_fini(struct lu_server_fld *fld,
345 const struct lu_env *env)
348 if (fld->lsf_obj != NULL) {
349 if (!IS_ERR(fld->lsf_obj))
350 lu_object_put(env, &fld->lsf_obj->do_lu);