Whamcloud - gitweb
update cmm-mdc code
[fs/lustre-release.git] / lustre / cmm / mdc_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/cmm/mdc_object.c
5  *  Lustre Cluster Metadata Manager (cmm)
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Mike Pershin <tappro@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
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32
33 #define DEBUG_SUBSYSTEM S_MDS
34 #include <obd_support.h>
35 #include <lustre_lib.h>
36 #include <obd_class.h>
37 #include "mdc_internal.h"
38
39 static struct md_object_operations mdc_mo_ops;
40 static struct md_dir_operations mdc_dir_ops;
41 static struct lu_object_operations mdc_obj_ops;
42
43 #ifdef CMM_CODE
44 extern struct lu_context_key mdc_thread_key;
45
46 struct lu_object *mdc_object_alloc(const struct lu_context *ctx,
47                                    const struct lu_object_header *hdr,
48                                    struct lu_device *ld)
49 {
50         struct mdc_object *mco;
51         ENTRY;
52
53         OBD_ALLOC_PTR(mco);
54         if (mco != NULL) {
55                 struct lu_object *lo;
56
57                 lo = &mco->mco_obj.mo_lu;
58                 lu_object_init(lo, NULL, ld);
59                 mco->mco_obj.mo_ops = &mdc_mo_ops;
60                 mco->mco_obj.mo_dir_ops = &mdc_dir_ops;
61                 lo->lo_ops = &mdc_obj_ops;
62                 RETURN(lo);
63         } else
64                 RETURN(NULL);
65 }
66
67 static void mdc_object_free(const struct lu_context *ctx, struct lu_object *lo)
68 {
69         struct mdc_object *mco = lu2mdc_obj(lo);
70         lu_object_fini(lo);
71         OBD_FREE_PTR(mco);
72 }
73
74 static int mdc_object_init(const struct lu_context *ctx, struct lu_object *lo)
75 {
76         ENTRY;
77
78         RETURN(0);
79 }
80
81 static void mdc_object_release(const struct lu_context *ctx,
82                                struct lu_object *lo)
83 {
84 }
85
86 static int mdc_object_exists(const struct lu_context *ctx, struct lu_object *lo)
87 {
88         /* we don't know does it exists or not - but suppose that it does*/
89         return 1;
90 }
91
92 static int mdc_object_print(const struct lu_context *ctx,
93                             struct seq_file *f, const struct lu_object *lo)
94 {
95         return seq_printf(f, LUSTRE_MDC0_NAME"-object@%p", lo);
96 }
97
98 static struct lu_object_operations mdc_obj_ops = {
99         .loo_object_init    = mdc_object_init,
100         .loo_object_release = mdc_object_release,
101         .loo_object_free    = mdc_object_free,
102         .loo_object_print   = mdc_object_print,
103         .loo_object_exists  = mdc_object_exists
104 };
105
106 /* md_object_operations */
107 static int mdc_object_create(const struct lu_context *ctx,
108                              struct md_object *mo, struct lu_attr *attr)
109 {
110         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
111         struct mdc_thread_info *mci;
112         int rc;
113         ENTRY;
114
115         mci = lu_context_get_key(ctx, &mdc_thread_key);
116         LASSERT(mci);
117
118         mci->mci_opdata.fid1 = *lu_object_fid(&mo->mo_lu);
119         mci->mci_opdata.fid2 = { 0 };
120         mci->mci_opdata.mod_time = attr->la_mtime;
121         mci->mci_opdata.name = NULL;
122         mci->mci_opdata.namelen = 0;
123
124         rc = md_create(mc->mc_desc.cl_exp, &mci->mci_opdata, NULL, 0,
125                        attr->la_mode, attr->la_uid, attr->la_gid, 0, 0,
126                        &mci->mci_req);
127
128         RETURN(rc);
129 }
130 static int mdc_ref_add(const struct lu_context *ctx, struct md_object *mo)
131 {
132         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
133         struct mdc_thread_info *mci;
134         int rc;
135         ENTRY;
136
137         mci = lu_context_get_key(ctx, &mdc_thread_key);
138         LASSERT(mci);
139
140         mci->mci_opdata.fid1 = *lu_object_fid(&mo->mo_lu);
141         mci->mci_opdata.fid2 = { 0 };
142         mci->mci_opdata.mod_time = attr->la_mtime;
143         mci->mci_opdata.name = NULL;
144         mci->mci_opdata.namelen = 0;
145
146         rc = md_link(mc->mc_desc.cl_exp, &mci->mci_opdata, &mci->mci_req);
147
148         RETURN(rc);
149 }
150
151 static int mdc_ref_del(const struct lu_context *ctx, struct md_object *mo)
152 {
153         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
154         struct mdc_thread_info *mci;
155         int rc;
156         ENTRY;
157
158         mci = lu_context_get_key(ctx, &mdc_thread_key);
159         LASSERT(mci);
160
161         mci->mci_opdata.fid1 = *lu_object_fid(&mo->mo_lu);
162         mci->mci_opdata.fid2 = { 0 };
163         mci->mci_opdata.mod_time = attr->la_mtime;
164         mci->mci_opdata.name = NULL;
165         mci->mci_opdata.namelen = 0;
166
167         rc = md_unlink(mc->mc_desc.cl_exp, &mci->mci_opdata, &mci->mci_req);
168
169         RETURN(rc);
170 }
171
172 static struct md_object_operations mdc_mo_ops = {
173         .moo_object_create  = mdc_object_create,
174         .moo_ref_add        = mdc_ref_add,
175         .moo_ref_del        = mdc_ref_del,
176 };
177
178 /* md_dir_operations */
179 static int mdc_name_insert(const struct lu_context *ctx, struct md_object *mo,
180                            const char *name, const struct lu_fid *lf)
181 {
182         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
183         struct mdc_thread_info *mci;
184         int rc;
185         ENTRY;
186
187         mci = lu_context_get_key(ctx, &mdc_thread_key);
188         LASSERT(mci);
189
190         mci->mci_opdata.fid1 = *lu_object_fid(&mo->mo_lu);
191         mci->mci_opdata.fid2 = *lf;
192         mci->mci_opdata.mod_time = attr->la_mtime;
193         //TODO: distinguish the name_insert and rename_tgt()
194
195         rc = md_rename(mc->mc_desc.cl_exp, &mci->mci_opdata, NULL, 0,
196                        name, strlen(name), &mci->mci_req);
197
198         RETURN(rc);
199 }
200
201 static int mdc_rename_tgt(const struct lu_context *ctx,
202                           struct md_object *mo_p, struct md_object *mo_s,
203                           struct md_object *mo_t, const char *name)
204 {
205         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
206         struct mdc_thread_info *mci;
207         int rc;
208         ENTRY;
209
210         mci = lu_context_get_key(ctx, &mdc_thread_key);
211         LASSERT(mci);
212
213         mci->mci_opdata.fid1 = *lu_object_fid(&mo_p->mo_lu);
214         mci->mci_opdata.fid2 = *lu_object_fid(&mo_s->mo_lu);
215         mci->mci_opdata.mod_time = attr->la_mtime;
216
217         rc = md_rename(mc->mc_desc.cl_exp, &mci->mci_opdata, NULL, 0,
218                        name, strlen(name), &mci->mci_req);
219
220         RETURN(rc);
221 }
222
223 static struct md_dir_operations mdc_dir_ops = {
224         .mdo_name_insert = mdc_name_insert,
225         .mdo_rename_tgt  = mdc_rename_tgt,
226 };
227
228 #else /* CMM_CODE */
229 struct lu_object *mdc_object_alloc(const struct lu_context *ctx,
230                                    const struct lu_object_header *hdr,
231                                    struct lu_device *ld)
232 {
233         struct mdc_object *mco;
234         ENTRY;
235
236         OBD_ALLOC_PTR(mco);
237         if (mco != NULL) {
238                 struct lu_object *lo;
239
240                 lo = &mco->mco_obj.mo_lu;
241                 lu_object_init(lo, NULL, ld);
242                 mco->mco_obj.mo_ops = &mdc_mo_ops;
243                 mco->mco_obj.mo_dir_ops = &mdc_dir_ops;
244                 lo->lo_ops = &mdc_obj_ops;
245                 RETURN(lo);
246         } else
247                 RETURN(NULL);
248 }
249
250 static int mdc_object_init(const struct lu_context *ctx, struct lu_object *lo)
251 {
252         //struct mdc_device *d = lu2mdc_dev(o->lo_dev);
253         //struct lu_device  *under;
254         //const struct lu_fid     *fid = lu_object_fid(o);
255
256         ENTRY;
257
258         RETURN(0);
259 }
260
261 static void mdc_object_free(const struct lu_context *ctx, struct lu_object *lo)
262 {
263         struct mdc_object *mco = lu2mdc_obj(lo);
264         lu_object_fini(lo);
265         OBD_FREE_PTR(mco);
266 }
267
268 static void mdc_object_release(const struct lu_context *ctx,
269                                struct lu_object *lo)
270 {
271         return;
272 }
273
274 static int mdc_object_exists(const struct lu_context *ctx, struct lu_object *lo)
275 {
276         return 0;
277 }
278
279 static int mdc_object_print(const struct lu_context *ctx,
280                             struct seq_file *f, const struct lu_object *lo)
281 {
282         return seq_printf(f, LUSTRE_MDC0_NAME"-object@%p", lo);
283 }
284
285 static int mdc_object_create(const struct lu_context *ctx,
286                              struct md_object *mo, struct lu_attr *attr)
287 {
288         struct mdc_device *mc = md2mdc_dev(md_device_get(mo));
289         struct obd_export *exp = mc->mc_desc.cl_exp;
290         struct ptlrpc_request *req;
291         struct md_op_data op_data = {
292                 .fid1 = mo->mo_lu.lo_header->loh_fid,
293                 .fid2 = { 0 },
294                 .mod_time = attr->la_mtime,
295                 .name = NULL,
296                 .namelen = 0,
297         };
298         int rc;
299
300         rc = md_create(exp, &op_data, NULL, 0, attr->la_mode, attr->la_uid,
301                        attr->la_gid, 0, 0, &req);
302         RETURN(rc);
303 }
304
305 static struct md_dir_operations mdc_dir_ops = {
306 };
307
308 static struct md_object_operations mdc_mo_ops = {
309         .moo_object_create  = mdc_object_create
310 };
311
312 static struct lu_object_operations mdc_obj_ops = {
313         .loo_object_init    = mdc_object_init,
314         .loo_object_release = mdc_object_release,
315         .loo_object_free    = mdc_object_free,
316         .loo_object_print   = mdc_object_print,
317         .loo_object_exists  = mdc_object_exists
318 };
319 #endif /* CMM_CODE */