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 static const struct lu_seq_range DOT_LUSTRE_FLD_RANGE = {
75 .lsr_start = FID_SEQ_DOT_LUSTRE,
76 .lsr_end = FID_SEQ_DOT_LUSTRE + 1,
78 .lsr_flags = LU_SEQ_RANGE_MDT
81 static const struct lu_seq_range ROOT_FLD_RANGE = {
82 .lsr_start = FID_SEQ_ROOT,
83 .lsr_end = FID_SEQ_ROOT + 1,
85 .lsr_flags = LU_SEQ_RANGE_MDT
88 const struct dt_index_features fld_index_features = {
89 .dif_flags = DT_IND_UPDATE,
90 .dif_keysize_min = sizeof(seqno_t),
91 .dif_keysize_max = sizeof(seqno_t),
92 .dif_recsize_min = sizeof(struct lu_seq_range),
93 .dif_recsize_max = sizeof(struct lu_seq_range),
97 extern struct lu_context_key fld_thread_key;
99 int fld_declare_index_create(const struct lu_env *env,
100 struct lu_server_fld *fld,
101 const struct lu_seq_range *new_range,
104 struct lu_seq_range *tmp;
105 struct lu_seq_range *range;
106 struct fld_thread_info *info;
111 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
112 range = &info->fti_lrange;
113 tmp = &info->fti_irange;
114 memset(range, 0, sizeof(*range));
116 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
118 /* In case of duplicate entry, the location must be same */
119 LASSERT((range_compare_loc(new_range, range) == 0));
120 GOTO(out, rc = -EEXIST);
124 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
125 fld->lsf_name, PRANGE(range), rc);
129 /* Check for merge case, since the fld entry can only be increamental,
130 * so we will only check whether it can be merged from the left. */
131 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
132 range_compare_loc(new_range, range) == 0) {
133 range_cpu_to_be(tmp, range);
134 rc = dt_declare_delete(env, fld->lsf_obj,
135 (struct dt_key *)&tmp->lsr_start, th);
137 CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
138 fld->lsf_name, PRANGE(range), rc);
141 memcpy(tmp, new_range, sizeof(*new_range));
142 tmp->lsr_start = range->lsr_start;
144 memcpy(tmp, new_range, sizeof(*new_range));
147 range_cpu_to_be(tmp, tmp);
148 rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
149 (struct dt_key *)&tmp->lsr_start, th);
155 * insert range in fld store.
157 * \param range range to be inserted
158 * \param th transaction for this operation as it could compound
164 * The whole fld index insertion is protected by seq->lss_mutex (see
165 * seq_server_alloc_super), i.e. only one thread will access fldb each
166 * time, so we do not need worry the fld file and cache will being
167 * changed between declare and create.
168 * Because the fld entry can only be increamental, so we will only check
169 * whether it can be merged from the left.
171 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
172 const struct lu_seq_range *new_range, struct thandle *th)
174 struct lu_seq_range *range;
175 struct lu_seq_range *tmp;
176 struct fld_thread_info *info;
179 struct fld_cache_entry *flde;
182 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
184 LASSERT_MUTEX_LOCKED(&fld->lsf_lock);
186 range = &info->fti_lrange;
187 memset(range, 0, sizeof(*range));
188 tmp = &info->fti_irange;
189 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
191 rc = rc == 0 ? -EEXIST : rc;
195 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
196 range_compare_loc(new_range, range) == 0) {
197 range_cpu_to_be(tmp, range);
198 rc = dt_delete(env, fld->lsf_obj,
199 (struct dt_key *)&tmp->lsr_start, th,
203 memcpy(tmp, new_range, sizeof(*new_range));
204 tmp->lsr_start = range->lsr_start;
207 memcpy(tmp, new_range, sizeof(*new_range));
210 range_cpu_to_be(tmp, tmp);
211 rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
212 (struct dt_key *)&tmp->lsr_start, th, BYPASS_CAPA, 1);
214 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
215 fld->lsf_name, PRANGE(new_range), rc);
219 flde = fld_cache_entry_create(new_range);
221 GOTO(out, rc = PTR_ERR(flde));
223 write_lock(&fld->lsf_cache->fci_lock);
225 fld_cache_delete_nolock(fld->lsf_cache, new_range);
226 rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
227 write_unlock(&fld->lsf_cache->fci_lock);
235 * lookup range for a seq passed. note here we only care about the start/end,
236 * caller should handle the attached location data (flags, index).
238 * \param seq seq for lookup.
239 * \param range result of lookup.
241 * \retval 0 found, \a range is the matched range;
242 * \retval -ENOENT not found, \a range is the left-side range;
243 * \retval -ve other error;
245 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
246 seqno_t seq, struct lu_seq_range *range)
248 struct lu_seq_range *fld_rec;
249 struct fld_thread_info *info;
254 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
255 fld_rec = &info->fti_rec;
257 rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
260 if (range_within(range, seq))
266 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
267 fld->lsf_name, seq, PRANGE(range), rc);
272 int fld_insert_entry(const struct lu_env *env,
273 struct lu_server_fld *fld,
274 const struct lu_seq_range *range)
280 th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
284 rc = fld_declare_index_create(env, fld, range, th);
291 rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
296 rc = fld_index_create(env, fld, range, th);
300 dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
303 EXPORT_SYMBOL(fld_insert_entry);
305 static int fld_insert_special_entries(const struct lu_env *env,
306 struct lu_server_fld *fld)
310 rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
314 rc = fld_insert_entry(env, fld, &DOT_LUSTRE_FLD_RANGE);
318 rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
323 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
324 struct dt_device *dt)
326 struct dt_object *dt_obj = NULL;
328 struct lu_attr *attr = NULL;
329 struct lu_seq_range *range = NULL;
330 struct fld_thread_info *info;
331 struct dt_object_format dof;
333 const struct dt_it_ops *iops;
337 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
338 LASSERT(info != NULL);
340 lu_local_obj_fid(&fid, FLD_INDEX_OID);
345 memset(attr, 0, sizeof(*attr));
346 attr->la_valid = LA_MODE;
347 attr->la_mode = S_IFREG | 0666;
348 dof.dof_type = DFT_INDEX;
349 dof.u.dof_idx.di_feat = &fld_index_features;
351 dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
352 if (IS_ERR(dt_obj)) {
353 rc = PTR_ERR(dt_obj);
354 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
360 fld->lsf_obj = dt_obj;
361 rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
363 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
364 fld->lsf_name, fld_index_name, rc);
368 range = &info->fti_rec;
369 /* Load fld entry to cache */
370 iops = &dt_obj->do_index_ops->dio_it;
371 it = iops->init(env, dt_obj, 0, NULL);
373 GOTO(out, rc = PTR_ERR(it));
375 rc = iops->load(env, it, 0);
377 GOTO(out_it_fini, rc);
380 /* Load FLD entry into server cache */
382 rc = iops->rec(env, it, (struct dt_rec *)range, 0);
384 GOTO(out_it_put, rc);
385 LASSERT(range != NULL);
386 range_be_to_cpu(range, range);
387 rc = fld_cache_insert(fld->lsf_cache, range);
389 GOTO(out_it_put, rc);
390 rc = iops->next(env, it);
394 /* Note: fld_insert_entry will detect whether these
395 * special entries already exist inside FLDB */
396 mutex_lock(&fld->lsf_lock);
397 rc = fld_insert_special_entries(env, fld);
398 mutex_unlock(&fld->lsf_lock);
400 CERROR("%s: insert special entries failed!: rc = %d\n",
402 GOTO(out_it_put, rc);
415 lu_object_put(env, &dt_obj->do_lu);
421 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
424 if (fld->lsf_obj != NULL) {
425 if (!IS_ERR(fld->lsf_obj))
426 lu_object_put(env, &fld->lsf_obj->do_lu);