Whamcloud - gitweb
LU-2075 fld: use predefined FIDs
[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 | DT_IND_RANGE,
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         if (fld->lsf_no_range_lookup) {
153                 /* Stub for underlying FS which can't lookup ranges */
154                 return 0;
155         }
156
157         start = range->lsr_start;
158         LASSERT(range_is_sane(range));
159
160         rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
161                                                       fld_rec(env, range),
162                                                       fld_key(env, start), th);
163         RETURN(rc);
164 }
165
166 /**
167  * insert range in fld store.
168  *
169  *      \param  range  range to be inserted
170  *      \param  th     transaction for this operation as it could compound
171  *                     transaction.
172  *
173  *      \retval  0  success
174  *      \retval  -ve error
175  */
176
177 int fld_index_create(struct lu_server_fld *fld,
178                      const struct lu_env *env,
179                      const struct lu_seq_range *range,
180                      struct thandle *th)
181 {
182         struct dt_object *dt_obj = fld->lsf_obj;
183         seqno_t start;
184         int rc;
185
186         ENTRY;
187
188         if (fld->lsf_no_range_lookup) {
189                 /* Stub for underlying FS which can't lookup ranges */
190                 if (range->lsr_index != 0) {
191                         CERROR("%s: FLD backend does not support range"
192                                "lookups, so DNE and FIDs-on-OST are not"
193                                "supported in this configuration\n",
194                                fld->lsf_name);
195                         return -EINVAL;
196                 }
197         }
198
199         start = range->lsr_start;
200         LASSERT(range_is_sane(range));
201
202         rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
203                                               fld_rec(env, range),
204                                               fld_key(env, start),
205                                               th, BYPASS_CAPA, 1);
206
207         CDEBUG(D_INFO, "%s: insert given range : "DRANGE" rc = %d\n",
208                fld->lsf_name, PRANGE(range), rc);
209         RETURN(rc);
210 }
211
212 /**
213  * delete range in fld store.
214  *
215  *      \param  range range to be deleted
216  *      \param  th     transaction
217  *
218  *      \retval  0  success
219  *      \retval  -ve error
220  */
221
222 int fld_index_delete(struct lu_server_fld *fld,
223                      const struct lu_env *env,
224                      struct lu_seq_range *range,
225                      struct thandle   *th)
226 {
227         struct dt_object *dt_obj = fld->lsf_obj;
228         seqno_t seq = range->lsr_start;
229         int rc;
230
231         ENTRY;
232
233         rc = dt_obj->do_index_ops->dio_delete(env, dt_obj, fld_key(env, seq),
234                                               th, BYPASS_CAPA);
235
236         CDEBUG(D_INFO, "%s: delete given range : "DRANGE" rc = %d\n",
237                fld->lsf_name, PRANGE(range), rc);
238
239         RETURN(rc);
240 }
241
242 /**
243  * lookup range for a seq passed. note here we only care about the start/end,
244  * caller should handle the attached location data (flags, index).
245  *
246  * \param  seq     seq for lookup.
247  * \param  range   result of lookup.
248  *
249  * \retval  0           found, \a range is the matched range;
250  * \retval -ENOENT      not found, \a range is the left-side range;
251  * \retval  -ve         other error;
252  */
253
254 int fld_index_lookup(struct lu_server_fld *fld,
255                      const struct lu_env *env,
256                      seqno_t seq,
257                      struct lu_seq_range *range)
258 {
259         struct dt_object        *dt_obj = fld->lsf_obj;
260         struct lu_seq_range     *fld_rec;
261         struct dt_key           *key = fld_key(env, seq);
262         struct fld_thread_info  *info;
263         int rc;
264
265         ENTRY;
266
267         if (fld->lsf_no_range_lookup) {
268                 /* Stub for underlying FS which can't lookup ranges */
269                 range->lsr_start = 0;
270                 range->lsr_end = ~0;
271                 range->lsr_index = 0;
272                 range->lsr_flags = LU_SEQ_RANGE_MDT;
273
274                 range_cpu_to_be(range, range);
275                 return 0;
276         }
277
278         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
279         fld_rec = &info->fti_rec;
280
281         rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj,
282                                               (struct dt_rec*) fld_rec,
283                                               key, BYPASS_CAPA);
284
285         if (rc >= 0) {
286                 range_be_to_cpu(fld_rec, fld_rec);
287                 *range = *fld_rec;
288                 if (range_within(range, seq))
289                         rc = 0;
290                 else
291                         rc = -ENOENT;
292         }
293
294         CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
295                fld->lsf_name, seq, PRANGE(range), rc);
296
297         RETURN(rc);
298 }
299
300 static int fld_insert_igif_fld(struct lu_server_fld *fld,
301                                const struct lu_env *env)
302 {
303         struct thandle *th;
304         int rc;
305         ENTRY;
306
307         /* FLD_TXN_INDEX_INSERT_CREDITS */
308         th = fld_trans_create(fld, env);
309         if (IS_ERR(th))
310                 RETURN(PTR_ERR(th));
311         rc = fld_declare_index_create(fld, env, &IGIF_FLD_RANGE, th);
312         if (rc) {
313                 fld_trans_stop(fld, env, th);
314                 RETURN(rc);
315         }
316         rc = fld_trans_start(fld, env, th);
317         if (rc) {
318                 fld_trans_stop(fld, env, th);
319                 RETURN(rc);
320         }
321
322         rc = fld_index_create(fld, env, &IGIF_FLD_RANGE, th);
323         fld_trans_stop(fld, env, th);
324         if (rc == -EEXIST)
325                 rc = 0;
326         RETURN(rc);
327 }
328
329 int fld_index_init(struct lu_server_fld *fld,
330                    const struct lu_env *env,
331                    struct dt_device *dt)
332 {
333         struct dt_object *dt_obj;
334         struct lu_fid fid;
335         struct lu_attr attr;
336         struct dt_object_format dof;
337         int rc;
338         ENTRY;
339
340         lu_local_obj_fid(&fid, FLD_INDEX_OID);
341
342         memset(&attr, 0, sizeof(attr));
343         attr.la_valid = LA_MODE;
344         attr.la_mode = S_IFREG | 0666;
345         dof.dof_type = DFT_INDEX;
346         dof.u.dof_idx.di_feat = &fld_index_features;
347
348         dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
349         if (!IS_ERR(dt_obj)) {
350                 fld->lsf_obj = dt_obj;
351                 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
352                                                   &fld_index_features);
353                 if (rc == 0) {
354                         LASSERT(dt_obj->do_index_ops != NULL);
355                         rc = fld_insert_igif_fld(fld, env);
356
357                         if (rc != 0) {
358                                 CERROR("insert igif in fld! = %d\n", rc);
359                                 lu_object_put(env, &dt_obj->do_lu);
360                                 fld->lsf_obj = NULL;
361                         }
362                 } else if (rc == -ERANGE) {
363                         CWARN("%s: File \"%s\" doesn't support range lookup, "
364                               "using stub. DNE and FIDs on OST will not work "
365                               "with this backend\n",
366                               fld->lsf_name, fld_index_name);
367
368                         LASSERT(dt_obj->do_index_ops == NULL);
369                         fld->lsf_no_range_lookup = 1;
370                         rc = 0;
371                 } else {
372                         CERROR("%s: File \"%s\" is not index, rc %d!\n",
373                                fld->lsf_name, fld_index_name, rc);
374                         lu_object_put(env, &fld->lsf_obj->do_lu);
375                         fld->lsf_obj = NULL;
376                 }
377
378
379         } else {
380                 CERROR("%s: Can't find \"%s\" obj %d\n",
381                        fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
382                 rc = PTR_ERR(dt_obj);
383         }
384
385         RETURN(rc);
386 }
387
388 void fld_index_fini(struct lu_server_fld *fld,
389                     const struct lu_env *env)
390 {
391         ENTRY;
392         if (fld->lsf_obj != NULL) {
393                 if (!IS_ERR(fld->lsf_obj))
394                         lu_object_put(env, &fld->lsf_obj->do_lu);
395                 fld->lsf_obj = NULL;
396         }
397         EXIT;
398 }