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