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