Whamcloud - gitweb
LU-1346 libcfs: cleanup macros in kp30.h
[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 #else /* __KERNEL__ */
48 # include <liblustre.h>
49 #endif
50
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"
56
57 static const char fld_index_name[] = "fld";
58
59 static const struct lu_seq_range IGIF_FLD_RANGE = {
60         .lsr_start = FID_SEQ_IGIF,
61         .lsr_end   = FID_SEQ_IGIF_MAX + 1,
62         .lsr_index = 0,
63         .lsr_flags = LU_SEQ_RANGE_MDT
64 };
65
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,
69         .lsr_index = 0,
70         .lsr_flags = LU_SEQ_RANGE_MDT
71 };
72
73 static const struct lu_seq_range ROOT_FLD_RANGE = {
74         .lsr_start = FID_SEQ_ROOT,
75         .lsr_end   = FID_SEQ_ROOT + 1,
76         .lsr_index = 0,
77         .lsr_flags = LU_SEQ_RANGE_MDT
78 };
79
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),
86         .dif_ptrsize     = 4
87 };
88
89 extern struct lu_context_key fld_thread_key;
90
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,
94                              struct thandle *th)
95 {
96         struct lu_seq_range     *tmp;
97         struct lu_seq_range     *range;
98         struct fld_thread_info  *info;
99         int                     rc = 0;
100
101         ENTRY;
102
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));
107
108         rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
109         if (rc == 0) {
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);
113         }
114
115         if (rc != -ENOENT) {
116                 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
117                         fld->lsf_name, PRANGE(range), rc);
118                 GOTO(out, rc);
119         }
120
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);
128                 if (rc) {
129                         CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
130                                fld->lsf_name, PRANGE(range), rc);
131                         GOTO(out, rc);
132                 }
133                 *tmp = *new_range;
134                 tmp->lsr_start = range->lsr_start;
135         } else {
136                 *tmp = *new_range;
137         }
138
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);
142 out:
143         RETURN(rc);
144 }
145
146 /**
147  * insert range in fld store.
148  *
149  *      \param  range  range to be inserted
150  *      \param  th     transaction for this operation as it could compound
151  *                     transaction.
152  *
153  *      \retval  0  success
154  *      \retval  -ve error
155  *
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.
162  **/
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)
165 {
166         struct lu_seq_range     *range;
167         struct lu_seq_range     *tmp;
168         struct fld_thread_info  *info;
169         int                     rc = 0;
170         int                     deleted = 0;
171         struct fld_cache_entry  *flde;
172         ENTRY;
173
174         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
175
176         LASSERT(mutex_is_locked(&fld->lsf_lock));
177
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);
182         if (rc != -ENOENT) {
183                 rc = rc == 0 ? -EEXIST : rc;
184                 GOTO(out, rc);
185         }
186
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,
192                                 BYPASS_CAPA);
193                 if (rc != 0)
194                         GOTO(out, rc);
195                 *tmp = *new_range;
196                 tmp->lsr_start = range->lsr_start;
197                 deleted = 1;
198         } else {
199                 *tmp = *new_range;
200         }
201
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);
205         if (rc != 0) {
206                 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
207                        fld->lsf_name, PRANGE(new_range), rc);
208                 GOTO(out, rc);
209         }
210
211         flde = fld_cache_entry_create(new_range);
212         if (IS_ERR(flde))
213                 GOTO(out, rc = PTR_ERR(flde));
214
215         write_lock(&fld->lsf_cache->fci_lock);
216         if (deleted)
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);
220         if (rc)
221                 OBD_FREE_PTR(flde);
222 out:
223         RETURN(rc);
224 }
225
226 /**
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).
229  *
230  * \param  seq     seq for lookup.
231  * \param  range   result of lookup.
232  *
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;
236  */
237 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
238                      seqno_t seq, struct lu_seq_range *range)
239 {
240         struct lu_seq_range     *fld_rec;
241         struct fld_thread_info  *info;
242         int rc;
243
244         ENTRY;
245
246         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
247         fld_rec = &info->fti_rec;
248
249         rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
250         if (rc == 0) {
251                 *range = *fld_rec;
252                 if (range_within(range, seq))
253                         rc = 0;
254                 else
255                         rc = -ENOENT;
256         }
257
258         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
259                fld->lsf_name, seq, PRANGE(range), rc);
260
261         RETURN(rc);
262 }
263
264 int fld_insert_entry(const struct lu_env *env,
265                      struct lu_server_fld *fld,
266                      const struct lu_seq_range *range)
267 {
268         struct thandle *th;
269         int rc;
270         ENTRY;
271
272         th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
273         if (IS_ERR(th))
274                 RETURN(PTR_ERR(th));
275
276         rc = fld_declare_index_create(env, fld, range, th);
277         if (rc != 0) {
278                 if (rc == -EEXIST)
279                         rc = 0;
280                 GOTO(out, rc);
281         }
282
283         rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
284                                   th);
285         if (rc)
286                 GOTO(out, rc);
287
288         rc = fld_index_create(env, fld, range, th);
289         if (rc == -EEXIST)
290                 rc = 0;
291 out:
292         dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
293         RETURN(rc);
294 }
295 EXPORT_SYMBOL(fld_insert_entry);
296
297 static int fld_insert_special_entries(const struct lu_env *env,
298                                       struct lu_server_fld *fld)
299 {
300         int rc;
301
302         rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
303         if (rc != 0)
304                 RETURN(rc);
305
306         rc = fld_insert_entry(env, fld, &DOT_LUSTRE_FLD_RANGE);
307         if (rc != 0)
308                 RETURN(rc);
309
310         rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
311
312         RETURN(rc);
313 }
314
315 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
316                    struct dt_device *dt)
317 {
318         struct dt_object        *dt_obj = NULL;
319         struct lu_fid           fid;
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;
324         struct dt_it            *it;
325         const struct dt_it_ops  *iops;
326         int                     rc;
327         ENTRY;
328
329         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
330         LASSERT(info != NULL);
331
332         lu_local_obj_fid(&fid, FLD_INDEX_OID);
333         OBD_ALLOC_PTR(attr);
334         if (attr == NULL)
335                 RETURN(-ENOMEM);
336
337         memset(attr, 0, sizeof(*attr));
338         attr->la_valid = LA_MODE;
339         attr->la_mode = S_IFREG | 0666;
340         dof.dof_type = DFT_INDEX;
341         dof.u.dof_idx.di_feat = &fld_index_features;
342
343         dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
344         if (IS_ERR(dt_obj)) {
345                 rc = PTR_ERR(dt_obj);
346                 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
347                         fld_index_name, rc);
348                 dt_obj = NULL;
349                 GOTO(out, rc);
350         }
351
352         fld->lsf_obj = dt_obj;
353         rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
354         if (rc != 0) {
355                 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
356                        fld->lsf_name, fld_index_name, rc);
357                 GOTO(out, rc);
358         }
359
360         range = &info->fti_rec;
361         /* Load fld entry to cache */
362         iops = &dt_obj->do_index_ops->dio_it;
363         it = iops->init(env, dt_obj, 0, NULL);
364         if (IS_ERR(it))
365                 GOTO(out, rc = PTR_ERR(it));
366
367         rc = iops->load(env, it, 0);
368         if (rc < 0)
369                 GOTO(out_it_fini, rc);
370
371         if (rc > 0) {
372                 /* Load FLD entry into server cache */
373                 do {
374                         rc = iops->rec(env, it, (struct dt_rec *)range, 0);
375                         if (rc != 0)
376                                 GOTO(out_it_put, rc);
377                         LASSERT(range != NULL);
378                         range_be_to_cpu(range, range);
379                         rc = fld_cache_insert(fld->lsf_cache, range);
380                         if (rc != 0)
381                                 GOTO(out_it_put, rc);
382                         rc = iops->next(env, it);
383                 } while (rc == 0);
384         }
385
386         /* Note: fld_insert_entry will detect whether these
387          * special entries already exist inside FLDB */
388         mutex_lock(&fld->lsf_lock);
389         rc = fld_insert_special_entries(env, fld);
390         mutex_unlock(&fld->lsf_lock);
391         if (rc != 0) {
392                 CERROR("%s: insert special entries failed!: rc = %d\n",
393                        fld->lsf_name, rc);
394                 GOTO(out_it_put, rc);
395         }
396
397 out_it_put:
398         iops->put(env, it);
399 out_it_fini:
400         iops->fini(env, it);
401 out:
402         if (attr != NULL)
403                 OBD_FREE_PTR(attr);
404
405         if (rc != 0) {
406                 if (dt_obj != NULL)
407                         lu_object_put(env, &dt_obj->do_lu);
408                 fld->lsf_obj = NULL;
409         }
410         RETURN(rc);
411 }
412
413 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
414 {
415         ENTRY;
416         if (fld->lsf_obj != NULL) {
417                 if (!IS_ERR(fld->lsf_obj))
418                         lu_object_put(env, &fld->lsf_obj->do_lu);
419                 fld->lsf_obj = NULL;
420         }
421         EXIT;
422 }