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