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 #else /* __KERNEL__ */
48 # include <liblustre.h>
51 #include <obd_support.h>
52 #include <dt_object.h>
53 #include <lustre_fid.h>
54 #include <lustre_fld.h>
55 #include "fld_internal.h"
57 static const char fld_index_name[] = "fld";
59 static const struct lu_seq_range IGIF_FLD_RANGE = {
60 .lsr_start = FID_SEQ_IGIF,
61 .lsr_end = FID_SEQ_IGIF_MAX + 1,
63 .lsr_flags = LU_SEQ_RANGE_MDT
66 static const struct lu_seq_range DOT_LUSTRE_FLD_RANGE = {
67 .lsr_start = FID_SEQ_DOT_LUSTRE,
68 .lsr_end = FID_SEQ_DOT_LUSTRE + 1,
70 .lsr_flags = LU_SEQ_RANGE_MDT
73 static const struct lu_seq_range ROOT_FLD_RANGE = {
74 .lsr_start = FID_SEQ_ROOT,
75 .lsr_end = FID_SEQ_ROOT + 1,
77 .lsr_flags = LU_SEQ_RANGE_MDT
80 static const struct dt_index_features fld_index_features = {
81 .dif_flags = DT_IND_UPDATE,
82 .dif_keysize_min = sizeof(seqno_t),
83 .dif_keysize_max = sizeof(seqno_t),
84 .dif_recsize_min = sizeof(struct lu_seq_range),
85 .dif_recsize_max = sizeof(struct lu_seq_range),
89 extern struct lu_context_key fld_thread_key;
91 int fld_declare_index_create(const struct lu_env *env,
92 struct lu_server_fld *fld,
93 const struct lu_seq_range *new_range,
96 struct lu_seq_range *tmp;
97 struct lu_seq_range *range;
98 struct fld_thread_info *info;
103 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
104 range = &info->fti_lrange;
105 tmp = &info->fti_irange;
106 memset(range, 0, sizeof(*range));
108 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
110 /* In case of duplicate entry, the location must be same */
111 LASSERT((range_compare_loc(new_range, range) == 0));
112 GOTO(out, rc = -EEXIST);
116 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
117 fld->lsf_name, PRANGE(range), rc);
121 /* Check for merge case, since the fld entry can only be increamental,
122 * so we will only check whether it can be merged from the left. */
123 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
124 range_compare_loc(new_range, range) == 0) {
125 range_cpu_to_be(tmp, range);
126 rc = dt_declare_delete(env, fld->lsf_obj,
127 (struct dt_key *)&tmp->lsr_start, th);
129 CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
130 fld->lsf_name, PRANGE(range), rc);
134 tmp->lsr_start = range->lsr_start;
139 range_cpu_to_be(tmp, tmp);
140 rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
141 (struct dt_key *)&tmp->lsr_start, th);
147 * insert range in fld store.
149 * \param range range to be inserted
150 * \param th transaction for this operation as it could compound
156 * The whole fld index insertion is protected by seq->lss_mutex (see
157 * seq_server_alloc_super), i.e. only one thread will access fldb each
158 * time, so we do not need worry the fld file and cache will being
159 * changed between declare and create.
160 * Because the fld entry can only be increamental, so we will only check
161 * whether it can be merged from the left.
163 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
164 const struct lu_seq_range *new_range, struct thandle *th)
166 struct lu_seq_range *range;
167 struct lu_seq_range *tmp;
168 struct fld_thread_info *info;
171 struct fld_cache_entry *flde;
174 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
176 LASSERT(mutex_is_locked(&fld->lsf_lock));
178 range = &info->fti_lrange;
179 memset(range, 0, sizeof(*range));
180 tmp = &info->fti_irange;
181 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
183 rc = rc == 0 ? -EEXIST : rc;
187 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
188 range_compare_loc(new_range, range) == 0) {
189 range_cpu_to_be(tmp, range);
190 rc = dt_delete(env, fld->lsf_obj,
191 (struct dt_key *)&tmp->lsr_start, th,
196 tmp->lsr_start = range->lsr_start;
202 range_cpu_to_be(tmp, tmp);
203 rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
204 (struct dt_key *)&tmp->lsr_start, th, BYPASS_CAPA, 1);
206 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
207 fld->lsf_name, PRANGE(new_range), rc);
211 flde = fld_cache_entry_create(new_range);
213 GOTO(out, rc = PTR_ERR(flde));
215 write_lock(&fld->lsf_cache->fci_lock);
217 fld_cache_delete_nolock(fld->lsf_cache, new_range);
218 rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
219 write_unlock(&fld->lsf_cache->fci_lock);
227 * lookup range for a seq passed. note here we only care about the start/end,
228 * caller should handle the attached location data (flags, index).
230 * \param seq seq for lookup.
231 * \param range result of lookup.
233 * \retval 0 found, \a range is the matched range;
234 * \retval -ENOENT not found, \a range is the left-side range;
235 * \retval -ve other error;
237 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
238 seqno_t seq, struct lu_seq_range *range)
240 struct lu_seq_range *fld_rec;
241 struct fld_thread_info *info;
246 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
247 fld_rec = &info->fti_rec;
249 rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
252 if (range_within(range, seq))
258 CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
259 fld->lsf_name, seq, PRANGE(range), rc);
264 int fld_insert_entry(const struct lu_env *env,
265 struct lu_server_fld *fld,
266 const struct lu_seq_range *range)
272 th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
276 rc = fld_declare_index_create(env, fld, range, th);
283 rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
288 rc = fld_index_create(env, fld, range, th);
292 dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
295 EXPORT_SYMBOL(fld_insert_entry);
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, &DOT_LUSTRE_FLD_RANGE);
310 rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
315 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
316 struct dt_device *dt, int type)
318 struct dt_object *dt_obj = NULL;
320 struct lu_attr *attr = NULL;
321 struct lu_seq_range *range = NULL;
322 struct fld_thread_info *info;
323 struct dt_object_format dof;
325 const struct dt_it_ops *iops;
330 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
331 LASSERT(info != NULL);
333 lu_local_obj_fid(&fid, FLD_INDEX_OID);
338 memset(attr, 0, sizeof(*attr));
339 attr->la_valid = LA_MODE;
340 attr->la_mode = S_IFREG | 0666;
341 dof.dof_type = DFT_INDEX;
342 dof.u.dof_idx.di_feat = &fld_index_features;
344 dt_obj = dt_locate(env, dt, &fid);
345 if (IS_ERR(dt_obj)) {
346 rc = PTR_ERR(dt_obj);
351 LASSERT(dt_obj != NULL);
352 if (!dt_object_exists(dt_obj)) {
353 lu_object_put(env, &dt_obj->do_lu);
354 dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
356 if (IS_ERR(dt_obj)) {
357 rc = PTR_ERR(dt_obj);
358 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
365 fld->lsf_obj = dt_obj;
366 rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
368 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
369 fld->lsf_name, fld_index_name, rc);
373 range = &info->fti_rec;
374 /* Load fld entry to cache */
375 iops = &dt_obj->do_index_ops->dio_it;
376 it = iops->init(env, dt_obj, 0, NULL);
378 GOTO(out, rc = PTR_ERR(it));
380 rc = iops->load(env, it, 0);
382 GOTO(out_it_fini, rc);
385 /* Load FLD entry into server cache */
387 rc = iops->rec(env, it, (struct dt_rec *)range, 0);
389 GOTO(out_it_put, rc);
390 LASSERT(range != NULL);
391 range_be_to_cpu(range, range);
392 rc = fld_cache_insert(fld->lsf_cache, range);
394 GOTO(out_it_put, rc);
395 rc = iops->next(env, it);
401 rc = fld_name_to_index(fld->lsf_name, &index);
403 GOTO(out_it_put, rc);
407 if (index == 0 && type == LU_SEQ_RANGE_MDT) {
408 /* Note: fld_insert_entry will detect whether these
409 * special entries already exist inside FLDB */
410 mutex_lock(&fld->lsf_lock);
411 rc = fld_insert_special_entries(env, fld);
412 mutex_unlock(&fld->lsf_lock);
414 CERROR("%s: insert special entries failed!: rc = %d\n",
416 GOTO(out_it_put, rc);
429 lu_object_put(env, &dt_obj->do_lu);
435 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
438 if (fld->lsf_obj != NULL) {
439 if (!IS_ERR(fld->lsf_obj))
440 lu_object_put(env, &fld->lsf_obj->do_lu);
446 int fld_server_read(const struct lu_env *env, struct lu_server_fld *fld,
447 struct lu_seq_range *range, void *data, int data_len)
449 struct lu_seq_range_array *lsra = data;
450 struct fld_thread_info *info;
451 struct dt_object *dt_obj = fld->lsf_obj;
452 struct lu_seq_range *entry;
454 const struct dt_it_ops *iops;
459 lsra->lsra_count = 0;
460 iops = &dt_obj->do_index_ops->dio_it;
461 it = iops->init(env, dt_obj, 0, NULL);
465 rc = iops->load(env, it, range->lsr_end);
467 GOTO(out_it_fini, rc);
469 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
470 LASSERT(info != NULL);
471 entry = &info->fti_rec;
473 rc = iops->rec(env, it, (struct dt_rec *)entry, 0);
475 GOTO(out_it_put, rc);
477 if (offsetof(typeof(*lsra), lsra_lsr[lsra->lsra_count + 1]) >
479 GOTO(out, rc = -EAGAIN);
481 range_be_to_cpu(entry, entry);
482 if (entry->lsr_index == range->lsr_index &&
483 entry->lsr_flags == range->lsr_flags &&
484 entry->lsr_start > range->lsr_start) {
485 lsra->lsra_lsr[lsra->lsra_count] = *entry;
489 rc = iops->next(env, it);
494 range_array_cpu_to_le(lsra, lsra);