Whamcloud - gitweb
LU-2904 llite: use 32bitapi for re-export Lustre via NFS
[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 DOT_LUSTRE_FLD_RANGE = {
75         .lsr_start = FID_SEQ_DOT_LUSTRE,
76         .lsr_end   = FID_SEQ_DOT_LUSTRE + 1,
77         .lsr_index = 0,
78         .lsr_flags = LU_SEQ_RANGE_MDT
79 };
80
81 static const struct lu_seq_range ROOT_FLD_RANGE = {
82         .lsr_start = FID_SEQ_ROOT,
83         .lsr_end   = FID_SEQ_ROOT + 1,
84         .lsr_index = 0,
85         .lsr_flags = LU_SEQ_RANGE_MDT
86 };
87
88 const struct dt_index_features fld_index_features = {
89         .dif_flags       = DT_IND_UPDATE,
90         .dif_keysize_min = sizeof(seqno_t),
91         .dif_keysize_max = sizeof(seqno_t),
92         .dif_recsize_min = sizeof(struct lu_seq_range),
93         .dif_recsize_max = sizeof(struct lu_seq_range),
94         .dif_ptrsize     = 4
95 };
96
97 extern struct lu_context_key fld_thread_key;
98
99 int fld_declare_index_create(const struct lu_env *env,
100                              struct lu_server_fld *fld,
101                              const struct lu_seq_range *new_range,
102                              struct thandle *th)
103 {
104         struct lu_seq_range     *tmp;
105         struct lu_seq_range     *range;
106         struct fld_thread_info  *info;
107         int                     rc = 0;
108
109         ENTRY;
110
111         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
112         range = &info->fti_lrange;
113         tmp = &info->fti_irange;
114         memset(range, 0, sizeof(*range));
115
116         rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
117         if (rc == 0) {
118                 /* In case of duplicate entry, the location must be same */
119                 LASSERT((range_compare_loc(new_range, range) == 0));
120                 GOTO(out, rc = -EEXIST);
121         }
122
123         if (rc != -ENOENT) {
124                 CERROR("%s: lookup range "DRANGE" error: rc = %d\n",
125                         fld->lsf_name, PRANGE(range), rc);
126                 GOTO(out, rc);
127         }
128
129         /* Check for merge case, since the fld entry can only be increamental,
130          * so we will only check whether it can be merged from the left. */
131         if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
132             range_compare_loc(new_range, range) == 0) {
133                 range_cpu_to_be(tmp, range);
134                 rc = dt_declare_delete(env, fld->lsf_obj,
135                                        (struct dt_key *)&tmp->lsr_start, th);
136                 if (rc) {
137                         CERROR("%s: declare record "DRANGE" failed: rc = %d\n",
138                                fld->lsf_name, PRANGE(range), rc);
139                         GOTO(out, rc);
140                 }
141                 memcpy(tmp, new_range, sizeof(*new_range));
142                 tmp->lsr_start = range->lsr_start;
143         } else {
144                 memcpy(tmp, new_range, sizeof(*new_range));
145         }
146
147         range_cpu_to_be(tmp, tmp);
148         rc = dt_declare_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
149                                (struct dt_key *)&tmp->lsr_start, th);
150 out:
151         RETURN(rc);
152 }
153
154 /**
155  * insert range in fld store.
156  *
157  *      \param  range  range to be inserted
158  *      \param  th     transaction for this operation as it could compound
159  *                     transaction.
160  *
161  *      \retval  0  success
162  *      \retval  -ve error
163  *
164  * The whole fld index insertion is protected by seq->lss_mutex (see
165  * seq_server_alloc_super), i.e. only one thread will access fldb each
166  * time, so we do not need worry the fld file and cache will being
167  * changed between declare and create.
168  * Because the fld entry can only be increamental, so we will only check
169  * whether it can be merged from the left.
170  **/
171 int fld_index_create(const struct lu_env *env, struct lu_server_fld *fld,
172                      const struct lu_seq_range *new_range, struct thandle *th)
173 {
174         struct lu_seq_range     *range;
175         struct lu_seq_range     *tmp;
176         struct fld_thread_info  *info;
177         int                     rc = 0;
178         int                     deleted = 0;
179         struct fld_cache_entry  *flde;
180         ENTRY;
181
182         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
183
184         LASSERT_MUTEX_LOCKED(&fld->lsf_lock);
185
186         range = &info->fti_lrange;
187         memset(range, 0, sizeof(*range));
188         tmp = &info->fti_irange;
189         rc = fld_index_lookup(env, fld, new_range->lsr_start, range);
190         if (rc != -ENOENT) {
191                 rc = rc == 0 ? -EEXIST : rc;
192                 GOTO(out, rc);
193         }
194
195         if (new_range->lsr_start == range->lsr_end && range->lsr_end != 0 &&
196             range_compare_loc(new_range, range) == 0) {
197                 range_cpu_to_be(tmp, range);
198                 rc = dt_delete(env, fld->lsf_obj,
199                                (struct dt_key *)&tmp->lsr_start, th,
200                                 BYPASS_CAPA);
201                 if (rc != 0)
202                         GOTO(out, rc);
203                 memcpy(tmp, new_range, sizeof(*new_range));
204                 tmp->lsr_start = range->lsr_start;
205                 deleted = 1;
206         } else {
207                 memcpy(tmp, new_range, sizeof(*new_range));
208         }
209
210         range_cpu_to_be(tmp, tmp);
211         rc = dt_insert(env, fld->lsf_obj, (struct dt_rec *)tmp,
212                        (struct dt_key *)&tmp->lsr_start, th, BYPASS_CAPA, 1);
213         if (rc != 0) {
214                 CERROR("%s: insert range "DRANGE" failed: rc = %d\n",
215                        fld->lsf_name, PRANGE(new_range), rc);
216                 GOTO(out, rc);
217         }
218
219         flde = fld_cache_entry_create(new_range);
220         if (IS_ERR(flde))
221                 GOTO(out, rc = PTR_ERR(flde));
222
223         write_lock(&fld->lsf_cache->fci_lock);
224         if (deleted)
225                 fld_cache_delete_nolock(fld->lsf_cache, new_range);
226         rc = fld_cache_insert_nolock(fld->lsf_cache, flde);
227         write_unlock(&fld->lsf_cache->fci_lock);
228         if (rc)
229                 OBD_FREE_PTR(flde);
230 out:
231         RETURN(rc);
232 }
233
234 /**
235  * lookup range for a seq passed. note here we only care about the start/end,
236  * caller should handle the attached location data (flags, index).
237  *
238  * \param  seq     seq for lookup.
239  * \param  range   result of lookup.
240  *
241  * \retval  0           found, \a range is the matched range;
242  * \retval -ENOENT      not found, \a range is the left-side range;
243  * \retval  -ve         other error;
244  */
245 int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
246                      seqno_t seq, struct lu_seq_range *range)
247 {
248         struct lu_seq_range     *fld_rec;
249         struct fld_thread_info  *info;
250         int rc;
251
252         ENTRY;
253
254         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
255         fld_rec = &info->fti_rec;
256
257         rc = fld_cache_lookup(fld->lsf_cache, seq, fld_rec);
258         if (rc == 0) {
259                 *range = *fld_rec;
260                 if (range_within(range, seq))
261                         rc = 0;
262                 else
263                         rc = -ENOENT;
264         }
265
266         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
267                fld->lsf_name, seq, PRANGE(range), rc);
268
269         RETURN(rc);
270 }
271
272 int fld_insert_entry(const struct lu_env *env,
273                      struct lu_server_fld *fld,
274                      const struct lu_seq_range *range)
275 {
276         struct thandle *th;
277         int rc;
278         ENTRY;
279
280         th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
281         if (IS_ERR(th))
282                 RETURN(PTR_ERR(th));
283
284         rc = fld_declare_index_create(env, fld, range, th);
285         if (rc != 0) {
286                 if (rc == -EEXIST)
287                         rc = 0;
288                 GOTO(out, rc);
289         }
290
291         rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
292                                   th);
293         if (rc)
294                 GOTO(out, rc);
295
296         rc = fld_index_create(env, fld, range, th);
297         if (rc == -EEXIST)
298                 rc = 0;
299 out:
300         dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
301         RETURN(rc);
302 }
303 EXPORT_SYMBOL(fld_insert_entry);
304
305 static int fld_insert_special_entries(const struct lu_env *env,
306                                       struct lu_server_fld *fld)
307 {
308         int rc;
309
310         rc = fld_insert_entry(env, fld, &IGIF_FLD_RANGE);
311         if (rc != 0)
312                 RETURN(rc);
313
314         rc = fld_insert_entry(env, fld, &DOT_LUSTRE_FLD_RANGE);
315         if (rc != 0)
316                 RETURN(rc);
317
318         rc = fld_insert_entry(env, fld, &ROOT_FLD_RANGE);
319
320         RETURN(rc);
321 }
322
323 int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
324                    struct dt_device *dt)
325 {
326         struct dt_object        *dt_obj = NULL;
327         struct lu_fid           fid;
328         struct lu_attr          *attr = NULL;
329         struct lu_seq_range     *range = NULL;
330         struct fld_thread_info  *info;
331         struct dt_object_format dof;
332         struct dt_it            *it;
333         const struct dt_it_ops  *iops;
334         int                     rc;
335         ENTRY;
336
337         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
338         LASSERT(info != NULL);
339
340         lu_local_obj_fid(&fid, FLD_INDEX_OID);
341         OBD_ALLOC_PTR(attr);
342         if (attr == NULL)
343                 RETURN(-ENOMEM);
344
345         memset(attr, 0, sizeof(*attr));
346         attr->la_valid = LA_MODE;
347         attr->la_mode = S_IFREG | 0666;
348         dof.dof_type = DFT_INDEX;
349         dof.u.dof_idx.di_feat = &fld_index_features;
350
351         dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
352         if (IS_ERR(dt_obj)) {
353                 rc = PTR_ERR(dt_obj);
354                 CERROR("%s: Can't find \"%s\" obj %d\n", fld->lsf_name,
355                         fld_index_name, rc);
356                 dt_obj = NULL;
357                 GOTO(out, rc);
358         }
359
360         fld->lsf_obj = dt_obj;
361         rc = dt_obj->do_ops->do_index_try(env, dt_obj, &fld_index_features);
362         if (rc != 0) {
363                 CERROR("%s: File \"%s\" is not an index: rc = %d!\n",
364                        fld->lsf_name, fld_index_name, rc);
365                 GOTO(out, rc);
366         }
367
368         range = &info->fti_rec;
369         /* Load fld entry to cache */
370         iops = &dt_obj->do_index_ops->dio_it;
371         it = iops->init(env, dt_obj, 0, NULL);
372         if (IS_ERR(it))
373                 GOTO(out, rc = PTR_ERR(it));
374
375         rc = iops->load(env, it, 0);
376         if (rc < 0)
377                 GOTO(out_it_fini, rc);
378
379         if (rc > 0) {
380                 /* Load FLD entry into server cache */
381                 do {
382                         rc = iops->rec(env, it, (struct dt_rec *)range, 0);
383                         if (rc != 0)
384                                 GOTO(out_it_put, rc);
385                         LASSERT(range != NULL);
386                         range_be_to_cpu(range, range);
387                         rc = fld_cache_insert(fld->lsf_cache, range);
388                         if (rc != 0)
389                                 GOTO(out_it_put, rc);
390                         rc = iops->next(env, it);
391                 } while (rc == 0);
392         }
393
394         /* Note: fld_insert_entry will detect whether these
395          * special entries already exist inside FLDB */
396         mutex_lock(&fld->lsf_lock);
397         rc = fld_insert_special_entries(env, fld);
398         mutex_unlock(&fld->lsf_lock);
399         if (rc != 0) {
400                 CERROR("%s: insert special entries failed!: rc = %d\n",
401                        fld->lsf_name, rc);
402                 GOTO(out_it_put, rc);
403         }
404
405 out_it_put:
406         iops->put(env, it);
407 out_it_fini:
408         iops->fini(env, it);
409 out:
410         if (attr != NULL)
411                 OBD_FREE_PTR(attr);
412
413         if (rc != 0) {
414                 if (dt_obj != NULL)
415                         lu_object_put(env, &dt_obj->do_lu);
416                 fld->lsf_obj = NULL;
417         }
418         RETURN(rc);
419 }
420
421 void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
422 {
423         ENTRY;
424         if (fld->lsf_obj != NULL) {
425                 if (!IS_ERR(fld->lsf_obj))
426                         lu_object_put(env, &fld->lsf_obj->do_lu);
427                 fld->lsf_obj = NULL;
428         }
429         EXIT;
430 }