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