Whamcloud - gitweb
- start fld and seq services in MDT, thus, make MDT an universal network related...
[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  *  fld/fld_index.c
5  *
6  *  Copyright (C) 2006 Cluster File Systems, Inc.
7  *   Author: WangDi <wangdi@clusterfs.com>
8  *           Yury Umanets <umka@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
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.
17  *
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.
22  *
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.
27  */
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_FLD
32
33 #ifdef __KERNEL__
34 # include <libcfs/libcfs.h>
35 # include <linux/module.h>
36 # include <linux/jbd.h>
37 #else /* __KERNEL__ */
38 # include <liblustre.h>
39 #endif
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <lustre_ver.h>
44 #include <obd_support.h>
45 #include <lprocfs_status.h>
46
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"
52
53 const char fld_index_name[] = "fld";
54
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)
61 };
62
63 /*
64  * number of blocks to reserve for particular operations. Should be function of
65  * ... something. Stub for now.
66  */
67 enum {
68         FLD_TXN_INDEX_INSERT_CREDITS  = 20,
69         FLD_TXN_INDEX_DELETE_CREDITS  = 20,
70 };
71
72 extern struct lu_context_key fld_thread_key;
73
74 static struct dt_key *fld_key(const struct lu_context *ctx,
75                               const seqno_t seq)
76 {
77         struct fld_thread_info *info;
78         ENTRY;
79
80         info = lu_context_key_get(ctx, &fld_thread_key);
81         LASSERT(info != NULL);
82
83         info->fti_key = cpu_to_be64(seq);
84         RETURN((void *)&info->fti_key);
85 }
86
87 static struct dt_rec *fld_rec(const struct lu_context *ctx,
88                               const mdsno_t mds)
89 {
90         struct fld_thread_info *info;
91         ENTRY;
92
93         info = lu_context_key_get(ctx, &fld_thread_key);
94         LASSERT(info != NULL);
95
96         info->fti_rec = cpu_to_be64(mds);
97         RETURN((void *)&info->fti_rec);
98 }
99
100 int fld_index_create(struct lu_server_fld *fld,
101                      const struct lu_context *ctx,
102                      seqno_t seq, mdsno_t mds)
103 {
104         struct dt_object *dt_obj = fld->lsf_obj;
105         struct dt_device *dt_dev;
106         struct txn_param txn;
107         struct thandle *th;
108         int rc;
109         ENTRY;
110
111         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
112         
113         /* stub here, will fix it later */
114         txn.tp_credits = FLD_TXN_INDEX_INSERT_CREDITS;
115
116         th = dt_dev->dd_ops->dt_trans_start(ctx, dt_dev, &txn);
117         if (!IS_ERR(th)) {
118                 rc = dt_obj->do_index_ops->dio_insert(ctx, dt_obj,
119                                                       fld_rec(ctx, mds),
120                                                       fld_key(ctx, seq), th);
121                 dt_dev->dd_ops->dt_trans_stop(ctx, th);
122         } else
123                 rc = PTR_ERR(th);
124         RETURN(rc);
125 }
126
127 int fld_index_delete(struct lu_server_fld *fld,
128                      const struct lu_context *ctx,
129                      seqno_t seq)
130 {
131         struct dt_object *dt_obj = fld->lsf_obj;
132         struct dt_device *dt_dev;
133         struct txn_param txn;
134         struct thandle *th;
135         int rc;
136         ENTRY;
137
138         dt_dev = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
139         txn.tp_credits = FLD_TXN_INDEX_DELETE_CREDITS;
140         th = dt_dev->dd_ops->dt_trans_start(ctx, dt_dev, &txn);
141         if (!IS_ERR(th)) {
142                 rc = dt_obj->do_index_ops->dio_delete(ctx, dt_obj,
143                                                       fld_key(ctx, seq), th);
144                 dt_dev->dd_ops->dt_trans_stop(ctx, th);
145         } else
146                 rc = PTR_ERR(th);
147         RETURN(rc);
148 }
149
150 int fld_index_lookup(struct lu_server_fld *fld,
151                      const struct lu_context *ctx,
152                      seqno_t seq, mdsno_t *mds)
153 {
154         struct dt_object *dt_obj = fld->lsf_obj;
155         struct dt_rec    *rec = fld_rec(ctx, 0);
156         int rc;
157         ENTRY;
158
159         rc = dt_obj->do_index_ops->dio_lookup(ctx, dt_obj, rec,
160                                               fld_key(ctx, seq));
161         if (rc == 0)
162                 *mds = be64_to_cpu(*(__u64 *)rec);
163         RETURN(rc);
164 }
165
166 int fld_index_init(struct lu_server_fld *fld,
167                    const struct lu_context *ctx,
168                    struct dt_device *dt)
169 {
170         struct dt_object *dt_obj;
171         struct lu_fid fid;
172         int rc;
173         ENTRY;
174
175         dt_obj = dt_store_open(ctx, dt, fld_index_name, &fid);
176         if (!IS_ERR(dt_obj)) {
177                 fld->lsf_obj = dt_obj;
178                 rc = dt_obj->do_ops->do_index_try(ctx, dt_obj,
179                                                   &fld_index_features);
180                 if (rc == 0)
181                         LASSERT(dt_obj->do_index_ops != NULL);
182                 else
183                         CERROR("\"%s\" is not an index!\n",
184                                fld_index_name);
185         } else {
186                 CERROR("cannot find \"%s\" obj %d\n",
187                        fld_index_name, (int)PTR_ERR(dt_obj));
188                 rc = PTR_ERR(dt_obj);
189         }
190
191         RETURN(rc);
192 }
193
194 void fld_index_fini(struct lu_server_fld *fld,
195                     const struct lu_context *ctx)
196 {
197         ENTRY;
198         if (fld->lsf_obj != NULL) {
199                 if (!IS_ERR(fld->lsf_obj))
200                         lu_object_put(ctx, &fld->lsf_obj->do_lu);
201                 fld->lsf_obj = NULL;
202         }
203         EXIT;
204 }