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 ROOT_FLD_RANGE = {
75 .lsr_start = FID_SEQ_ROOT,
76 .lsr_end = FID_SEQ_ROOT + 1,
78 .lsr_flags = LU_SEQ_RANGE_MDT
81 const struct dt_index_features fld_index_features = {
82 .dif_flags = DT_IND_UPDATE,
83 .dif_keysize_min = sizeof(seqno_t),
84 .dif_keysize_max = sizeof(seqno_t),
85 .dif_recsize_min = sizeof(struct lu_seq_range),
86 .dif_recsize_max = sizeof(struct lu_seq_range),
90 extern struct lu_context_key fld_thread_key;
92 int fld_declare_index_create(const struct lu_env *env,
93 struct lu_server_fld *fld,
94 const struct lu_seq_range *new_range,
97 struct lu_seq_range *tmp;
98 struct lu_seq_range *range;
99 struct fld_thread_info *info;
104 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
105 range = &info->fti_lrange;
106 tmp = &info->fti_irange;
107 memset(range, 0, sizeof(*range));
109 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
111 /* In case of duplicate entry, the location must be same */
112 LASSERT((range_compare_loc(new_range, range) == 0));
113 GOTO(out, rc = -EEXIST);
117 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
118 fld->lsf_name, PRANGE(range), rc);
122 /* Check for merge case, since the fld entry can only be increamental,
123 * so we will only check whether it can be merged from the left. */
124 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
125 range_compare_loc(new_range, range) == 0) {
126 range_cpu_to_be(tmp, range);
127 rc = dt_declare_delete(env, fld->lsf_obj,
128 (struct dt_key *)&tmp->lsr_start, th);
130 CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
131 fld->lsf_name, PRANGE(range), rc);
134 memcpy(tmp, new_range, sizeof(*new_range));
135 tmp->lsr_start = range->lsr_start;
137 memcpy(tmp, new_range, sizeof(*new_range));
140 range_cpu_to_be(tmp, tmp);
141 rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
142 (struct dt_key *)&tmp->lsr_start, th);
148 * insert range in fld store.
150 * \param range range to be inserted
151 * \param th transaction for this operation as it could compound
157 * The whole fld index insertion is protected by seq->lss_mutex (see
158 * seq_server_alloc_super), i.e. only one thread will access fldb each
159 * time, so we do not need worry the fld file and cache will being
160 * changed between declare and create.
161 * Because the fld entry can only be increamental, so we will only check
162 * whether it can be merged from the left.
164 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
165 const struct lu_seq_range *new_range, struct thandle *th)
167 struct lu_seq_range *range;
168 struct lu_seq_range *tmp;
169 struct fld_thread_info *info;
172 struct fld_cache_entry *flde;
175 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
177 LASSERT_MUTEX_LOCKED(&fld->lsf_lock);
179 range = &info->fti_lrange;
180 memset(range, 0, sizeof(*range));
181 tmp = &info->fti_irange;
182 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
184 rc = rc == 0 ? -EEXIST : rc;
188 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
189 range_compare_loc(new_range, range) == 0) {
190 range_cpu_to_be(tmp, range);
191 rc = dt_delete(env, fld->lsf_obj,
192 (struct dt_key *)&tmp->lsr_start, th,
196 memcpy(tmp, new_range, sizeof(*new_range));
197 tmp->lsr_start = range->lsr_start;
200 memcpy(tmp, new_range, sizeof(*new_range));
203 range_cpu_to_be(tmp, tmp);
204 rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
205 (struct dt_key *)&tmp->lsr_start, th, BYPASS_CAPA, 1);
207 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
208 fld->lsf_name, PRANGE(new_range), rc);
212 flde = fld_cache_entry_create(new_range);
214 GOTO(out, rc = PTR_ERR(flde));
216 write_lock(&fld->lsf_cache->fci_lock);
218 fld_cache_delete_nolock(fld->lsf_cache, new_range);
219 rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
220 write_unlock(&fld->lsf_cache->fci_lock);
228 * lookup range for a seq passed. note here we only care about the start/end,
229 * caller should handle the attached location data (flags, index).
231 * \param seq seq for lookup.
232 * \param range result of lookup.
234 * \retval 0 found, \a range is the matched range;
235 * \retval -ENOENT not found, \a range is the left-side range;
236 * \retval -ve other error;
238 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
239 seqno_t seq, struct lu_seq_range *range)
241 struct lu_seq_range *fld_rec;
242 struct fld_thread_info *info;
247 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
248 fld_rec = &info->fti_rec;
250 rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
253 if (range_within(range, seq))
259 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
260 fld->lsf_name, seq, PRANGE(range), rc);
265 static int fld_insert_entry(const struct lu_env *env,
266 struct lu_server_fld *fld,
267 const struct lu_seq_range *range)
273 th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
277 rc = fld_declare_index_create(env, fld, range, th);
284 rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
289 rc = fld_index_create(env, fld, range, th);
293 dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
297 static int fld_insert_special_entries(const struct lu_env *env,
298 struct lu_server_fld *fld)
302 rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
306 rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
311 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
312 struct dt_device *dt)
314 struct dt_object *dt_obj = NULL;
316 struct lu_attr *attr = NULL;
317 struct lu_seq_range *range = NULL;
318 struct fld_thread_info *info;
319 struct dt_object_format dof;
321 const struct dt_it_ops *iops;
325 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
326 LASSERT(info != NULL);
328 lu_local_obj_fid(&fid, FLD_INDEX_OID);
333 memset(attr, 0, sizeof(attr));
334 attr->la_valid = LA_MODE;
335 attr->la_mode = S_IFREG | 0666;
336 dof.dof_type = DFT_INDEX;
337 dof.u.dof_idx.di_feat = &fld_index_features;
339 dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
340 if (IS_ERR(dt_obj)) {
341 rc = PTR_ERR(dt_obj);
342 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
348 fld->lsf_obj = dt_obj;
349 rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
351 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
352 fld->lsf_name, fld_index_name, rc);
356 range = &info->fti_rec;
357 /* Load fld entry to cache */
358 iops = &dt_obj->do_index_ops->dio_it;
359 it = iops->init(env, dt_obj, 0, NULL);
361 GOTO(out, rc = PTR_ERR(it));
363 rc = iops->load(env, it, 0);
365 GOTO(out_it_fini, rc);
368 /* Load FLD entry into server cache */
370 rc = iops->rec(env, it, (struct dt_rec *)range, 0);
372 GOTO(out_it_put, rc);
373 LASSERT(range != NULL);
374 range_be_to_cpu(range, range);
375 rc = fld_cache_insert(fld->lsf_cache, range);
377 GOTO(out_it_put, rc);
378 rc = iops->next(env, it);
382 /* Note: fld_insert_entry will detect whether these
383 * special entries already exist inside FLDB */
384 mutex_lock(&fld->lsf_lock);
385 rc = fld_insert_special_entries(env, fld);
386 mutex_unlock(&fld->lsf_lock);
388 CERROR("%s: insert special entries failed!: rc = %d\n",
390 GOTO(out_it_put, rc);
403 lu_object_put(env, &dt_obj->do_lu);
409 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
412 if (fld->lsf_obj != NULL) {
413 if (!IS_ERR(fld->lsf_obj))
414 lu_object_put(env, &fld->lsf_obj->do_lu);