Whamcloud - gitweb
kernel-patches: add dynlocks to -fc{3,5} series.
[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         struct dt_device  *dt;
55         int rc = 0;
56         ENTRY;
57
58         mdd->mdd_child = lu2dt_dev(next);
59
60         dt = mdd->mdd_child;
61         /* prepare transactions callbacks */
62         mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
63         mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
64         mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
65         mdd->mdd_txn_cb.dtc_cookie = mdd;
66         rc = mdd_procfs_init(mdd);
67         RETURN(rc);
68 }
69
70 static struct lu_device *mdd_device_fini(const struct lu_env *env,
71                                          struct lu_device *d)
72 {
73         struct mdd_device *mdd = lu2mdd_dev(d);
74         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
75         int rc;
76         
77         rc = mdd_procfs_fini(mdd);
78         if (rc) {
79                 CERROR("proc fini error %d \n", rc);
80                 return ERR_PTR(rc);
81         }
82         return next;
83 }
84
85 static int mdd_mount(const struct lu_env *env, struct mdd_device *mdd)
86 {
87         int rc;
88         struct dt_object *root;
89         ENTRY;
90
91         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
92         root = dt_store_open(env, mdd->mdd_child, mdd_root_dir_name,
93                              &mdd->mdd_root_fid);
94         if (!IS_ERR(root)) {
95                 LASSERT(root != NULL);
96                 lu_object_put(env, &root->do_lu);
97                 rc = orph_index_init(env, mdd);
98         } else
99                 rc = PTR_ERR(root);
100
101         RETURN(rc);
102 }
103
104 static void mdd_device_shutdown(const struct lu_env *env,
105                                 struct mdd_device *m)
106 {
107         dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
108         if (m->mdd_obd_dev)
109                 mdd_fini_obd(env, m);
110         orph_index_fini(env, m);
111 }
112
113 static int mdd_process_config(const struct lu_env *env,
114                               struct lu_device *d, struct lustre_cfg *cfg)
115 {
116         struct mdd_device *m    = lu2mdd_dev(d);
117         struct dt_device  *dt   = m->mdd_child;
118         struct lu_device  *next = &dt->dd_lu_dev;
119         int rc;
120         ENTRY;
121
122         switch (cfg->lcfg_command) {
123         case LCFG_SETUP:
124                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
125                 if (rc)
126                         GOTO(out, rc);
127                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
128
129                 rc = mdd_init_obd(env, m, cfg);
130                 if (rc) {
131                         CERROR("lov init error %d \n", rc);
132                         GOTO(out, rc);
133                 }
134                 rc = mdd_mount(env, m);
135                 if (rc)
136                         GOTO(out, rc);
137                 rc = mdd_txn_init_credits(env, m);
138                 break;
139         case LCFG_CLEANUP:
140                 mdd_device_shutdown(env, m);
141         default:
142                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
143                 break;
144         }
145 out:
146         RETURN(rc);
147 }
148 #if 0
149 static int mdd_lov_set_nextid(const struct lu_env *env,
150                               struct mdd_device *mdd)
151 {
152         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
153         int rc;
154         ENTRY;
155
156         LASSERT(mds->mds_lov_objids != NULL);
157         rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
158                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
159                                 mds->mds_lov_objids, NULL);
160
161         RETURN(rc);
162 }
163
164 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
165                                    struct mdd_device *mdd)
166 {
167         /* XXX: to be implemented! */
168         return 0;
169 }
170 #endif
171
172 static int mdd_recovery_complete(const struct lu_env *env,
173                                  struct lu_device *d)
174 {
175         struct mdd_device *mdd = lu2mdd_dev(d);
176         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
177         struct obd_device *obd = mdd2obd_dev(mdd);
178         int rc;
179         ENTRY;
180
181         LASSERT(mdd != NULL);
182         LASSERT(obd != NULL);
183 #if 0
184         /* XXX: Do we need this in new stack? */
185         rc = mdd_lov_set_nextid(env, mdd);
186         if (rc) {
187                 CERROR("mdd_lov_set_nextid() failed %d\n",
188                        rc);
189                 RETURN(rc);
190         }
191
192         /* XXX: cleanup unlink. */
193         rc = mdd_cleanup_unlink_llog(env, mdd);
194         if (rc) {
195                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
196                        rc);
197                 RETURN(rc);
198         }
199 #endif
200         obd_notify(obd->u.mds.mds_osc_obd, NULL,
201                    (obd->obd_async_recov ?
202                     OBD_NOTIFY_SYNC_NONBLOCK :
203                     OBD_NOTIFY_SYNC), NULL);
204
205         obd->obd_recovering = 0;
206         obd->obd_type->typ_dt_ops->o_postrecov(obd);
207
208         /* XXX: orphans handling. */
209         __mdd_orphan_cleanup(env, mdd);
210         rc = next->ld_ops->ldo_recovery_complete(env, next);
211
212         RETURN(rc);
213 }
214
215 struct lu_device_operations mdd_lu_ops = {
216         .ldo_object_alloc      = mdd_object_alloc,
217         .ldo_process_config    = mdd_process_config,
218         .ldo_recovery_complete = mdd_recovery_complete
219 };
220
221 /*
222  * No permission check is needed.
223  */
224 static int mdd_root_get(const struct lu_env *env,
225                         struct md_device *m, struct lu_fid *f)
226 {
227         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
228
229         ENTRY;
230         *f = mdd->mdd_root_fid;
231         RETURN(0);
232 }
233
234 /*
235  * No permission check is needed.
236  */
237 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
238                       struct kstatfs *sfs)
239 {
240         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
241         int rc;
242
243         ENTRY;
244
245         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
246
247         RETURN(rc);
248 }
249
250 /*
251  * No permission check is needed.
252  */
253 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
254                            int *md_size, int *cookie_size)
255 {
256         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
257         ENTRY;
258
259         *md_size = mdd_lov_mdsize(env, mdd);
260         *cookie_size = mdd_lov_cookiesize(env, mdd);
261
262         RETURN(0);
263 }
264
265 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
266                               int mode, unsigned long timeout, __u32 alg,
267                               struct lustre_capa_key *keys)
268 {
269         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
270         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
271         int rc;
272         ENTRY;
273
274         mds->mds_capa_keys = keys;
275         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
276                                                    timeout, alg, keys);
277         RETURN(rc);
278 }
279
280 static int mdd_update_capa_key(const struct lu_env *env,
281                                struct md_device *m,
282                                struct lustre_capa_key *key)
283 {
284         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
285         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
286         int rc;
287         ENTRY;
288
289         rc = obd_set_info_async(lov_exp, strlen(KEY_CAPA_KEY), KEY_CAPA_KEY,
290                                 sizeof(*key), key, NULL);
291         RETURN(rc);
292 }
293
294 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
295                                           struct lu_device_type *t,
296                                           struct lustre_cfg *lcfg)
297 {
298         struct lu_device  *l;
299         struct mdd_device *m;
300
301         OBD_ALLOC_PTR(m);
302         if (m == NULL) {
303                 l = ERR_PTR(-ENOMEM);
304         } else {
305                 md_device_init(&m->mdd_md_dev, t);
306                 l = mdd2lu_dev(m);
307                 l->ld_ops = &mdd_lu_ops;
308                 m->mdd_md_dev.md_ops = &mdd_ops;
309         }
310
311         return l;
312 }
313
314 static void mdd_device_free(const struct lu_env *env,
315                             struct lu_device *lu)
316 {
317         struct mdd_device *m = lu2mdd_dev(lu);
318
319         LASSERT(atomic_read(&lu->ld_ref) == 0);
320         md_device_fini(&m->mdd_md_dev);
321         OBD_FREE_PTR(m);
322 }
323
324 static struct obd_ops mdd_obd_device_ops = {
325         .o_owner = THIS_MODULE
326 };
327
328 static void *mdd_ucred_key_init(const struct lu_context *ctx,
329                                 struct lu_context_key *key)
330 {
331         struct md_ucred *uc;
332
333         OBD_ALLOC_PTR(uc);
334         if (uc == NULL)
335                 uc = ERR_PTR(-ENOMEM);
336         return uc;
337 }
338
339 static void mdd_ucred_key_fini(const struct lu_context *ctx,
340                              struct lu_context_key *key, void *data)
341 {
342         struct md_ucred *uc = data;
343         OBD_FREE_PTR(uc);
344 }
345
346 static struct lu_context_key mdd_ucred_key = {
347         .lct_tags = LCT_SESSION,
348         .lct_init = mdd_ucred_key_init,
349         .lct_fini = mdd_ucred_key_fini
350 };
351
352 struct md_ucred *md_ucred(const struct lu_env *env)
353 {
354         LASSERT(env->le_ses != NULL);
355         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
356 }
357 EXPORT_SYMBOL(md_ucred);
358
359 static void *mdd_capainfo_key_init(const struct lu_context *ctx,
360                                    struct lu_context_key *key)
361 {
362         struct md_capainfo *ci;
363
364         OBD_ALLOC_PTR(ci);
365         if (ci == NULL)
366                 ci = ERR_PTR(-ENOMEM);
367         return ci;
368 }
369
370 static void mdd_capainfo_key_fini(const struct lu_context *ctx,
371                                   struct lu_context_key *key, void *data)
372 {
373         struct md_capainfo *ci = data;
374         OBD_FREE_PTR(ci);
375 }
376
377 struct lu_context_key mdd_capainfo_key = {
378         .lct_tags = LCT_SESSION,
379         .lct_init = mdd_capainfo_key_init,
380         .lct_fini = mdd_capainfo_key_fini
381 };
382
383 struct md_capainfo *md_capainfo(const struct lu_env *env)
384 {
385         /* NB, in mdt_init0 */
386         if (env->le_ses == NULL)
387                 return NULL;
388         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
389 }
390 EXPORT_SYMBOL(md_capainfo);
391
392 static int mdd_type_init(struct lu_device_type *t)
393 {
394         int result;
395
396         result = lu_context_key_register(&mdd_thread_key);
397         if (result == 0)
398                 result = lu_context_key_register(&mdd_ucred_key);
399         if (result == 0)
400                 result = lu_context_key_register(&mdd_capainfo_key);
401         return result;
402 }
403
404 static void mdd_type_fini(struct lu_device_type *t)
405 {
406         lu_context_key_degister(&mdd_capainfo_key);
407         lu_context_key_degister(&mdd_ucred_key);
408         lu_context_key_degister(&mdd_thread_key);
409 }
410
411 struct md_device_operations mdd_ops = {
412         .mdo_statfs         = mdd_statfs,
413         .mdo_root_get       = mdd_root_get,
414         .mdo_maxsize_get    = mdd_maxsize_get,
415         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
416         .mdo_update_capa_key= mdd_update_capa_key,
417 };
418
419 static struct lu_device_type_operations mdd_device_type_ops = {
420         .ldto_init = mdd_type_init,
421         .ldto_fini = mdd_type_fini,
422
423         .ldto_device_alloc = mdd_device_alloc,
424         .ldto_device_free  = mdd_device_free,
425
426         .ldto_device_init    = mdd_device_init,
427         .ldto_device_fini    = mdd_device_fini
428 };
429
430 static struct lu_device_type mdd_device_type = {
431         .ldt_tags     = LU_DEVICE_MD,
432         .ldt_name     = LUSTRE_MDD_NAME,
433         .ldt_ops      = &mdd_device_type_ops,
434         .ldt_ctx_tags = LCT_MD_THREAD
435 };
436
437 static void *mdd_key_init(const struct lu_context *ctx,
438                           struct lu_context_key *key)
439 {
440         struct mdd_thread_info *info;
441
442         OBD_ALLOC_PTR(info);
443         if (info == NULL)
444                 info = ERR_PTR(-ENOMEM);
445         return info;
446 }
447
448 static void mdd_key_fini(const struct lu_context *ctx,
449                          struct lu_context_key *key, void *data)
450 {
451         struct mdd_thread_info *info = data;
452         OBD_FREE_PTR(info);
453 }
454
455 struct lu_context_key mdd_thread_key = {
456         .lct_tags = LCT_MD_THREAD,
457         .lct_init = mdd_key_init,
458         .lct_fini = mdd_key_fini
459 };
460
461 struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
462         { 0 }
463 };
464
465 struct lprocfs_vars lprocfs_mdd_module_vars[] = {
466         { 0 }
467 };
468
469 LPROCFS_INIT_VARS(mdd, lprocfs_mdd_module_vars, lprocfs_mdd_obd_vars);
470
471 static int __init mdd_mod_init(void)
472 {
473         struct lprocfs_static_vars lvars;
474         printk(KERN_INFO "Lustre: MetaData Device; info@clusterfs.com\n");
475         lprocfs_init_vars(mdd, &lvars);
476         return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
477                                    LUSTRE_MDD_NAME, &mdd_device_type);
478 }
479
480 static void __exit mdd_mod_exit(void)
481 {
482         class_unregister_type(LUSTRE_MDD_NAME);
483 }
484
485 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
486 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
487 MODULE_LICENSE("GPL");
488
489 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);