Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[fs/lustre-release.git] / lustre / mdd / mdd_device.c
1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  mdd/mdd_handler.c
5  *  Lustre Metadata Server (mdd) routines
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@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_MDS
32
33 #include <linux/module.h>
34 #include <linux/jbd.h>
35 #include <obd.h>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
40
41 #include <linux/ldiskfs_fs.h>
42 #include <lustre_mds.h>
43 #include <lustre/lustre_idl.h>
44
45 #include "mdd_internal.h"
46
47 struct md_device_operations mdd_ops;
48
49 static const char *mdd_root_dir_name = "root";
50 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
51                            const char *name, struct lu_device *next)
52 {
53         struct mdd_device *mdd = lu2mdd_dev(d);
54         int rc;
55         ENTRY;
56
57         mdd->mdd_child = lu2dt_dev(next);
58
59         /* Prepare transactions callbacks. */
60         mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
61         mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
62         mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
63         mdd->mdd_txn_cb.dtc_cookie = mdd;
64         CFS_INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
65         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
66
67         rc = mdd_procfs_init(mdd, name);
68         RETURN(rc);
69 }
70
71 static struct lu_device *mdd_device_fini(const struct lu_env *env,
72                                          struct lu_device *d)
73 {
74         struct mdd_device *mdd = lu2mdd_dev(d);
75         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
76         int rc;
77
78         rc = mdd_procfs_fini(mdd);
79         if (rc) {
80                 CERROR("proc fini error %d \n", rc);
81                 return ERR_PTR(rc);
82         }
83         return next;
84 }
85
86 static int mdd_mount(const struct lu_env *env, struct mdd_device *mdd)
87 {
88         int rc;
89         struct dt_object *root;
90         ENTRY;
91
92         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
93         root = dt_store_open(env, mdd->mdd_child, mdd_root_dir_name,
94                              &mdd->mdd_root_fid);
95         if (!IS_ERR(root)) {
96                 LASSERT(root != NULL);
97                 lu_object_put(env, &root->do_lu);
98                 rc = orph_index_init(env, mdd);
99         } else
100                 rc = PTR_ERR(root);
101
102         RETURN(rc);
103 }
104
105 static void mdd_device_shutdown(const struct lu_env *env,
106                                 struct mdd_device *m, struct lustre_cfg *cfg)
107 {
108         ENTRY;
109         dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
110         if (m->mdd_obd_dev)
111                 mdd_fini_obd(env, m, cfg);
112         orph_index_fini(env, m);
113         /* remove upcall device*/
114         md_upcall_fini(&m->mdd_md_dev);
115         EXIT;
116 }
117
118 static int mdd_process_config(const struct lu_env *env,
119                               struct lu_device *d, struct lustre_cfg *cfg)
120 {
121         struct mdd_device *m    = lu2mdd_dev(d);
122         struct dt_device  *dt   = m->mdd_child;
123         struct lu_device  *next = &dt->dd_lu_dev;
124         int rc;
125         ENTRY;
126
127         switch (cfg->lcfg_command) {
128         case LCFG_SETUP:
129                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
130                 if (rc)
131                         GOTO(out, rc);
132                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
133
134                 rc = mdd_init_obd(env, m, cfg);
135                 if (rc) {
136                         CERROR("lov init error %d \n", rc);
137                         GOTO(out, rc);
138                 }
139                 rc = mdd_mount(env, m);
140                 if (rc)
141                         GOTO(out, rc);
142                 rc = mdd_txn_init_credits(env, m);
143                 break;
144         case LCFG_CLEANUP:
145                 mdd_device_shutdown(env, m, cfg);
146         default:
147                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
148                 break;
149         }
150 out:
151         RETURN(rc);
152 }
153 #if 0
154 static int mdd_lov_set_nextid(const struct lu_env *env,
155                               struct mdd_device *mdd)
156 {
157         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
158         int rc;
159         ENTRY;
160
161         LASSERT(mds->mds_lov_objids != NULL);
162         rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
163                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
164                                 mds->mds_lov_objids, NULL);
165
166         RETURN(rc);
167 }
168
169 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
170                                    struct mdd_device *mdd)
171 {
172         /* XXX: to be implemented! */
173         return 0;
174 }
175 #endif
176
177 static int mdd_recovery_complete(const struct lu_env *env,
178                                  struct lu_device *d)
179 {
180         struct mdd_device *mdd = lu2mdd_dev(d);
181         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
182         struct obd_device *obd = mdd2obd_dev(mdd);
183         int rc;
184         ENTRY;
185
186         LASSERT(mdd != NULL);
187         LASSERT(obd != NULL);
188 #if 0
189         /* XXX: Do we need this in new stack? */
190         rc = mdd_lov_set_nextid(env, mdd);
191         if (rc) {
192                 CERROR("mdd_lov_set_nextid() failed %d\n",
193                        rc);
194                 RETURN(rc);
195         }
196
197         /* XXX: cleanup unlink. */
198         rc = mdd_cleanup_unlink_llog(env, mdd);
199         if (rc) {
200                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
201                        rc);
202                 RETURN(rc);
203         }
204 #endif
205         /* Call that with obd_recovering = 1 just to update objids */
206         obd_notify(obd->u.mds.mds_osc_obd, NULL, (obd->obd_async_recov ?
207                     OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
208
209         /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
210         obd->obd_recovering = 0;
211         obd->obd_type->typ_dt_ops->o_postrecov(obd);
212
213         /* XXX: orphans handling. */
214         __mdd_orphan_cleanup(env, mdd);
215         rc = next->ld_ops->ldo_recovery_complete(env, next);
216
217         RETURN(rc);
218 }
219
220 struct lu_device_operations mdd_lu_ops = {
221         .ldo_object_alloc      = mdd_object_alloc,
222         .ldo_process_config    = mdd_process_config,
223         .ldo_recovery_complete = mdd_recovery_complete
224 };
225
226 /*
227  * No permission check is needed.
228  */
229 static int mdd_root_get(const struct lu_env *env,
230                         struct md_device *m, struct lu_fid *f)
231 {
232         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
233
234         ENTRY;
235         *f = mdd->mdd_root_fid;
236         RETURN(0);
237 }
238
239 /*
240  * No permission check is needed.
241  */
242 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
243                       struct kstatfs *sfs)
244 {
245         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
246         int rc;
247
248         ENTRY;
249
250         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
251
252         RETURN(rc);
253 }
254
255 /*
256  * No permission check is needed.
257  */
258 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
259                            int *md_size, int *cookie_size)
260 {
261         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
262         ENTRY;
263
264         *md_size = mdd_lov_mdsize(env, mdd);
265         *cookie_size = mdd_lov_cookiesize(env, mdd);
266
267         RETURN(0);
268 }
269
270 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
271                               int mode, unsigned long timeout, __u32 alg,
272                               struct lustre_capa_key *keys)
273 {
274         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
275         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
276         int rc;
277         ENTRY;
278
279         mds->mds_capa_keys = keys;
280         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
281                                                    timeout, alg, keys);
282         RETURN(rc);
283 }
284
285 static int mdd_update_capa_key(const struct lu_env *env,
286                                struct md_device *m,
287                                struct lustre_capa_key *key)
288 {
289         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
290         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
291         int rc;
292         ENTRY;
293
294         rc = obd_set_info_async(lov_exp, strlen(KEY_CAPA_KEY), KEY_CAPA_KEY,
295                                 sizeof(*key), key, NULL);
296         RETURN(rc);
297 }
298
299 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
300                                           struct lu_device_type *t,
301                                           struct lustre_cfg *lcfg)
302 {
303         struct lu_device  *l;
304         struct mdd_device *m;
305
306         OBD_ALLOC_PTR(m);
307         if (m == NULL) {
308                 l = ERR_PTR(-ENOMEM);
309         } else {
310                 md_device_init(&m->mdd_md_dev, t);
311                 l = mdd2lu_dev(m);
312                 l->ld_ops = &mdd_lu_ops;
313                 m->mdd_md_dev.md_ops = &mdd_ops;
314                 md_upcall_init(&m->mdd_md_dev, NULL);
315         }
316
317         return l;
318 }
319
320 static struct lu_device *mdd_device_free(const struct lu_env *env,
321                                          struct lu_device *lu)
322 {
323         struct mdd_device *m = lu2mdd_dev(lu);
324         struct lu_device  *next = &m->mdd_child->dd_lu_dev;
325         ENTRY;
326
327         LASSERT(atomic_read(&lu->ld_ref) == 0);
328         md_device_fini(&m->mdd_md_dev);
329         OBD_FREE_PTR(m);
330         RETURN(next);
331 }
332
333 static struct obd_ops mdd_obd_device_ops = {
334         .o_owner = THIS_MODULE
335 };
336
337 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
338 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
339
340 static struct lu_context_key mdd_ucred_key = {
341         .lct_tags = LCT_SESSION,
342         .lct_init = mdd_ucred_key_init,
343         .lct_fini = mdd_ucred_key_fini
344 };
345
346 struct md_ucred *md_ucred(const struct lu_env *env)
347 {
348         LASSERT(env->le_ses != NULL);
349         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
350 }
351 EXPORT_SYMBOL(md_ucred);
352
353 /*
354  * context key constructor/destructor:
355  * mdd_capainfo_key_init, mdd_capainfo_key_fini
356  */
357 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
358
359 struct lu_context_key mdd_capainfo_key = {
360         .lct_tags = LCT_SESSION,
361         .lct_init = mdd_capainfo_key_init,
362         .lct_fini = mdd_capainfo_key_fini
363 };
364
365 struct md_capainfo *md_capainfo(const struct lu_env *env)
366 {
367         /* NB, in mdt_init0 */
368         if (env->le_ses == NULL)
369                 return NULL;
370         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
371 }
372 EXPORT_SYMBOL(md_capainfo);
373
374 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
375 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key);
376
377 struct md_device_operations mdd_ops = {
378         .mdo_statfs         = mdd_statfs,
379         .mdo_root_get       = mdd_root_get,
380         .mdo_maxsize_get    = mdd_maxsize_get,
381         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
382         .mdo_update_capa_key= mdd_update_capa_key,
383 };
384
385 static struct lu_device_type_operations mdd_device_type_ops = {
386         .ldto_init = mdd_type_init,
387         .ldto_fini = mdd_type_fini,
388
389         .ldto_device_alloc = mdd_device_alloc,
390         .ldto_device_free  = mdd_device_free,
391
392         .ldto_device_init    = mdd_device_init,
393         .ldto_device_fini    = mdd_device_fini
394 };
395
396 static struct lu_device_type mdd_device_type = {
397         .ldt_tags     = LU_DEVICE_MD,
398         .ldt_name     = LUSTRE_MDD_NAME,
399         .ldt_ops      = &mdd_device_type_ops,
400         .ldt_ctx_tags = LCT_MD_THREAD
401 };
402
403 /* context key constructor: mdd_key_init */
404 LU_KEY_INIT(mdd, struct mdd_thread_info);
405
406 static void mdd_key_fini(const struct lu_context *ctx,
407                          struct lu_context_key *key, void *data)
408 {
409         struct mdd_thread_info *info = data;
410         if (info->mti_max_lmm != NULL)
411                 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
412         if (info->mti_max_cookie != NULL)
413                 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
414         OBD_FREE_PTR(info);
415 }
416
417 /* context key: mdd_thread_key */
418 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
419
420 static int __init mdd_mod_init(void)
421 {
422         struct lprocfs_static_vars lvars;
423         lprocfs_mdd_init_vars(&lvars);
424         return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
425                                    LUSTRE_MDD_NAME, &mdd_device_type);
426 }
427
428 static void __exit mdd_mod_exit(void)
429 {
430         class_unregister_type(LUSTRE_MDD_NAME);
431 }
432
433 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
434 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
435 MODULE_LICENSE("GPL");
436
437 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);