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/fld/fld_index.c
38 * Author: WangDi <wangdi@clusterfs.com>
39 * Author: Yury Umanets <umka@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_FLD
45 # include <libcfs/libcfs.h>
46 # include <linux/module.h>
47 # include <linux/jbd.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <lustre_ver.h>
55 #include <obd_support.h>
56 #include <lprocfs_status.h>
58 #include <dt_object.h>
59 #include <md_object.h>
60 #include <lustre_mdc.h>
61 #include <lustre_fid.h>
62 #include <lustre_fld.h>
63 #include "fld_internal.h"
65 const char fld_index_name[] = "fld";
67 static const struct lu_seq_range IGIF_FLD_RANGE = {
69 .lsr_end = FID_SEQ_IDIF,
71 .lsr_flags = LU_SEQ_RANGE_MDT
74 const struct dt_index_features fld_index_features = {
75 .dif_flags = DT_IND_UPDATE | DT_IND_RANGE,
76 .dif_keysize_min = sizeof(seqno_t),
77 .dif_keysize_max = sizeof(seqno_t),
78 .dif_recsize_min = sizeof(struct lu_seq_range),
79 .dif_recsize_max = sizeof(struct lu_seq_range),
83 extern struct lu_context_key fld_thread_key;
85 static struct dt_key *fld_key(const struct lu_env *env, const seqno_t seq)
87 struct fld_thread_info *info;
90 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
91 LASSERT(info != NULL);
93 info->fti_key = cpu_to_be64(seq);
94 RETURN((void *)&info->fti_key);
97 static struct dt_rec *fld_rec(const struct lu_env *env,
98 const struct lu_seq_range *range)
100 struct fld_thread_info *info;
101 struct lu_seq_range *rec;
104 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
105 LASSERT(info != NULL);
106 rec = &info->fti_rec;
108 range_cpu_to_be(rec, range);
112 struct thandle *fld_trans_create(struct lu_server_fld *fld,
113 const struct lu_env *env)
115 struct dt_device *dt_dev;
117 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
119 return dt_dev->dd_ops->dt_trans_create(env, dt_dev);
122 int fld_trans_start(struct lu_server_fld *fld,
123 const struct lu_env *env, struct thandle *th)
125 struct dt_device *dt_dev;
127 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
129 return dt_dev->dd_ops->dt_trans_start(env, dt_dev, th);
132 void fld_trans_stop(struct lu_server_fld *fld,
133 const struct lu_env *env, struct thandle* th)
135 struct dt_device *dt_dev;
137 dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
138 dt_dev->dd_ops->dt_trans_stop(env, th);
141 int fld_declare_index_create(struct lu_server_fld *fld,
142 const struct lu_env *env,
143 const struct lu_seq_range *range,
146 struct dt_object *dt_obj = fld->lsf_obj;
152 if (fld->lsf_no_range_lookup) {
153 /* Stub for underlying FS which can't lookup ranges */
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 if (fld->lsf_no_range_lookup) {
189 /* Stub for underlying FS which can't lookup ranges */
190 if (range->lsr_index != 0) {
191 CERROR("%s: FLD backend does not support range"
192 "lookups, so DNE and FIDs-on-OST are not"
193 "supported in this configuration\n",
199 start = range->lsr_start;
200 LASSERT(range_is_sane(range));
202 rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
207 CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
208 fld->lsf_name, PRANGE(range), rc);
213 * delete range in fld store.
215 * \param range range to be deleted
216 * \param th transaction
222 int fld_index_delete(struct lu_server_fld *fld,
223 const struct lu_env *env,
224 struct lu_seq_range *range,
227 struct dt_object *dt_obj = fld->lsf_obj;
228 seqno_t seq = range->lsr_start;
233 rc = dt_obj->do_index_ops->dio_delete(env, dt_obj, fld_key(env, seq),
236 CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
237 fld->lsf_name, PRANGE(range), rc);
243 * lookup range for a seq passed. note here we only care about the start/end,
244 * caller should handle the attached location data (flags, index).
246 * \param seq seq for lookup.
247 * \param range result of lookup.
249 * \retval 0 found, \a range is the matched range;
250 * \retval -ENOENT not found, \a range is the left-side range;
251 * \retval -ve other error;
254 int fld_index_lookup(struct lu_server_fld *fld,
255 const struct lu_env *env,
257 struct lu_seq_range *range)
259 struct dt_object *dt_obj = fld->lsf_obj;
260 struct lu_seq_range *fld_rec;
261 struct dt_key *key = fld_key(env, seq);
262 struct fld_thread_info *info;
267 if (fld->lsf_no_range_lookup) {
268 /* Stub for underlying FS which can't lookup ranges */
269 range->lsr_start = 0;
271 range->lsr_index = 0;
272 range->lsr_flags = LU_SEQ_RANGE_MDT;
274 range_cpu_to_be(range, range);
278 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
279 fld_rec = &info->fti_rec;
281 rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
282 (struct dt_rec*) fld_rec,
286 range_be_to_cpu(fld_rec, fld_rec);
288 if (range_within(range, seq))
294 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
295 fld->lsf_name, seq, PRANGE(range), rc);
300 static int fld_insert_igif_fld(struct lu_server_fld *fld,
301 const struct lu_env *env)
307 /* FLD_TXN_INDEX_INSERT_CREDITS */
308 th = fld_trans_create(fld, env);
311 rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
313 fld_trans_stop(fld, env, th);
316 rc = fld_trans_start(fld, env, th);
318 fld_trans_stop(fld, env, th);
322 rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
323 fld_trans_stop(fld, env, th);
329 int fld_index_init(struct lu_server_fld *fld,
330 const struct lu_env *env,
331 struct dt_device *dt)
333 struct dt_object *dt_obj;
338 dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
339 if (!IS_ERR(dt_obj)) {
340 fld->lsf_obj = dt_obj;
341 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
342 &fld_index_features);
344 LASSERT(dt_obj->do_index_ops != NULL);
345 rc = fld_insert_igif_fld(fld, env);
348 CERROR("insert igif in fld! = %d\n", rc);
349 lu_object_put(env, &dt_obj->do_lu);
352 } else if (rc == -ERANGE) {
353 CWARN("%s: File \"%s\" doesn't support range lookup, "
354 "using stub. DNE and FIDs on OST will not work "
355 "with this backend\n",
356 fld->lsf_name, fld_index_name);
358 LASSERT(dt_obj->do_index_ops == NULL);
359 fld->lsf_no_range_lookup = 1;
362 CERROR("%s: File \"%s\" is not index, rc %d!\n",
363 fld->lsf_name, fld_index_name, rc);
364 lu_object_put(env, &fld->lsf_obj->do_lu);
370 CERROR("%s: Can't find \"%s\" obj %d\n",
371 fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
372 rc = PTR_ERR(dt_obj);
378 void fld_index_fini(struct lu_server_fld *fld,
379 const struct lu_env *env)
382 if (fld->lsf_obj != NULL) {
383 if (!IS_ERR(fld->lsf_obj))
384 lu_object_put(env, &fld->lsf_obj->do_lu);