1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Copyright (C) 2006 Cluster File Systems, Inc.
7 * Author: WangDi <wangdi@clusterfs.com>
8 * Yury Umanets <umka@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_FLD
34 # include <libcfs/libcfs.h>
35 # include <linux/module.h>
36 # include <linux/jbd.h>
37 #else /* __KERNEL__ */
38 # include <liblustre.h>
42 #include <obd_class.h>
43 #include <lustre_ver.h>
44 #include <obd_support.h>
45 #include <lprocfs_status.h>
47 #include <dt_object.h>
48 #include <md_object.h>
49 #include <lustre_mdc.h>
50 #include <lustre_fld.h>
51 #include "fld_internal.h"
53 const char fld_index_name[] = "fld";
55 static const struct dt_index_features fld_index_features = {
56 .dif_flags = DT_IND_UPDATE,
57 .dif_keysize_min = sizeof(seqno_t),
58 .dif_keysize_max = sizeof(seqno_t),
59 .dif_recsize_min = sizeof(mdsno_t),
60 .dif_recsize_max = sizeof(mdsno_t)
64 * number of blocks to reserve for particular operations. Should be function of
65 * ... something. Stub for now.
68 FLD_TXN_INDEX_INSERT_CREDITS = 20,
69 FLD_TXN_INDEX_DELETE_CREDITS = 20
72 struct fld_thread_info {
77 static void *fld_key_init(const struct lu_context *ctx,
78 struct lu_context_key *key)
80 struct fld_thread_info *info;
85 info = ERR_PTR(-ENOMEM);
89 static void fld_key_fini(const struct lu_context *ctx,
90 struct lu_context_key *key, void *data)
92 struct fld_thread_info *info = data;
98 static int fld_key_registered = 0;
100 static struct lu_context_key fld_thread_key = {
101 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD,
102 .lct_init = fld_key_init,
103 .lct_fini = fld_key_fini
106 static struct dt_key *fld_key(const struct lu_context *ctx,
109 struct fld_thread_info *info;
112 info = lu_context_key_get(ctx, &fld_thread_key);
113 LASSERT(info != NULL);
115 info->fti_key = cpu_to_be64(seq);
116 RETURN((void *)&info->fti_key);
119 static struct dt_rec *fld_rec(const struct lu_context *ctx,
122 struct fld_thread_info *info;
125 info = lu_context_key_get(ctx, &fld_thread_key);
126 LASSERT(info != NULL);
128 info->fti_rec = cpu_to_be64(mds);
129 RETURN((void *)&info->fti_rec);
132 int fld_index_create(struct lu_server_fld *fld,
133 const struct lu_context *ctx,
134 seqno_t seq, mdsno_t mds)
136 struct dt_device *dt = fld->fld_dt;
137 struct dt_object *dt_obj = fld->fld_obj;
138 struct txn_param txn;
143 /*stub here, will fix it later*/
144 txn.tp_credits = FLD_TXN_INDEX_INSERT_CREDITS;
146 th = dt->dd_ops->dt_trans_start(ctx, dt, &txn);
148 rc = dt_obj->do_index_ops->dio_insert(ctx, dt_obj,
150 fld_key(ctx, seq), th);
151 dt->dd_ops->dt_trans_stop(ctx, th);
157 int fld_index_delete(struct lu_server_fld *fld,
158 const struct lu_context *ctx,
161 struct dt_device *dt = fld->fld_dt;
162 struct dt_object *dt_obj = fld->fld_obj;
163 struct txn_param txn;
168 txn.tp_credits = FLD_TXN_INDEX_DELETE_CREDITS;
169 th = dt->dd_ops->dt_trans_start(ctx, dt, &txn);
171 rc = dt_obj->do_index_ops->dio_delete(ctx, dt_obj,
172 fld_key(ctx, seq), th);
173 dt->dd_ops->dt_trans_stop(ctx, th);
179 int fld_index_lookup(struct lu_server_fld *fld,
180 const struct lu_context *ctx,
181 seqno_t seq, mdsno_t *mds)
183 struct dt_object *dt_obj = fld->fld_obj;
184 struct dt_rec *rec = fld_rec(ctx, 0);
188 rc = dt_obj->do_index_ops->dio_lookup(ctx, dt_obj, rec,
191 *mds = be64_to_cpu(*(__u64 *)rec);
195 int fld_index_init(struct lu_server_fld *fld,
196 const struct lu_context *ctx)
198 struct dt_device *dt = fld->fld_dt;
199 struct dt_object *dt_obj;
203 if (fld_key_registered == 0) {
204 rc = lu_context_key_register(&fld_thread_key);
208 fld_key_registered++;
211 * lu_context_key has to be registered before threads are started,
214 LASSERT(fld->fld_service == NULL);
216 dt_obj = dt_store_open(ctx, dt, fld_index_name, &fld->fld_fid);
217 if (!IS_ERR(dt_obj)) {
218 fld->fld_obj = dt_obj;
219 rc = dt_obj->do_ops->do_object_index_try(ctx, dt_obj,
220 &fld_index_features);
222 LASSERT(dt_obj->do_index_ops != NULL);
224 CERROR("\"%s\" is not an index!\n", fld_index_name);
226 CERROR("cannot find \"%s\" obj %d\n",
227 fld_index_name, (int)PTR_ERR(dt_obj));
228 rc = PTR_ERR(dt_obj);
234 void fld_index_fini(struct lu_server_fld *fld,
235 const struct lu_context *ctx)
238 if (fld->fld_obj != NULL) {
239 lu_object_put(ctx, &fld->fld_obj->do_lu);
242 if (fld_key_registered > 0) {
243 if (--fld_key_registered == 0)
244 lu_context_key_degister(&fld_thread_key);