Whamcloud - gitweb
b=17785
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_device.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #include <linux/jbd.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58 #include <linux/ldiskfs_fs.h>
59 #include <lustre_mds.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_param.h>
62
63 #include "mdd_internal.h"
64
65 const struct md_device_operations mdd_ops;
66
67 static const char mdd_root_dir_name[] = "ROOT";
68
69 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
70                            const char *name, struct lu_device *next)
71 {
72         struct mdd_device *mdd = lu2mdd_dev(d);
73         int rc;
74         ENTRY;
75
76         mdd->mdd_child = lu2dt_dev(next);
77
78         /* Prepare transactions callbacks. */
79         mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
80         mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
81         mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
82         mdd->mdd_txn_cb.dtc_cookie = mdd;
83         CFS_INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
84         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
85
86         rc = mdd_procfs_init(mdd, name);
87         RETURN(rc);
88 }
89
90 static struct lu_device *mdd_device_fini(const struct lu_env *env,
91                                          struct lu_device *d)
92 {
93         struct mdd_device *mdd = lu2mdd_dev(d);
94         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
95         int rc;
96
97         rc = mdd_procfs_fini(mdd);
98         if (rc) {
99                 CERROR("proc fini error %d \n", rc);
100                 return ERR_PTR(rc);
101         }
102         return next;
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_PARAM: {
129                 struct lprocfs_static_vars lvars;
130
131                 lprocfs_mdd_init_vars(&lvars);
132                 rc = class_process_proc_param(PARAM_MDD, lvars.obd_vars, cfg,m);
133                 if (rc > 0 || rc == -ENOSYS)
134                         /* we don't understand; pass it on */
135                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
136                 break;
137         }
138         case LCFG_SETUP:
139                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
140                 if (rc)
141                         GOTO(out, rc);
142                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
143
144                 rc = mdd_init_obd(env, m, cfg);
145                 if (rc) {
146                         CERROR("lov init error %d \n", rc);
147                         GOTO(out, rc);
148                 }
149                 rc = mdd_txn_init_credits(env, m);
150                 break;
151         case LCFG_CLEANUP:
152                 mdd_device_shutdown(env, m, cfg);
153         default:
154                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
155                 break;
156         }
157 out:
158         RETURN(rc);
159 }
160 #if 0
161 static int mdd_lov_set_nextid(const struct lu_env *env,
162                               struct mdd_device *mdd)
163 {
164         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
165         int rc;
166         ENTRY;
167
168         LASSERT(mds->mds_lov_objids != NULL);
169         rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
170                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
171                                 mds->mds_lov_objids, NULL);
172
173         RETURN(rc);
174 }
175
176 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
177                                    struct mdd_device *mdd)
178 {
179         /* XXX: to be implemented! */
180         return 0;
181 }
182 #endif
183
184 static int mdd_recovery_complete(const struct lu_env *env,
185                                  struct lu_device *d)
186 {
187         struct mdd_device *mdd = lu2mdd_dev(d);
188         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
189         struct obd_device *obd = mdd2obd_dev(mdd);
190         int rc;
191         ENTRY;
192
193         LASSERT(mdd != NULL);
194         LASSERT(obd != NULL);
195 #if 0
196         /* XXX: Do we need this in new stack? */
197         rc = mdd_lov_set_nextid(env, mdd);
198         if (rc) {
199                 CERROR("mdd_lov_set_nextid() failed %d\n",
200                        rc);
201                 RETURN(rc);
202         }
203
204         /* XXX: cleanup unlink. */
205         rc = mdd_cleanup_unlink_llog(env, mdd);
206         if (rc) {
207                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
208                        rc);
209                 RETURN(rc);
210         }
211 #endif
212         /* Call that with obd_recovering = 1 just to update objids */
213         obd_notify(obd->u.mds.mds_osc_obd, NULL, (obd->obd_async_recov ?
214                     OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
215
216         /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
217         obd->obd_recovering = 0;
218         obd->obd_type->typ_dt_ops->o_postrecov(obd);
219
220         /* XXX: orphans handling. */
221         __mdd_orphan_cleanup(env, mdd);
222         rc = next->ld_ops->ldo_recovery_complete(env, next);
223
224         RETURN(rc);
225 }
226
227 static int mdd_prepare(const struct lu_env *env,
228                        struct lu_device *pdev,
229                        struct lu_device *cdev)
230 {
231         struct mdd_device *mdd = lu2mdd_dev(cdev);
232         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
233         struct dt_object *root;
234         int rc;
235
236         ENTRY;
237         rc = next->ld_ops->ldo_prepare(env, cdev, next);
238         if (rc)
239                 GOTO(out, rc);
240
241         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
242         root = dt_store_open(env, mdd->mdd_child, "", mdd_root_dir_name,
243                              &mdd->mdd_root_fid);
244         if (!IS_ERR(root)) {
245                 LASSERT(root != NULL);
246                 lu_object_put(env, &root->do_lu);
247                 rc = orph_index_init(env, mdd);
248         } else
249                 rc = PTR_ERR(root);
250
251 out:
252         RETURN(rc);
253 }
254
255 const struct lu_device_operations mdd_lu_ops = {
256         .ldo_object_alloc      = mdd_object_alloc,
257         .ldo_process_config    = mdd_process_config,
258         .ldo_recovery_complete = mdd_recovery_complete,
259         .ldo_prepare           = mdd_prepare,
260 };
261
262 /*
263  * No permission check is needed.
264  */
265 static int mdd_root_get(const struct lu_env *env,
266                         struct md_device *m, struct lu_fid *f)
267 {
268         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
269
270         ENTRY;
271         *f = mdd->mdd_root_fid;
272         RETURN(0);
273 }
274
275 /*
276  * No permission check is needed.
277  */
278 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
279                       struct kstatfs *sfs)
280 {
281         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
282         int rc;
283
284         ENTRY;
285
286         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
287
288         RETURN(rc);
289 }
290
291 /*
292  * No permission check is needed.
293  */
294 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
295                            int *md_size, int *cookie_size)
296 {
297         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
298         ENTRY;
299
300         *md_size = mdd_lov_mdsize(env, mdd);
301         *cookie_size = mdd_lov_cookiesize(env, mdd);
302
303         RETURN(0);
304 }
305
306 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
307                               int mode, unsigned long timeout, __u32 alg,
308                               struct lustre_capa_key *keys)
309 {
310         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
311         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
312         int rc;
313         ENTRY;
314
315         mds->mds_capa_keys = keys;
316         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
317                                                    timeout, alg, keys);
318         RETURN(rc);
319 }
320
321 static int mdd_update_capa_key(const struct lu_env *env,
322                                struct md_device *m,
323                                struct lustre_capa_key *key)
324 {
325         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
326         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
327         int rc;
328         ENTRY;
329
330         rc = obd_set_info_async(lov_exp, sizeof(KEY_CAPA_KEY), KEY_CAPA_KEY,
331                                 sizeof(*key), key, NULL);
332         RETURN(rc);
333 }
334
335 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
336                                           struct lu_device_type *t,
337                                           struct lustre_cfg *lcfg)
338 {
339         struct lu_device  *l;
340         struct mdd_device *m;
341
342         OBD_ALLOC_PTR(m);
343         if (m == NULL) {
344                 l = ERR_PTR(-ENOMEM);
345         } else {
346                 md_device_init(&m->mdd_md_dev, t);
347                 l = mdd2lu_dev(m);
348                 l->ld_ops = &mdd_lu_ops;
349                 m->mdd_md_dev.md_ops = &mdd_ops;
350                 md_upcall_init(&m->mdd_md_dev, NULL);
351         }
352
353         return l;
354 }
355
356 static struct lu_device *mdd_device_free(const struct lu_env *env,
357                                          struct lu_device *lu)
358 {
359         struct mdd_device *m = lu2mdd_dev(lu);
360         struct lu_device  *next = &m->mdd_child->dd_lu_dev;
361         ENTRY;
362
363         LASSERT(atomic_read(&lu->ld_ref) == 0);
364         md_device_fini(&m->mdd_md_dev);
365         OBD_FREE_PTR(m);
366         RETURN(next);
367 }
368
369 static struct obd_ops mdd_obd_device_ops = {
370         .o_owner = THIS_MODULE
371 };
372
373 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
374 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
375
376 static struct lu_context_key mdd_ucred_key = {
377         .lct_tags = LCT_SESSION,
378         .lct_init = mdd_ucred_key_init,
379         .lct_fini = mdd_ucred_key_fini
380 };
381
382 struct md_ucred *md_ucred(const struct lu_env *env)
383 {
384         LASSERT(env->le_ses != NULL);
385         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
386 }
387 EXPORT_SYMBOL(md_ucred);
388
389 /*
390  * context key constructor/destructor:
391  * mdd_capainfo_key_init, mdd_capainfo_key_fini
392  */
393 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
394
395 struct lu_context_key mdd_capainfo_key = {
396         .lct_tags = LCT_SESSION,
397         .lct_init = mdd_capainfo_key_init,
398         .lct_fini = mdd_capainfo_key_fini
399 };
400
401 struct md_capainfo *md_capainfo(const struct lu_env *env)
402 {
403         /* NB, in mdt_init0 */
404         if (env->le_ses == NULL)
405                 return NULL;
406         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
407 }
408 EXPORT_SYMBOL(md_capainfo);
409
410 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
411 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key);
412
413 const struct md_device_operations mdd_ops = {
414         .mdo_statfs         = mdd_statfs,
415         .mdo_root_get       = mdd_root_get,
416         .mdo_maxsize_get    = mdd_maxsize_get,
417         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
418         .mdo_update_capa_key= mdd_update_capa_key,
419 #ifdef HAVE_QUOTA_SUPPORT
420         .mdo_quota          = {
421                 .mqo_notify      = mdd_quota_notify,
422                 .mqo_setup       = mdd_quota_setup,
423                 .mqo_cleanup     = mdd_quota_cleanup,
424                 .mqo_recovery    = mdd_quota_recovery,
425                 .mqo_check       = mdd_quota_check,
426                 .mqo_on          = mdd_quota_on,
427                 .mqo_off         = mdd_quota_off,
428                 .mqo_setinfo     = mdd_quota_setinfo,
429                 .mqo_getinfo     = mdd_quota_getinfo,
430                 .mqo_setquota    = mdd_quota_setquota,
431                 .mqo_getquota    = mdd_quota_getquota,
432                 .mqo_getoinfo    = mdd_quota_getoinfo,
433                 .mqo_getoquota   = mdd_quota_getoquota,
434                 .mqo_invalidate  = mdd_quota_invalidate,
435                 .mqo_finvalidate = mdd_quota_finvalidate
436         }
437 #endif
438 };
439
440 static struct lu_device_type_operations mdd_device_type_ops = {
441         .ldto_init = mdd_type_init,
442         .ldto_fini = mdd_type_fini,
443
444         .ldto_start = mdd_type_start,
445         .ldto_stop  = mdd_type_stop,
446
447         .ldto_device_alloc = mdd_device_alloc,
448         .ldto_device_free  = mdd_device_free,
449
450         .ldto_device_init    = mdd_device_init,
451         .ldto_device_fini    = mdd_device_fini
452 };
453
454 static struct lu_device_type mdd_device_type = {
455         .ldt_tags     = LU_DEVICE_MD,
456         .ldt_name     = LUSTRE_MDD_NAME,
457         .ldt_ops      = &mdd_device_type_ops,
458         .ldt_ctx_tags = LCT_MD_THREAD
459 };
460
461 /* context key constructor: mdd_key_init */
462 LU_KEY_INIT(mdd, struct mdd_thread_info);
463
464 static void mdd_key_fini(const struct lu_context *ctx,
465                          struct lu_context_key *key, void *data)
466 {
467         struct mdd_thread_info *info = data;
468         if (info->mti_max_lmm != NULL)
469                 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
470         if (info->mti_max_cookie != NULL)
471                 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
472         OBD_FREE_PTR(info);
473 }
474
475 /* context key: mdd_thread_key */
476 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
477
478 static struct lu_local_obj_desc llod_capa_key = {
479         .llod_name      = CAPA_KEYS,
480         .llod_oid       = MDD_CAPA_KEYS_OID,
481         .llod_is_index  = 0,
482 };
483
484 static struct lu_local_obj_desc llod_mdd_orphan = {
485         .llod_name      = orph_index_name,
486         .llod_oid       = MDD_ORPHAN_OID,
487         .llod_is_index  = 1,
488         .llod_feat      = &dt_directory_features,
489 };
490
491 static struct lu_local_obj_desc llod_mdd_root = {
492         .llod_name      = mdd_root_dir_name,
493         .llod_oid       = MDD_ROOT_INDEX_OID,
494         .llod_is_index  = 1,
495         .llod_feat      = &dt_directory_features,
496 };
497
498 static int __init mdd_mod_init(void)
499 {
500         struct lprocfs_static_vars lvars;
501         lprocfs_mdd_init_vars(&lvars);
502
503         llo_local_obj_register(&llod_capa_key);
504         llo_local_obj_register(&llod_mdd_orphan);
505         llo_local_obj_register(&llod_mdd_root);
506
507         return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
508                                    LUSTRE_MDD_NAME, &mdd_device_type);
509 }
510
511 static void __exit mdd_mod_exit(void)
512 {
513         llo_local_obj_unregister(&llod_capa_key);
514         llo_local_obj_unregister(&llod_mdd_orphan);
515         llo_local_obj_unregister(&llod_mdd_root);
516
517         class_unregister_type(LUSTRE_MDD_NAME);
518 }
519
520 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
521 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
522 MODULE_LICENSE("GPL");
523
524 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);