Whamcloud - gitweb
LU-3068 build: fix 'incorrect expression' errors
[fs/lustre-release.git] / lustre / fld / fld_index.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fld/fld_index.c
37  *
38  * Author: WangDi <wangdi@clusterfs.com>
39  * Author: Yury Umanets <umka@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_FLD
43
44 #ifdef __KERNEL__
45 # include <libcfs/libcfs.h>
46 # include <linux/module.h>
47 # include <linux/jbd.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
50 #endif
51
52 #include <obd.h>
53 #include <obd_class.h>
54 #include <lustre_ver.h>
55 #include <obd_support.h>
56 #include <lprocfs_status.h>
57
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"
64
65 const char fld_index_name[] = "fld";
66
67 static const struct lu_seq_range IGIF_FLD_RANGE = {
68         .lsr_start = FID_SEQ_IGIF,
69         .lsr_end   = FID_SEQ_IGIF_MAX + 1,
70         .lsr_index = 0,
71         .lsr_flags = LU_SEQ_RANGE_MDT
72 };
73
74 static const struct lu_seq_range ROOT_FLD_RANGE = {
75         .lsr_start = FID_SEQ_ROOT,
76         .lsr_end   = FID_SEQ_ROOT + 1,
77         .lsr_index = 0,
78         .lsr_flags = LU_SEQ_RANGE_MDT
79 };
80
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),
87         .dif_ptrsize     = 4
88 };
89
90 extern struct lu_context_key fld_thread_key;
91
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,
95                              struct thandle *th)
96 {
97         struct lu_seq_range     *tmp;
98         struct lu_seq_range     *range;
99         struct fld_thread_info  *info;
100         int                     rc = 0;
101
102         ENTRY;
103
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));
108
109         rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
110         if (rc == 0) {
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);
114         }
115
116         if (rc != -ENOENT) {
117                 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
118                         fld->lsf_name, PRANGE(range), rc);
119                 GOTO(out, rc);
120         }
121
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);
129                 if (rc) {
130                         CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
131                                fld->lsf_name, PRANGE(range), rc);
132                         GOTO(out, rc);
133                 }
134                 memcpy(tmp, new_range, sizeof(*new_range));
135                 tmp->lsr_start = range->lsr_start;
136         } else {
137                 memcpy(tmp, new_range, sizeof(*new_range));
138         }
139
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);
143 out:
144         RETURN(rc);
145 }
146
147 /**
148  * insert range in fld store.
149  *
150  *      \param  range  range to be inserted
151  *      \param  th     transaction for this operation as it could compound
152  *                     transaction.
153  *
154  *      \retval  0  success
155  *      \retval  -ve error
156  *
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.
163  **/
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)
166 {
167         struct lu_seq_range     *range;
168         struct lu_seq_range     *tmp;
169         struct fld_thread_info  *info;
170         int                     rc = 0;
171         int                     deleted = 0;
172         struct fld_cache_entry  *flde;
173         ENTRY;
174
175         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
176
177         LASSERT_MUTEX_LOCKED(&fld->lsf_lock);
178
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);
183         if (rc != -ENOENT) {
184                 rc = rc == 0 ? -EEXIST : rc;
185                 GOTO(out, rc);
186         }
187
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,
193                                 BYPASS_CAPA);
194                 if (rc != 0)
195                         GOTO(out, rc);
196                 memcpy(tmp, new_range, sizeof(*new_range));
197                 tmp->lsr_start = range->lsr_start;
198                 deleted = 1;
199         } else {
200                 memcpy(tmp, new_range, sizeof(*new_range));
201         }
202
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);
206         if (rc != 0) {
207                 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
208                        fld->lsf_name, PRANGE(new_range), rc);
209                 GOTO(out, rc);
210         }
211
212         flde = fld_cache_entry_create(new_range);
213         if (IS_ERR(flde))
214                 GOTO(out, rc = PTR_ERR(flde));
215
216         write_lock(&fld->lsf_cache->fci_lock);
217         if (deleted)
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);
221         if (rc)
222                 OBD_FREE_PTR(flde);
223 out:
224         RETURN(rc);
225 }
226
227 /**
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).
230  *
231  * \param  seq     seq for lookup.
232  * \param  range   result of lookup.
233  *
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;
237  */
238 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
239                      seqno_t seq, struct lu_seq_range *range)
240 {
241         struct lu_seq_range     *fld_rec;
242         struct fld_thread_info  *info;
243         int rc;
244
245         ENTRY;
246
247         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
248         fld_rec = &info->fti_rec;
249
250         rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
251         if (rc == 0) {
252                 *range = *fld_rec;
253                 if (range_within(range, seq))
254                         rc = 0;
255                 else
256                         rc = -ENOENT;
257         }
258
259         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
260                fld->lsf_name, seq, PRANGE(range), rc);
261
262         RETURN(rc);
263 }
264
265 int fld_insert_entry(const struct lu_env *env,
266                      struct lu_server_fld *fld,
267                      const struct lu_seq_range *range)
268 {
269         struct thandle *th;
270         int rc;
271         ENTRY;
272
273         th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
274         if (IS_ERR(th))
275                 RETURN(PTR_ERR(th));
276
277         rc = fld_declare_index_create(env, fld, range, th);
278         if (rc != 0) {
279                 if (rc == -EEXIST)
280                         rc = 0;
281                 GOTO(out, rc);
282         }
283
284         rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
285                                   th);
286         if (rc)
287                 GOTO(out, rc);
288
289         rc = fld_index_create(env, fld, range, th);
290         if (rc == -EEXIST)
291                 rc = 0;
292 out:
293         dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
294         RETURN(rc);
295 }
296 EXPORT_SYMBOL(fld_insert_entry);
297
298 static int fld_insert_special_entries(const struct lu_env *env,
299                                       struct lu_server_fld *fld)
300 {
301         int rc;
302
303         rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
304         if (rc != 0)
305                 RETURN(rc);
306
307         rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
308
309         RETURN(rc);
310 }
311
312 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
313                    struct dt_device *dt)
314 {
315         struct dt_object        *dt_obj = NULL;
316         struct lu_fid           fid;
317         struct lu_attr          *attr = NULL;
318         struct lu_seq_range     *range = NULL;
319         struct fld_thread_info  *info;
320         struct dt_object_format dof;
321         struct dt_it            *it;
322         const struct dt_it_ops  *iops;
323         int                     rc;
324         ENTRY;
325
326         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
327         LASSERT(info != NULL);
328
329         lu_local_obj_fid(&fid, FLD_INDEX_OID);
330         OBD_ALLOC_PTR(attr);
331         if (attr == NULL)
332                 RETURN(-ENOMEM);
333
334         memset(attr, 0, sizeof(*attr));
335         attr->la_valid = LA_MODE;
336         attr->la_mode = S_IFREG | 0666;
337         dof.dof_type = DFT_INDEX;
338         dof.u.dof_idx.di_feat = &fld_index_features;
339
340         dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
341         if (IS_ERR(dt_obj)) {
342                 rc = PTR_ERR(dt_obj);
343                 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
344                         fld_index_name, rc);
345                 dt_obj = NULL;
346                 GOTO(out, rc);
347         }
348
349         fld->lsf_obj = dt_obj;
350         rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
351         if (rc != 0) {
352                 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
353                        fld->lsf_name, fld_index_name, rc);
354                 GOTO(out, rc);
355         }
356
357         range = &info->fti_rec;
358         /* Load fld entry to cache */
359         iops = &dt_obj->do_index_ops->dio_it;
360         it = iops->init(env, dt_obj, 0, NULL);
361         if (IS_ERR(it))
362                 GOTO(out, rc = PTR_ERR(it));
363
364         rc = iops->load(env, it, 0);
365         if (rc < 0)
366                 GOTO(out_it_fini, rc);
367
368         if (rc > 0) {
369                 /* Load FLD entry into server cache */
370                 do {
371                         rc = iops->rec(env, it, (struct dt_rec *)range, 0);
372                         if (rc != 0)
373                                 GOTO(out_it_put, rc);
374                         LASSERT(range != NULL);
375                         range_be_to_cpu(range, range);
376                         rc = fld_cache_insert(fld->lsf_cache, range);
377                         if (rc != 0)
378                                 GOTO(out_it_put, rc);
379                         rc = iops->next(env, it);
380                 } while (rc == 0);
381         }
382
383         /* Note: fld_insert_entry will detect whether these
384          * special entries already exist inside FLDB */
385         mutex_lock(&fld->lsf_lock);
386         rc = fld_insert_special_entries(env, fld);
387         mutex_unlock(&fld->lsf_lock);
388         if (rc != 0) {
389                 CERROR("%s: insert special entries failed!: rc = %d\n",
390                        fld->lsf_name, rc);
391                 GOTO(out_it_put, rc);
392         }
393
394 out_it_put:
395         iops->put(env, it);
396 out_it_fini:
397         iops->fini(env, it);
398 out:
399         if (attr != NULL)
400                 OBD_FREE_PTR(attr);
401
402         if (rc != 0) {
403                 if (dt_obj != NULL)
404                         lu_object_put(env, &dt_obj->do_lu);
405                 fld->lsf_obj = NULL;
406         }
407         RETURN(rc);
408 }
409
410 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
411 {
412         ENTRY;
413         if (fld->lsf_obj != NULL) {
414                 if (!IS_ERR(fld->lsf_obj))
415                         lu_object_put(env, &fld->lsf_obj->do_lu);
416                 fld->lsf_obj = NULL;
417         }
418         EXIT;
419 }