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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/fld/fld_index.c
33 * Author: WangDi <wangdi@clusterfs.com>
34 * Author: Yury Umanets <umka@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_FLD
39 #include <libcfs/libcfs.h>
40 #include <linux/module.h>
41 #include <obd_support.h>
42 #include <dt_object.h>
43 #include <lustre_fid.h>
44 #include <lustre_fld.h>
45 #include "fld_internal.h"
47 static const char fld_index_name[] = "fld";
49 static const struct lu_seq_range IGIF_FLD_RANGE = {
50 .lsr_start = FID_SEQ_IGIF,
51 .lsr_end = FID_SEQ_IGIF_MAX + 1,
53 .lsr_flags = LU_SEQ_RANGE_MDT
56 static const struct lu_seq_range DOT_LUSTRE_FLD_RANGE = {
57 .lsr_start = FID_SEQ_DOT_LUSTRE,
58 .lsr_end = FID_SEQ_DOT_LUSTRE + 1,
60 .lsr_flags = LU_SEQ_RANGE_MDT
63 static const struct lu_seq_range ROOT_FLD_RANGE = {
64 .lsr_start = FID_SEQ_ROOT,
65 .lsr_end = FID_SEQ_ROOT + 1,
67 .lsr_flags = LU_SEQ_RANGE_MDT
70 static const struct dt_index_features fld_index_features = {
71 .dif_flags = DT_IND_UPDATE,
72 .dif_keysize_min = sizeof(u64),
73 .dif_keysize_max = sizeof(u64),
74 .dif_recsize_min = sizeof(struct lu_seq_range),
75 .dif_recsize_max = sizeof(struct lu_seq_range),
79 int fld_declare_index_create(const struct lu_env *env,
80 struct lu_server_fld *fld,
81 const struct lu_seq_range *new_range,
84 struct lu_seq_range *tmp;
85 struct lu_seq_range *range;
86 struct fld_thread_info *info;
91 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
92 range = &info->fti_lrange;
93 tmp = &info->fti_irange;
94 memset(range, 0, sizeof(*range));
96 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
98 /* In case of duplicate entry, the location must be same */
99 LASSERT((lu_seq_range_compare_loc(new_range, range) == 0));
100 GOTO(out, rc = -EEXIST);
104 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
105 fld->lsf_name, PRANGE(range), rc);
110 * Check for merge case, since the fld entry can only be increamental,
111 * so we will only check whether it can be merged from the left.
113 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
114 lu_seq_range_compare_loc(new_range, range) == 0) {
115 range_cpu_to_be(tmp, range);
116 rc = dt_declare_delete(env, fld->lsf_obj,
117 (struct dt_key *)&tmp->lsr_start, th);
119 CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
120 fld->lsf_name, PRANGE(range), rc);
124 tmp->lsr_start = range->lsr_start;
129 range_cpu_to_be(tmp, tmp);
130 rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
131 (struct dt_key *)&tmp->lsr_start, th);
137 * insert range in fld store.
139 * \param range range to be inserted
140 * \param th transaction for this operation as it could compound
146 * The whole fld index insertion is protected by seq->lss_mutex (see
147 * seq_server_alloc_super), i.e. only one thread will access fldb each
148 * time, so we do not need worry the fld file and cache will being
149 * changed between declare and create.
150 * Because the fld entry can only be increamental, so we will only check
151 * whether it can be merged from the left.
153 * Caller must hold fld->lsf_lock
155 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
156 const struct lu_seq_range *new_range, struct thandle *th)
158 struct lu_seq_range *range;
159 struct lu_seq_range *tmp;
160 struct fld_thread_info *info;
163 struct fld_cache_entry *flde;
167 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
169 LASSERT(mutex_is_locked(&fld->lsf_lock));
171 range = &info->fti_lrange;
172 memset(range, 0, sizeof(*range));
173 tmp = &info->fti_irange;
174 rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
176 rc = rc == 0 ? -EEXIST : rc;
180 if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
181 lu_seq_range_compare_loc(new_range, range) == 0) {
182 range_cpu_to_be(tmp, range);
183 rc = dt_delete(env, fld->lsf_obj,
184 (struct dt_key *)&tmp->lsr_start, th);
188 tmp->lsr_start = range->lsr_start;
194 range_cpu_to_be(tmp, tmp);
195 rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
196 (struct dt_key *)&tmp->lsr_start, th);
198 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
199 fld->lsf_name, PRANGE(new_range), rc);
203 flde = fld_cache_entry_create(new_range);
205 GOTO(out, rc = PTR_ERR(flde));
207 write_lock(&fld->lsf_cache->fci_lock);
209 fld_cache_delete_nolock(fld->lsf_cache, new_range);
210 rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
211 write_unlock(&fld->lsf_cache->fci_lock);
219 * lookup range for a seq passed. note here we only care about the start/end,
220 * caller should handle the attached location data (flags, index).
222 * \param seq seq for lookup.
223 * \param range result of lookup.
225 * \retval 0 found, \a range is the matched range;
226 * \retval -ENOENT not found, \a range is the left-side range;
227 * \retval -ve other error;
229 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
230 u64 seq, struct lu_seq_range *range)
232 struct lu_seq_range *fld_rec;
233 struct fld_thread_info *info;
238 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
239 fld_rec = &info->fti_rec;
241 rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
244 if (lu_seq_range_within(range, seq))
250 CDEBUG(D_INFO, "%s: lookup seq = %#llx range : "DRANGE" rc = %d\n",
251 fld->lsf_name, seq, PRANGE(range), rc);
257 * insert entry in fld store.
259 * \param env relevant lu_env
260 * \param fld fld store
261 * \param range range to be inserted
266 * Caller must hold fld->lsf_lock
269 int fld_insert_entry(const struct lu_env *env,
270 struct lu_server_fld *fld,
271 const struct lu_seq_range *range)
274 struct dt_device *dt = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
279 LASSERT(mutex_is_locked(&fld->lsf_lock));
284 th = dt_trans_create(env, dt);
288 rc = fld_declare_index_create(env, fld, range, th);
295 rc = dt_trans_start_local(env, dt, th);
299 rc = fld_index_create(env, fld, range, th);
303 dt_trans_stop(env, dt, th);
306 EXPORT_SYMBOL(fld_insert_entry);
308 static int fld_insert_special_entries(const struct lu_env *env,
309 struct lu_server_fld *fld)
313 rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
317 rc = fld_insert_entry(env, fld, &DOT_LUSTRE_FLD_RANGE);
321 rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
326 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
327 struct dt_device *dt, int type)
329 struct dt_object *dt_obj = NULL;
331 struct lu_attr *attr = NULL;
332 struct lu_seq_range *range = NULL;
333 struct fld_thread_info *info;
334 struct dt_object_format dof;
336 const struct dt_it_ops *iops;
343 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
344 LASSERT(info != NULL);
346 lu_local_obj_fid(&fid, FLD_INDEX_OID);
351 memset(attr, 0, sizeof(*attr));
352 attr->la_valid = LA_MODE;
353 attr->la_mode = S_IFREG | 0666;
354 dof.dof_type = DFT_INDEX;
355 dof.u.dof_idx.di_feat = &fld_index_features;
357 dt_obj = dt_locate(env, dt, &fid);
358 if (IS_ERR(dt_obj)) {
359 rc = PTR_ERR(dt_obj);
364 LASSERT(dt_obj != NULL);
365 if (!dt_object_exists(dt_obj)) {
366 dt_object_put(env, dt_obj);
367 dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
369 if (IS_ERR(dt_obj)) {
370 rc = PTR_ERR(dt_obj);
371 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
378 fld->lsf_obj = dt_obj;
379 rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
381 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
382 fld->lsf_name, fld_index_name, rc);
386 range = &info->fti_rec;
387 /* Load fld entry to cache */
388 iops = &dt_obj->do_index_ops->dio_it;
389 it = iops->init(env, dt_obj, 0);
391 GOTO(out, rc = PTR_ERR(it));
393 rc = iops->load(env, it, 0);
397 rc = iops->next(env, it);
400 GOTO(out_it_fini, rc);
403 rc = iops->rec(env, it, (struct dt_rec *)range, 0);
405 GOTO(out_it_put, rc);
407 range_be_to_cpu(range, range);
410 * Newly created ldiskfs IAM indexes may include a
411 * zeroed-out key and record. Ignore it here.
413 if (range->lsr_start < range->lsr_end) {
414 rc = fld_cache_insert(fld->lsf_cache, range);
416 GOTO(out_it_put, rc);
421 rc = iops->next(env, it);
423 GOTO(out_it_fini, rc);
426 if (range_count == 0)
429 rc = fld_name_to_index(fld->lsf_name, &index);
431 GOTO(out_it_put, rc);
435 if (index == 0 && type == LU_SEQ_RANGE_MDT) {
437 * Note: fld_insert_entry will detect whether these
438 * special entries already exist inside FLDB
440 mutex_lock(&fld->lsf_lock);
441 rc = fld_insert_special_entries(env, fld);
442 mutex_unlock(&fld->lsf_lock);
444 CERROR("%s: insert special entries failed!: rc = %d\n",
446 GOTO(out_it_put, rc);
459 dt_object_put(env, dt_obj);
465 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
469 if (!IS_ERR(fld->lsf_obj))
470 dt_object_put(env, fld->lsf_obj);
476 int fld_server_read(const struct lu_env *env, struct lu_server_fld *fld,
477 struct lu_seq_range *range, void *data, int data_len)
479 struct lu_seq_range_array *lsra = data;
480 struct fld_thread_info *info;
481 struct dt_object *dt_obj = fld->lsf_obj;
482 struct lu_seq_range *entry;
484 const struct dt_it_ops *iops;
489 lsra->lsra_count = 0;
490 iops = &dt_obj->do_index_ops->dio_it;
491 it = iops->init(env, dt_obj, 0);
495 rc = iops->load(env, it, range->lsr_end);
497 GOTO(out_it_fini, rc);
499 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
500 LASSERT(info != NULL);
501 entry = &info->fti_rec;
503 rc = iops->rec(env, it, (struct dt_rec *)entry, 0);
505 GOTO(out_it_put, rc);
507 if (offsetof(typeof(*lsra), lsra_lsr[lsra->lsra_count + 1]) >
509 GOTO(out, rc = -EAGAIN);
511 range_be_to_cpu(entry, entry);
512 if (entry->lsr_index == range->lsr_index &&
513 entry->lsr_flags == range->lsr_flags &&
514 entry->lsr_start > range->lsr_start) {
515 lsra->lsra_lsr[lsra->lsra_count] = *entry;
519 rc = iops->next(env, it);
524 range_array_cpu_to_le(lsra, lsra);