Whamcloud - gitweb
Land b_head_interop_disk on HEAD (20081119_1314)
[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  2008 Sun Microsystems, Inc. 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_fld.h>
64 #include "fld_internal.h"
65
66 const char fld_index_name[] = "fld";
67 EXPORT_SYMBOL(fld_index_name);
68
69 const struct dt_index_features fld_index_features = {
70         .dif_flags       = DT_IND_UPDATE,
71         .dif_keysize_min = sizeof(seqno_t),
72         .dif_keysize_max = sizeof(seqno_t),
73         .dif_recsize_min = sizeof(mdsno_t),
74         .dif_recsize_max = sizeof(mdsno_t),
75         .dif_ptrsize     = 4
76 };
77
78 EXPORT_SYMBOL(fld_index_features);
79
80 /*
81  * number of blocks to reserve for particular operations. Should be function of
82  * ... something. Stub for now.
83  */
84 enum {
85         FLD_TXN_INDEX_INSERT_CREDITS  = 20,
86         FLD_TXN_INDEX_DELETE_CREDITS  = 20,
87 };
88
89 extern struct lu_context_key fld_thread_key;
90
91 static struct dt_key *fld_key(const struct lu_env *env,
92                               const seqno_t seq)
93 {
94         struct fld_thread_info *info;
95         ENTRY;
96
97         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
98         LASSERT(info != NULL);
99
100         info->fti_key = cpu_to_be64(seq);
101         RETURN((void *)&info->fti_key);
102 }
103
104 static struct dt_rec *fld_rec(const struct lu_env *env,
105                               const mdsno_t mds)
106 {
107         struct fld_thread_info *info;
108         ENTRY;
109
110         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
111         LASSERT(info != NULL);
112
113         info->fti_rec = cpu_to_be64(mds);
114         RETURN((void *)&info->fti_rec);
115 }
116
117 int fld_index_create(struct lu_server_fld *fld,
118                      const struct lu_env *env,
119                      seqno_t seq, mdsno_t mds)
120 {
121         struct dt_object *dt_obj = fld->lsf_obj;
122         struct dt_device *dt_dev;
123         struct txn_param txn;
124         struct thandle *th;
125         int rc;
126         ENTRY;
127
128         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
129
130         /* stub here, will fix it later */
131         txn_param_init(&txn, FLD_TXN_INDEX_INSERT_CREDITS);
132
133         th = dt_dev->dd_ops->dt_trans_start(env, dt_dev, &txn);
134         if (!IS_ERR(th)) {
135                 rc = dt_obj->do_index_ops->dio_insert(env, dt_obj,
136                                                       fld_rec(env, mds),
137                                                       fld_key(env, seq),
138                                                       th, BYPASS_CAPA, 1);
139                 dt_dev->dd_ops->dt_trans_stop(env, th);
140         } else
141                 rc = PTR_ERR(th);
142         RETURN(rc);
143 }
144
145 int fld_index_delete(struct lu_server_fld *fld,
146                      const struct lu_env *env,
147                      seqno_t seq)
148 {
149         struct dt_object *dt_obj = fld->lsf_obj;
150         struct dt_device *dt_dev;
151         struct txn_param txn;
152         struct thandle *th;
153         int rc;
154         ENTRY;
155
156         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
157         txn_param_init(&txn, FLD_TXN_INDEX_DELETE_CREDITS);
158         th = dt_dev->dd_ops->dt_trans_start(env, dt_dev, &txn);
159         if (!IS_ERR(th)) {
160                 rc = dt_obj->do_index_ops->dio_delete(env, dt_obj,
161                                                       fld_key(env, seq), th,
162                                                       BYPASS_CAPA);
163                 dt_dev->dd_ops->dt_trans_stop(env, th);
164         } else
165                 rc = PTR_ERR(th);
166         RETURN(rc);
167 }
168
169 int fld_index_lookup(struct lu_server_fld *fld,
170                      const struct lu_env *env,
171                      seqno_t seq, mdsno_t *mds)
172 {
173         struct dt_object *dt_obj = fld->lsf_obj;
174         struct dt_rec    *rec = fld_rec(env, 0);
175         int rc;
176         ENTRY;
177
178         rc = dt_obj->do_index_ops->dio_lookup(env, dt_obj, rec,
179                                               fld_key(env, seq), BYPASS_CAPA);
180         if (rc > 0) {
181                 *mds = be64_to_cpu(*(__u64 *)rec);
182                 rc = 0;
183         } else
184                 rc = -ENOENT;
185         RETURN(rc);
186 }
187
188 int fld_index_init(struct lu_server_fld *fld,
189                    const struct lu_env *env,
190                    struct dt_device *dt)
191 {
192         struct dt_object *dt_obj;
193         struct lu_fid fid;
194         int rc;
195         ENTRY;
196
197         dt_obj = dt_store_open(env, dt, "", fld_index_name, &fid);
198         if (!IS_ERR(dt_obj)) {
199                 fld->lsf_obj = dt_obj;
200                 rc = dt_obj->do_ops->do_index_try(env, dt_obj,
201                                                   &fld_index_features);
202                 if (rc == 0)
203                         LASSERT(dt_obj->do_index_ops != NULL);
204                 else
205                         CERROR("%s: File \"%s\" is not an index!\n",
206                                fld->lsf_name, fld_index_name);
207         } else {
208                 CERROR("%s: Can't find \"%s\" obj %d\n",
209                        fld->lsf_name, fld_index_name, (int)PTR_ERR(dt_obj));
210                 rc = PTR_ERR(dt_obj);
211         }
212
213         RETURN(rc);
214 }
215
216 void fld_index_fini(struct lu_server_fld *fld,
217                     const struct lu_env *env)
218 {
219         ENTRY;
220         if (fld->lsf_obj != NULL) {
221                 if (!IS_ERR(fld->lsf_obj))
222                         lu_object_put(env, &fld->lsf_obj->do_lu);
223                 fld->lsf_obj = NULL;
224         }
225         EXIT;
226 }