Whamcloud - gitweb
LU-909 osd: changes to osd api
[fs/lustre-release.git] / lustre / fld / fld_index.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  * Copyright (c) 2011 Whamcloud, Inc.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Sun Microsystems, Inc.
36  *
37  * lustre/fld/fld_index.c
38  *
39  * Author: WangDi <wangdi@clusterfs.com>
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45 #define DEBUG_SUBSYSTEM S_FLD
46
47 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 # include <linux/module.h>
50 # include <linux/jbd.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <lustre_ver.h>
58 #include <obd_support.h>
59 #include <lprocfs_status.h>
60
61 #include <dt_object.h>
62 #include <md_object.h>
63 #include <lustre_mdc.h>
64 #include <lustre_fid.h>
65 #include <lustre_fld.h>
66 #include "fld_internal.h"
67
68 const char fld_index_name[] = "fld";
69
70 static const struct lu_seq_range IGIF_FLD_RANGE = {
71         .lsr_start = 1,
72         .lsr_end   = FID_SEQ_IDIF,
73         .lsr_index   = 0,
74         .lsr_flags  = LU_SEQ_RANGE_MDT
75 };
76
77 const struct dt_index_features fld_index_features = {
78         .dif_flags       = DT_IND_UPDATE,
79         .dif_keysize_min = sizeof(seqno_t),
80         .dif_keysize_max = sizeof(seqno_t),
81         .dif_recsize_min = sizeof(struct lu_seq_range),
82         .dif_recsize_max = sizeof(struct lu_seq_range),
83         .dif_ptrsize     = 4
84 };
85
86 extern struct lu_context_key fld_thread_key;
87
88 static struct dt_key *fld_key(const struct lu_env *env,
89                               const seqno_t seq)
90 {
91         struct fld_thread_info *info;
92         ENTRY;
93
94         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
95         LASSERT(info != NULL);
96
97         info->fti_key = cpu_to_be64(seq);
98         RETURN((void *)&info->fti_key);
99 }
100
101 static struct dt_rec *fld_rec(const struct lu_env *env,
102                               const struct lu_seq_range *range)
103 {
104         struct fld_thread_info *info;
105         struct lu_seq_range *rec;
106         ENTRY;
107
108         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
109         LASSERT(info != NULL);
110         rec = &info->fti_rec;
111
112         range_cpu_to_be(rec, range);
113         RETURN((void *)rec);
114 }
115
116 struct thandle *fld_trans_create(struct lu_server_fld *fld,
117                                 const struct lu_env *env)
118 {
119         struct dt_device *dt_dev;
120
121         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
122
123         return dt_dev->dd_ops->dt_trans_create(env, dt_dev);
124 }
125
126 int fld_trans_start(struct lu_server_fld *fld,
127                                 const struct lu_env *env, struct thandle *th)
128 {
129         struct dt_device *dt_dev;
130
131         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
132
133         return dt_dev->dd_ops->dt_trans_start(env, dt_dev, th);
134 }
135
136 void fld_trans_stop(struct lu_server_fld *fld,
137                     const struct lu_env *env, struct thandle* th)
138 {
139         struct dt_device *dt_dev;
140
141         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
142         dt_dev->dd_ops->dt_trans_stop(env, th);
143 }
144
145 int fld_declare_index_create(struct lu_server_fld *fld,
146                              const struct lu_env *env,
147                              const struct lu_seq_range *range,
148                              struct thandle *th)
149 {
150         struct dt_object *dt_obj = fld->lsf_obj;
151         seqno_t start;
152         int rc;
153
154         ENTRY;
155
156         start = range->lsr_start;
157         LASSERT(range_is_sane(range));
158
159         rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
160                                                       fld_rec(env, range),
161                                                       fld_key(env, start), th);
162         RETURN(rc);
163 }
164
165 /**
166  * insert range in fld store.
167  *
168  *      \param  range  range to be inserted
169  *      \param  th     transaction for this operation as it could compound
170  *                     transaction.
171  *
172  *      \retval  0  success
173  *      \retval  -ve error
174  */
175
176 int fld_index_create(struct lu_server_fld *fld,
177                      const struct lu_env *env,
178                      const struct lu_seq_range *range,
179                      struct thandle *th)
180 {
181         struct dt_object *dt_obj = fld->lsf_obj;
182         seqno_t start;
183         int rc;
184
185         ENTRY;
186
187         start = range->lsr_start;
188         LASSERT(range_is_sane(range));
189
190         rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
191                                               fld_rec(env, range),
192                                               fld_key(env, start),
193                                               th, BYPASS_CAPA, 1);
194
195         CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
196                fld->lsf_name, PRANGE(range), rc);
197         RETURN(rc);
198 }
199
200 /**
201  * delete range in fld store.
202  *
203  *      \param  range range to be deleted
204  *      \param  th     transaction
205  *
206  *      \retval  0  success
207  *      \retval  -ve error
208  */
209
210 int fld_index_delete(struct lu_server_fld *fld,
211                      const struct lu_env *env,
212                      struct lu_seq_range *range,
213                      struct thandle   *th)
214 {
215         struct dt_object *dt_obj = fld->lsf_obj;
216         seqno_t seq = range->lsr_start;
217         int rc;
218
219         ENTRY;
220
221         rc = dt_obj->do_index_ops->dio_delete(env, dt_obj, fld_key(env, seq),
222                                               th, BYPASS_CAPA);
223
224         CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
225                fld->lsf_name, PRANGE(range), rc);
226
227         RETURN(rc);
228 }
229
230 /**
231  * lookup range for a seq passed. note here we only care about the start/end,
232  * caller should handle the attached location data (flags, index).
233  *
234  * \param  seq     seq for lookup.
235  * \param  range   result of lookup.
236  *
237  * \retval  0           found, \a range is the matched range;
238  * \retval -ENOENT      not found, \a range is the left-side range;
239  * \retval  -ve         other error;
240  */
241
242 int fld_index_lookup(struct lu_server_fld *fld,
243                      const struct lu_env *env,
244                      seqno_t seq,
245                      struct lu_seq_range *range)
246 {
247         struct dt_object        *dt_obj = fld->lsf_obj;
248         struct lu_seq_range     *fld_rec;
249         struct dt_key           *key = fld_key(env, seq);
250         struct fld_thread_info  *info;
251         int rc;
252
253         ENTRY;
254
255         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
256         fld_rec = &info->fti_rec;
257
258         rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
259                                               (struct dt_rec*) fld_rec,
260                                               key, BYPASS_CAPA);
261
262         if (rc >= 0) {
263                 range_be_to_cpu(fld_rec, fld_rec);
264                 *range = *fld_rec;
265                 if (range_within(range, seq))
266                         rc = 0;
267                 else
268                         rc = -ENOENT;
269         }
270
271         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
272                fld->lsf_name, seq, PRANGE(range), rc);
273
274         RETURN(rc);
275 }
276
277 static int fld_insert_igif_fld(struct lu_server_fld *fld,
278                                const struct lu_env *env)
279 {
280         struct thandle *th;
281         int rc;
282         ENTRY;
283
284         /* FLD_TXN_INDEX_INSERT_CREDITS */
285         th = fld_trans_create(fld, env);
286         if (IS_ERR(th))
287                 RETURN(PTR_ERR(th));
288         rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
289         if (rc) {
290                 fld_trans_stop(fld, env, th);
291                 RETURN(rc);
292         }
293         rc = fld_trans_start(fld, env, th);
294         if (rc) {
295                 fld_trans_stop(fld, env, th);
296                 RETURN(rc);
297         }
298
299         rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
300         fld_trans_stop(fld, env, th);
301         if (rc == -EEXIST)
302                 rc = 0;
303         RETURN(rc);
304 }
305
306 int fld_index_init(struct lu_server_fld *fld,
307                    const struct lu_env *env,
308                    struct dt_device *dt)
309 {
310         struct dt_object *dt_obj;
311         struct lu_fid fid;
312         int rc;
313         ENTRY;
314
315         dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
316         if (!IS_ERR(dt_obj)) {
317                 fld->lsf_obj = dt_obj;
318                 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
319                                                   &fld_index_features);
320                 if (rc == 0) {
321                         LASSERT(dt_obj->do_index_ops != NULL);
322                         rc = fld_insert_igif_fld(fld, env);
323
324                         if (rc != 0) {
325                                 CERROR("insert igif in fld! = %d\n", rc);
326                                 lu_object_put(env, &dt_obj->do_lu);
327                                 fld->lsf_obj = NULL;
328                         }
329                 } else
330                         CERROR("%s: File \"%s\" is not an index!\n",
331                                fld->lsf_name, fld_index_name);
332
333
334         } else {
335                 CERROR("%s: Can't find \"%s\" obj %d\n",
336                        fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
337                 rc = PTR_ERR(dt_obj);
338         }
339
340         RETURN(rc);
341 }
342
343 void fld_index_fini(struct lu_server_fld *fld,
344                     const struct lu_env *env)
345 {
346         ENTRY;
347         if (fld->lsf_obj != NULL) {
348                 if (!IS_ERR(fld->lsf_obj))
349                         lu_object_put(env, &fld->lsf_obj->do_lu);
350                 fld->lsf_obj = NULL;
351         }
352         EXIT;
353 }