Whamcloud - gitweb
66d29b5b2e9efec444c34c0318480489eed5f3d5
[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  */
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 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_FLD
45
46 #ifdef __KERNEL__
47 # include <libcfs/libcfs.h>
48 # include <linux/module.h>
49 # include <linux/jbd.h>
50 #else /* __KERNEL__ */
51 # include <liblustre.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
59
60 #include <dt_object.h>
61 #include <md_object.h>
62 #include <lustre_mdc.h>
63 #include <lustre_fid.h>
64 #include <lustre_fld.h>
65 #include "fld_internal.h"
66
67 const char fld_index_name[] = "fld";
68
69 static const struct lu_seq_range IGIF_FLD_RANGE = {
70         .lsr_start = 1,
71         .lsr_end   = FID_SEQ_IDIF,
72         .lsr_mdt   = 0
73 };
74
75 const struct dt_index_features fld_index_features = {
76         .dif_flags       = DT_IND_UPDATE,
77         .dif_keysize_min = sizeof(seqno_t),
78         .dif_keysize_max = sizeof(seqno_t),
79         .dif_recsize_min = sizeof(struct lu_seq_range),
80         .dif_recsize_max = sizeof(struct lu_seq_range),
81         .dif_ptrsize     = 4
82 };
83
84 extern struct lu_context_key fld_thread_key;
85
86 static struct dt_key *fld_key(const struct lu_env *env,
87                               const seqno_t seq)
88 {
89         struct fld_thread_info *info;
90         ENTRY;
91
92         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
93         LASSERT(info != NULL);
94
95         info->fti_key = cpu_to_be64(seq);
96         RETURN((void *)&info->fti_key);
97 }
98
99 static struct dt_rec *fld_rec(const struct lu_env *env,
100                               const struct lu_seq_range *range)
101 {
102         struct fld_thread_info *info;
103         struct lu_seq_range *rec;
104         ENTRY;
105
106         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
107         LASSERT(info != NULL);
108         rec = &info->fti_rec;
109
110         range_cpu_to_be(rec, range);
111         RETURN((void *)rec);
112 }
113
114 struct thandle* fld_trans_start(struct lu_server_fld *fld,
115                                 const struct lu_env *env, int credit)
116 {
117         struct fld_thread_info *info;
118         struct dt_device *dt_dev;
119         struct txn_param *p;
120
121         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
122         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
123         p = &info->fti_txn_param;
124         txn_param_init(p, credit);
125
126         return dt_dev->dd_ops->dt_trans_start(env, dt_dev, p);
127 }
128
129 void fld_trans_stop(struct lu_server_fld *fld,
130                     const struct lu_env *env, struct thandle* th)
131 {
132         struct dt_device *dt_dev;
133
134         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
135         dt_dev->dd_ops->dt_trans_stop(env, th);
136 }
137
138 /**
139  * insert range in fld store.
140  *
141  *      \param  range  range to be inserted
142  *      \param  th     transaction for this operation as it could compound
143  *                     transaction.
144  *
145  *      \retval  0  success
146  *      \retval  -ve error
147  */
148
149 int fld_index_create(struct lu_server_fld *fld,
150                      const struct lu_env *env,
151                      const struct lu_seq_range *range,
152                      struct thandle *th)
153 {
154         struct dt_object *dt_obj = fld->lsf_obj;
155         struct dt_device *dt_dev;
156         seqno_t start;
157         int rc;
158
159         ENTRY;
160
161         start = range->lsr_start;
162         LASSERT(range_is_sane(range));
163         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
164
165         rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
166                                               fld_rec(env, range),
167                                               fld_key(env, start),
168                                               th, BYPASS_CAPA, 1);
169
170         CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
171                fld->lsf_name, PRANGE(range), rc);
172         RETURN(rc);
173 }
174
175 /**
176  * delete range in fld store.
177  *
178  *      \param  range range to be deleted
179  *      \param  th     transaction
180  *
181  *      \retval  0  success
182  *      \retval  -ve error
183  */
184
185 int fld_index_delete(struct lu_server_fld *fld,
186                      const struct lu_env *env,
187                      struct lu_seq_range *range,
188                      struct thandle   *th)
189 {
190         struct dt_object *dt_obj = fld->lsf_obj;
191         struct dt_device *dt_dev;
192         seqno_t seq = range->lsr_start;
193         int rc;
194
195         ENTRY;
196
197         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
198         rc = dt_obj->do_index_ops->dio_delete(env, dt_obj,
199                                               fld_key(env, seq), th,
200                                               BYPASS_CAPA);
201
202         CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
203                fld->lsf_name, PRANGE(range), rc);
204
205         RETURN(rc);
206 }
207
208 /**
209  * lookup range for a seq passed
210  *
211  *      \param  seq     seq for lookup.
212  *      \param  range   result of lookup.
213  *
214  *      \retval  0  success
215  *      \retval  -ve error
216  */
217
218 int fld_index_lookup(struct lu_server_fld *fld,
219                      const struct lu_env *env,
220                      seqno_t seq,
221                      struct lu_seq_range *range)
222 {
223         struct dt_object        *dt_obj = fld->lsf_obj;
224         struct lu_seq_range     *fld_rec;
225         struct dt_key           *key = fld_key(env, seq);
226         struct fld_thread_info  *info;
227         int rc;
228
229         ENTRY;
230
231         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
232         fld_rec = &info->fti_rec;
233
234         rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
235                                               (struct dt_rec*) fld_rec,
236                                               key, BYPASS_CAPA);
237
238         if (rc >= 0) {
239                 range_be_to_cpu(fld_rec, fld_rec);
240                 *range = *fld_rec;
241                 if (range_within(range, seq))
242                         rc = 0;
243                 else
244                         rc = -ENOENT;
245         }
246
247         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
248                fld->lsf_name, seq, PRANGE(range), rc);
249
250         RETURN(rc);
251 }
252
253 static int fld_insert_igif_fld(struct lu_server_fld *fld,
254                                const struct lu_env *env)
255 {
256         struct thandle *th;
257         int rc;
258
259         ENTRY;
260         th = fld_trans_start(fld, env, FLD_TXN_INDEX_INSERT_CREDITS);
261         if (IS_ERR(th))
262                 RETURN(PTR_ERR(th));
263
264         rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
265         fld_trans_stop(fld, env, th);
266         if (rc == -EEXIST)
267                 rc = 0;
268         RETURN(rc);
269 }
270
271 int fld_index_init(struct lu_server_fld *fld,
272                    const struct lu_env *env,
273                    struct dt_device *dt)
274 {
275         struct dt_object *dt_obj;
276         struct lu_fid fid;
277         int rc;
278         ENTRY;
279
280         dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
281         if (!IS_ERR(dt_obj)) {
282                 fld->lsf_obj = dt_obj;
283                 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
284                                                   &fld_index_features);
285                 if (rc == 0) {
286                         LASSERT(dt_obj->do_index_ops != NULL);
287                         rc = fld_insert_igif_fld(fld, env);
288
289                         if (rc != 0) {
290                                 CERROR("insert igif in fld! = %d\n", rc);
291                                 lu_object_put(env, &dt_obj->do_lu);
292                                 fld->lsf_obj = NULL;
293                         }
294                 } else
295                         CERROR("%s: File \"%s\" is not an index!\n",
296                                fld->lsf_name, fld_index_name);
297
298
299         } else {
300                 CERROR("%s: Can't find \"%s\" obj %d\n",
301                        fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
302                 rc = PTR_ERR(dt_obj);
303         }
304
305         RETURN(rc);
306 }
307
308 void fld_index_fini(struct lu_server_fld *fld,
309                     const struct lu_env *env)
310 {
311         ENTRY;
312         if (fld->lsf_obj != NULL) {
313                 if (!IS_ERR(fld->lsf_obj))
314                         lu_object_put(env, &fld->lsf_obj->do_lu);
315                 fld->lsf_obj = NULL;
316         }
317         EXIT;
318 }