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