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, 2013, Intel Corporation.
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,
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 int fld_declare_index_create(const struct lu_env *env,
86 struct lu_server_fld *fld,
87 const struct lu_seq_range *new_range,
90 struct lu_seq_range *tmp;
91 struct lu_seq_range *range;
92 struct fld_thread_info *info;
97 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
98 range = &info->fti_lrange;
99 tmp = &info->fti_irange;
100 memset(range, 0, sizeof(*range));
102 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
104 /* In case of duplicate entry, the location must be same */
105 LASSERT((range_compare_loc(new_range, range) == 0));
106 GOTO(out, rc = -EEXIST);
110 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
111 fld->lsf_name, PRANGE(range), rc);
115 /* Check for merge case, since the fld entry can only be increamental,
116 * so we will only check whether it can be merged from the left. */
117 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
118 range_compare_loc(new_range, range) == 0) {
119 range_cpu_to_be(tmp, range);
120 rc = dt_declare_delete(env, fld->lsf_obj,
121 (struct dt_key *)&tmp->lsr_start, th);
123 CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
124 fld->lsf_name, PRANGE(range), rc);
127 memcpy(tmp, new_range, sizeof(*new_range));
128 tmp->lsr_start = range->lsr_start;
130 memcpy(tmp, new_range, sizeof(*new_range));
133 range_cpu_to_be(tmp, tmp);
134 rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
135 (struct dt_key *)&tmp->lsr_start, th);
141 * insert range in fld store.
143 * \param range range to be inserted
144 * \param th transaction for this operation as it could compound
150 * The whole fld index insertion is protected by seq->lss_mutex (see
151 * seq_server_alloc_super), i.e. only one thread will access fldb each
152 * time, so we do not need worry the fld file and cache will being
153 * changed between declare and create.
154 * Because the fld entry can only be increamental, so we will only check
155 * whether it can be merged from the left.
157 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
158 const struct lu_seq_range *new_range, struct thandle *th)
160 struct lu_seq_range *range;
161 struct lu_seq_range *tmp;
162 struct fld_thread_info *info;
165 struct fld_cache_entry *flde;
168 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
170 LASSERT_MUTEX_LOCKED(&fld->lsf_lock);
172 range = &info->fti_lrange;
173 memset(range, 0, sizeof(*range));
174 tmp = &info->fti_irange;
175 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
177 rc = rc == 0 ? -EEXIST : rc;
181 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
182 range_compare_loc(new_range, range) == 0) {
183 range_cpu_to_be(tmp, range);
184 rc = dt_delete(env, fld->lsf_obj,
185 (struct dt_key *)&tmp->lsr_start, th,
189 memcpy(tmp, new_range, sizeof(*new_range));
190 tmp->lsr_start = range->lsr_start;
193 memcpy(tmp, new_range, sizeof(*new_range));
196 range_cpu_to_be(tmp, tmp);
197 rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
198 (struct dt_key *)&tmp->lsr_start, th, BYPASS_CAPA, 1);
200 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
201 fld->lsf_name, PRANGE(new_range), rc);
205 flde = fld_cache_entry_create(new_range);
207 GOTO(out, rc = PTR_ERR(flde));
209 spin_lock(&fld->lsf_cache->fci_lock);
211 fld_cache_delete_nolock(fld->lsf_cache, new_range);
212 rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
213 spin_unlock(&fld->lsf_cache->fci_lock);
221 * lookup range for a seq passed. note here we only care about the start/end,
222 * caller should handle the attached location data (flags, index).
224 * \param seq seq for lookup.
225 * \param range result of lookup.
227 * \retval 0 found, \a range is the matched range;
228 * \retval -ENOENT not found, \a range is the left-side range;
229 * \retval -ve other error;
231 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
232 seqno_t seq, struct lu_seq_range *range)
234 struct lu_seq_range *fld_rec;
235 struct fld_thread_info *info;
240 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
241 fld_rec = &info->fti_rec;
243 rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
246 if (range_within(range, seq))
252 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
253 fld->lsf_name, seq, PRANGE(range), rc);
258 static int fld_insert_igif_fld(struct lu_server_fld *fld,
259 const struct lu_env *env)
265 th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
269 rc = fld_declare_index_create(env, fld, &IGIF_FLD_RANGE, th);
276 rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
281 rc = fld_index_create(env, fld, &IGIF_FLD_RANGE, th);
285 dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
289 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
290 struct dt_device *dt)
292 struct dt_object *dt_obj = NULL;
294 struct lu_attr *attr = NULL;
295 struct lu_seq_range *range = NULL;
296 struct fld_thread_info *info;
297 struct dt_object_format dof;
299 const struct dt_it_ops *iops;
303 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
304 LASSERT(info != NULL);
306 lu_local_obj_fid(&fid, FLD_INDEX_OID);
311 memset(attr, 0, sizeof(attr));
312 attr->la_valid = LA_MODE;
313 attr->la_mode = S_IFREG | 0666;
314 dof.dof_type = DFT_INDEX;
315 dof.u.dof_idx.di_feat = &fld_index_features;
317 dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
318 if (IS_ERR(dt_obj)) {
319 rc = PTR_ERR(dt_obj);
320 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
326 fld->lsf_obj = dt_obj;
327 rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
329 LASSERT(dt_obj->do_index_ops != NULL);
330 mutex_lock(&fld->lsf_lock);
331 rc = fld_insert_igif_fld(fld, env);
332 mutex_unlock(&fld->lsf_lock);
334 CERROR("insert igif in fld! = %d\n", rc);
338 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
339 fld->lsf_name, fld_index_name, rc);
343 range = &info->fti_rec;
344 /* Load fld entry to cache */
345 iops = &dt_obj->do_index_ops->dio_it;
346 it = iops->init(env, dt_obj, 0, NULL);
348 GOTO(out, rc = PTR_ERR(it));
350 rc = iops->load(env, it, 0);
352 GOTO(out_it_fini, rc);
355 rc = iops->rec(env, it, (struct dt_rec *)range, 0);
357 GOTO(out_it_fini, rc);
359 LASSERT(range != NULL);
360 range_be_to_cpu(range, range);
361 rc = fld_cache_insert(fld->lsf_cache, range);
363 GOTO(out_it_fini, rc);
364 rc = iops->next(env, it);
377 lu_object_put(env, &dt_obj->do_lu);
383 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
386 if (fld->lsf_obj != NULL) {
387 if (!IS_ERR(fld->lsf_obj))
388 lu_object_put(env, &fld->lsf_obj->do_lu);