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 = {
68 .lsr_start = FID_SEQ_IGIF,
69 .lsr_end = FID_SEQ_IGIF_MAX + 1,
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 int fld_declare_index_create(struct lu_server_fld *fld,
113 const struct lu_env *env,
114 const struct lu_seq_range *range,
121 if (fld->lsf_no_range_lookup) {
122 /* Stub for underlying FS which can't lookup ranges */
126 LASSERT(range_is_sane(range));
128 rc = dt_declare_insert(env, fld->lsf_obj, fld_rec(env, range),
129 fld_key(env, range->lsr_start), th);
134 * insert range in fld store.
136 * \param range range to be inserted
137 * \param th transaction for this operation as it could compound
143 int fld_index_create(struct lu_server_fld *fld,
144 const struct lu_env *env,
145 const struct lu_seq_range *range,
152 if (fld->lsf_no_range_lookup) {
153 /* Stub for underlying FS which can't lookup ranges */
154 if (range->lsr_index != 0) {
155 CERROR("%s: FLD backend does not support range"
156 "lookups, so DNE and FIDs-on-OST are not"
157 "supported in this configuration\n",
163 LASSERT(range_is_sane(range));
165 rc = dt_insert(env, fld->lsf_obj, fld_rec(env, range),
166 fld_key(env, range->lsr_start), th, BYPASS_CAPA, 1);
167 CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
168 fld->lsf_name, PRANGE(range), rc);
173 * delete range in fld store.
175 * \param range range to be deleted
176 * \param th transaction
181 int fld_index_delete(struct lu_server_fld *fld,
182 const struct lu_env *env,
183 struct lu_seq_range *range,
190 rc = dt_delete(env, fld->lsf_obj, fld_key(env, range->lsr_start), th,
192 CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
193 fld->lsf_name, PRANGE(range), rc);
198 * lookup range for a seq passed. note here we only care about the start/end,
199 * caller should handle the attached location data (flags, index).
201 * \param seq seq for lookup.
202 * \param range result of lookup.
204 * \retval 0 found, \a range is the matched range;
205 * \retval -ENOENT not found, \a range is the left-side range;
206 * \retval -ve other error;
209 int fld_index_lookup(struct lu_server_fld *fld,
210 const struct lu_env *env,
212 struct lu_seq_range *range)
214 struct dt_object *dt_obj = fld->lsf_obj;
215 struct lu_seq_range *fld_rec;
216 struct dt_key *key = fld_key(env, seq);
217 struct fld_thread_info *info;
222 if (fld->lsf_no_range_lookup) {
223 /* Stub for underlying FS which can't lookup ranges */
224 range->lsr_start = 0;
226 range->lsr_index = 0;
227 range->lsr_flags = LU_SEQ_RANGE_MDT;
229 range_cpu_to_be(range, range);
233 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
234 fld_rec = &info->fti_rec;
236 rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
237 (struct dt_rec*) fld_rec,
241 range_be_to_cpu(fld_rec, fld_rec);
243 if (range_within(range, seq))
249 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
250 fld->lsf_name, seq, PRANGE(range), rc);
255 static int fld_insert_igif_fld(struct lu_server_fld *fld,
256 const struct lu_env *env)
262 th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
265 rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
269 rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
274 rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
278 dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
282 int fld_index_init(struct lu_server_fld *fld,
283 const struct lu_env *env,
284 struct dt_device *dt)
286 struct dt_object *dt_obj;
289 struct dt_object_format dof;
293 lu_local_obj_fid(&fid, FLD_INDEX_OID);
295 memset(&attr, 0, sizeof(attr));
296 attr.la_valid = LA_MODE;
297 attr.la_mode = S_IFREG | 0666;
298 dof.dof_type = DFT_INDEX;
299 dof.u.dof_idx.di_feat = &fld_index_features;
301 dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
302 if (!IS_ERR(dt_obj)) {
303 fld->lsf_obj = dt_obj;
304 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
305 &fld_index_features);
307 LASSERT(dt_obj->do_index_ops != NULL);
308 rc = fld_insert_igif_fld(fld, env);
311 CERROR("insert igif in fld! = %d\n", rc);
312 lu_object_put(env, &dt_obj->do_lu);
315 } else if (rc == -ERANGE) {
316 CWARN("%s: File \"%s\" doesn't support range lookup, "
317 "using stub. DNE and FIDs on OST will not work "
318 "with this backend\n",
319 fld->lsf_name, fld_index_name);
321 LASSERT(dt_obj->do_index_ops == NULL);
322 fld->lsf_no_range_lookup = 1;
325 CERROR("%s: File \"%s\" is not index, rc %d!\n",
326 fld->lsf_name, fld_index_name, rc);
327 lu_object_put(env, &fld->lsf_obj->do_lu);
333 CERROR("%s: Can't find \"%s\" obj %d\n",
334 fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
335 rc = PTR_ERR(dt_obj);
341 void fld_index_fini(struct lu_server_fld *fld,
342 const struct lu_env *env)
345 if (fld->lsf_obj != NULL) {
346 if (!IS_ERR(fld->lsf_obj))
347 lu_object_put(env, &fld->lsf_obj->do_lu);