Whamcloud - gitweb
LU-3285 mdc: add cl_device to the MDC
[fs/lustre-release.git] / lustre / mdc / mdc_dev.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2017 Intel Corporation.
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  *
28  * Implementation of cl_device, cl_req for MDC layer.
29  *
30  * Author: Mikhail Pershin <mike.pershin@intel.com>
31  */
32
33 #define DEBUG_SUBSYSTEM S_MDC
34
35 #include <obd_class.h>
36 #include <lustre_osc.h>
37
38 #include "mdc_internal.h"
39
40 int mdc_page_init(const struct lu_env *env, struct cl_object *obj,
41                   struct cl_page *page, pgoff_t index)
42 {
43         return -ENOTSUPP;
44 }
45
46 int mdc_lock_init(const struct lu_env *env,
47                   struct cl_object *obj, struct cl_lock *lock,
48                   const struct cl_io *unused)
49 {
50         return -ENOTSUPP;
51 }
52
53 int mdc_io_init(const struct lu_env *env,
54                 struct cl_object *obj, struct cl_io *io)
55 {
56         return -ENOTSUPP;
57 }
58
59 /**
60  * Implementation of struct cl_req_operations::cro_attr_set() for MDC
61  * layer. MDC is responsible for struct obdo::o_id and struct obdo::o_seq
62  * fields.
63  */
64 static void mdc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
65                              struct cl_req_attr *attr)
66 {
67         u64 flags = attr->cra_flags;
68
69         /* Copy object FID to cl_attr */
70         attr->cra_oa->o_oi.oi_fid = *lu_object_fid(&obj->co_lu);
71
72         if (flags & OBD_MD_FLGROUP)
73                 attr->cra_oa->o_valid |= OBD_MD_FLGROUP;
74
75         if (flags & OBD_MD_FLID)
76                 attr->cra_oa->o_valid |= OBD_MD_FLID;
77 }
78
79 static const struct cl_object_operations mdc_ops = {
80         .coo_page_init = mdc_page_init,
81         .coo_lock_init = mdc_lock_init,
82         .coo_io_init = mdc_io_init,
83         .coo_attr_get = osc_attr_get,
84         .coo_attr_update = osc_attr_update,
85         .coo_glimpse = osc_object_glimpse,
86         .coo_req_attr_set = mdc_req_attr_set,
87 };
88
89 static int mdc_object_init(const struct lu_env *env, struct lu_object *obj,
90                            const struct lu_object_conf *conf)
91 {
92         struct osc_object *osc = lu2osc(obj);
93
94         if (osc->oo_initialized)
95                 return 0;
96
97         osc->oo_initialized = true;
98
99         return osc_object_init(env, obj, conf);
100 }
101
102 static void mdc_object_free(const struct lu_env *env, struct lu_object *obj)
103 {
104         osc_object_free(env, obj);
105 }
106
107 static const struct lu_object_operations mdc_lu_obj_ops = {
108         .loo_object_init = mdc_object_init,
109         .loo_object_delete = NULL,
110         .loo_object_release = NULL,
111         .loo_object_free = mdc_object_free,
112         .loo_object_print = osc_object_print,
113         .loo_object_invariant = NULL
114 };
115
116 struct lu_object *mdc_object_alloc(const struct lu_env *env,
117                                    const struct lu_object_header *unused,
118                                    struct lu_device *dev)
119 {
120         struct osc_object *osc;
121         struct lu_object  *obj;
122
123         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
124         if (osc != NULL) {
125                 obj = osc2lu(osc);
126                 lu_object_init(obj, NULL, dev);
127                 osc->oo_cl.co_ops = &mdc_ops;
128                 obj->lo_ops = &mdc_lu_obj_ops;
129                 osc->oo_initialized = false;
130         } else {
131                 obj = NULL;
132         }
133         return obj;
134 }
135
136 static int mdc_cl_process_config(const struct lu_env *env,
137                                  struct lu_device *d, struct lustre_cfg *cfg)
138 {
139         return mdc_process_config(d->ld_obd, 0, cfg);
140 }
141
142 const struct lu_device_operations mdc_lu_ops = {
143         .ldo_object_alloc = mdc_object_alloc,
144         .ldo_process_config = mdc_cl_process_config,
145         .ldo_recovery_complete = NULL,
146 };
147
148 static struct lu_device *mdc_device_alloc(const struct lu_env *env,
149                                           struct lu_device_type *t,
150                                           struct lustre_cfg *cfg)
151 {
152         struct lu_device *d;
153         struct osc_device *od;
154         struct obd_device *obd;
155         int rc;
156
157         OBD_ALLOC_PTR(od);
158         if (od == NULL)
159                 RETURN(ERR_PTR(-ENOMEM));
160
161         cl_device_init(&od->od_cl, t);
162         d = osc2lu_dev(od);
163         d->ld_ops = &mdc_lu_ops;
164
165         /* Setup MDC OBD */
166         obd = class_name2obd(lustre_cfg_string(cfg, 0));
167         if (obd == NULL)
168                 RETURN(ERR_PTR(-ENODEV));
169
170         rc = mdc_setup(obd, cfg);
171         if (rc < 0) {
172                 osc_device_free(env, d);
173                 RETURN(ERR_PTR(rc));
174         }
175         od->od_exp = obd->obd_self_export;
176         RETURN(d);
177 }
178
179 static const struct lu_device_type_operations mdc_device_type_ops = {
180         .ldto_device_alloc = mdc_device_alloc,
181         .ldto_device_free = osc_device_free,
182         .ldto_device_init = osc_device_init,
183         .ldto_device_fini = osc_device_fini
184 };
185
186 struct lu_device_type mdc_device_type = {
187         .ldt_tags = LU_DEVICE_CL,
188         .ldt_name = LUSTRE_MDC_NAME,
189         .ldt_ops = &mdc_device_type_ops,
190         .ldt_ctx_tags = LCT_CL_THREAD
191 };
192
193 /** @} osc */