Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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, 2012, Whamcloud, Inc.
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 = 1,
69         .lsr_end   = FID_SEQ_IDIF,
70         .lsr_index   = 0,
71         .lsr_flags  = LU_SEQ_RANGE_MDT
72 };
73
74 const struct dt_index_features fld_index_features = {
75         .dif_flags       = DT_IND_UPDATE,
76         .dif_keysize_min = sizeof(seqno_t),
77         .dif_keysize_max = sizeof(seqno_t),
78         .dif_recsize_min = sizeof(struct lu_seq_range),
79         .dif_recsize_max = sizeof(struct lu_seq_range),
80         .dif_ptrsize     = 4
81 };
82
83 extern struct lu_context_key fld_thread_key;
84
85 static struct dt_key *fld_key(const struct lu_env *env,
86                               const seqno_t seq)
87 {
88         struct fld_thread_info *info;
89         ENTRY;
90
91         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
92         LASSERT(info != NULL);
93
94         info->fti_key = cpu_to_be64(seq);
95         RETURN((void *)&info->fti_key);
96 }
97
98 static struct dt_rec *fld_rec(const struct lu_env *env,
99                               const struct lu_seq_range *range)
100 {
101         struct fld_thread_info *info;
102         struct lu_seq_range *rec;
103         ENTRY;
104
105         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
106         LASSERT(info != NULL);
107         rec = &info->fti_rec;
108
109         range_cpu_to_be(rec, range);
110         RETURN((void *)rec);
111 }
112
113 struct thandle *fld_trans_create(struct lu_server_fld *fld,
114                                 const struct lu_env *env)
115 {
116         struct dt_device *dt_dev;
117
118         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
119
120         return dt_dev->dd_ops->dt_trans_create(env, dt_dev);
121 }
122
123 int fld_trans_start(struct lu_server_fld *fld,
124                                 const struct lu_env *env, struct thandle *th)
125 {
126         struct dt_device *dt_dev;
127
128         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
129
130         return dt_dev->dd_ops->dt_trans_start(env, dt_dev, th);
131 }
132
133 void fld_trans_stop(struct lu_server_fld *fld,
134                     const struct lu_env *env, struct thandle* th)
135 {
136         struct dt_device *dt_dev;
137
138         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
139         dt_dev->dd_ops->dt_trans_stop(env, th);
140 }
141
142 int fld_declare_index_create(struct lu_server_fld *fld,
143                              const struct lu_env *env,
144                              const struct lu_seq_range *range,
145                              struct thandle *th)
146 {
147         struct dt_object *dt_obj = fld->lsf_obj;
148         seqno_t start;
149         int rc;
150
151         ENTRY;
152
153         start = range->lsr_start;
154         LASSERT(range_is_sane(range));
155
156         rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
157                                                       fld_rec(env, range),
158                                                       fld_key(env, start), th);
159         RETURN(rc);
160 }
161
162 /**
163  * insert range in fld store.
164  *
165  *      \param  range  range to be inserted
166  *      \param  th     transaction for this operation as it could compound
167  *                     transaction.
168  *
169  *      \retval  0  success
170  *      \retval  -ve error
171  */
172
173 int fld_index_create(struct lu_server_fld *fld,
174                      const struct lu_env *env,
175                      const struct lu_seq_range *range,
176                      struct thandle *th)
177 {
178         struct dt_object *dt_obj = fld->lsf_obj;
179         seqno_t start;
180         int rc;
181
182         ENTRY;
183
184         start = range->lsr_start;
185         LASSERT(range_is_sane(range));
186
187         rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
188                                               fld_rec(env, range),
189                                               fld_key(env, start),
190                                               th, BYPASS_CAPA, 1);
191
192         CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
193                fld->lsf_name, PRANGE(range), rc);
194         RETURN(rc);
195 }
196
197 /**
198  * delete range in fld store.
199  *
200  *      \param  range range to be deleted
201  *      \param  th     transaction
202  *
203  *      \retval  0  success
204  *      \retval  -ve error
205  */
206
207 int fld_index_delete(struct lu_server_fld *fld,
208                      const struct lu_env *env,
209                      struct lu_seq_range *range,
210                      struct thandle   *th)
211 {
212         struct dt_object *dt_obj = fld->lsf_obj;
213         seqno_t seq = range->lsr_start;
214         int rc;
215
216         ENTRY;
217
218         rc = dt_obj->do_index_ops->dio_delete(env, dt_obj, fld_key(env, seq),
219                                               th, BYPASS_CAPA);
220
221         CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
222                fld->lsf_name, PRANGE(range), rc);
223
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
239 int fld_index_lookup(struct lu_server_fld *fld,
240                      const struct lu_env *env,
241                      seqno_t seq,
242                      struct lu_seq_range *range)
243 {
244         struct dt_object        *dt_obj = fld->lsf_obj;
245         struct lu_seq_range     *fld_rec;
246         struct dt_key           *key = fld_key(env, seq);
247         struct fld_thread_info  *info;
248         int rc;
249
250         ENTRY;
251
252         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
253         fld_rec = &info->fti_rec;
254
255         rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
256                                               (struct dt_rec*) fld_rec,
257                                               key, BYPASS_CAPA);
258
259         if (rc >= 0) {
260                 range_be_to_cpu(fld_rec, fld_rec);
261                 *range = *fld_rec;
262                 if (range_within(range, seq))
263                         rc = 0;
264                 else
265                         rc = -ENOENT;
266         }
267
268         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
269                fld->lsf_name, seq, PRANGE(range), rc);
270
271         RETURN(rc);
272 }
273
274 static int fld_insert_igif_fld(struct lu_server_fld *fld,
275                                const struct lu_env *env)
276 {
277         struct thandle *th;
278         int rc;
279         ENTRY;
280
281         /* FLD_TXN_INDEX_INSERT_CREDITS */
282         th = fld_trans_create(fld, env);
283         if (IS_ERR(th))
284                 RETURN(PTR_ERR(th));
285         rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
286         if (rc) {
287                 fld_trans_stop(fld, env, th);
288                 RETURN(rc);
289         }
290         rc = fld_trans_start(fld, env, th);
291         if (rc) {
292                 fld_trans_stop(fld, env, th);
293                 RETURN(rc);
294         }
295
296         rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
297         fld_trans_stop(fld, env, th);
298         if (rc == -EEXIST)
299                 rc = 0;
300         RETURN(rc);
301 }
302
303 int fld_index_init(struct lu_server_fld *fld,
304                    const struct lu_env *env,
305                    struct dt_device *dt)
306 {
307         struct dt_object *dt_obj;
308         struct lu_fid fid;
309         int rc;
310         ENTRY;
311
312         dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
313         if (!IS_ERR(dt_obj)) {
314                 fld->lsf_obj = dt_obj;
315                 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
316                                                   &fld_index_features);
317                 if (rc == 0) {
318                         LASSERT(dt_obj->do_index_ops != NULL);
319                         rc = fld_insert_igif_fld(fld, env);
320
321                         if (rc != 0) {
322                                 CERROR("insert igif in fld! = %d\n", rc);
323                                 lu_object_put(env, &dt_obj->do_lu);
324                                 fld->lsf_obj = NULL;
325                         }
326                 } else
327                         CERROR("%s: File \"%s\" is not an index!\n",
328                                fld->lsf_name, fld_index_name);
329
330
331         } else {
332                 CERROR("%s: Can't find \"%s\" obj %d\n",
333                        fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
334                 rc = PTR_ERR(dt_obj);
335         }
336
337         RETURN(rc);
338 }
339
340 void fld_index_fini(struct lu_server_fld *fld,
341                     const struct lu_env *env)
342 {
343         ENTRY;
344         if (fld->lsf_obj != NULL) {
345                 if (!IS_ERR(fld->lsf_obj))
346                         lu_object_put(env, &fld->lsf_obj->do_lu);
347                 fld->lsf_obj = NULL;
348         }
349         EXIT;
350 }