Motr  M0
fom_simple.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
29 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_FOP
30 
31 #include "lib/misc.h" /* M0_IN, M0_BITS */
32 #include "lib/errno.h" /* ENOMEM */
33 #include "lib/memory.h"
34 #include "lib/locality.h"
35 #include "lib/trace.h" /* M0_ERR */
36 #include "lib/finject.h"
37 #include "reqh/reqh.h"
38 #include "reqh/reqh_service.h"
39 #include "fop/fom.h"
40 #include "fop/fom_simple.h"
41 
42 static const struct m0_fom_type_ops fom_simple_ft_ops;
44 static const struct m0_fom_ops fom_simple_ops;
46 
47 M0_EXTERN struct m0_sm_conf fom_states_conf;
48 
49 M0_INTERNAL void m0_fom_simple_post(struct m0_fom_simple *simpleton,
50  struct m0_reqh *reqh,
51  struct m0_sm_conf *conf,
52  int (*tick)(struct m0_fom *, void *, int *),
53  void (*free)(struct m0_fom_simple *sfom),
54  void *data, size_t locality)
55 {
56  struct m0_fom_type *fomt;
57 
58  if (conf != NULL) {
59  if (conf->scf_trans_nr > 0)
61  } else
63  fomt = &simpleton->si_type;
64  *fomt = (typeof(*fomt)) {
66  .ft_conf = *conf,
67  .ft_state_conf = fom_states_conf,
68  .ft_rstype = &fom_simple_rstype
69  };
70  m0_fom_init(&simpleton->si_fom, fomt, &fom_simple_ops,
71  NULL, NULL, reqh);
72  simpleton->si_data = data;
73  simpleton->si_tick = tick;
74  simpleton->si_free = free;
77  simpleton->si_locality = locality;
78  m0_fom_queue(&simpleton->si_fom);
79 }
80 
81 M0_INTERNAL void m0_fom_simple_hoard(struct m0_fom_simple *cat, size_t nr,
82  struct m0_reqh *reqh,
83  struct m0_sm_conf *conf,
84  int (*tick)(struct m0_fom *, void *,
85  int *),
86  void (*free)(struct m0_fom_simple *sfom),
87  void *data)
88 {
89  size_t i;
90 
91  for (i = 0; i < nr; ++i)
92  m0_fom_simple_post(&cat[i], reqh, conf, tick, free, data, i);
93 }
94 
95 M0_INTERNAL int m0_fom_simples_init(void)
96 {
98 }
99 
100 M0_INTERNAL void m0_fom_simples_fini(void)
101 {
103 }
104 
105 /* fom ops */
106 
107 #define FOM_SIMPLE(fom) (container_of(fom, struct m0_fom_simple, si_fom))
108 
109 static int fom_simple_tick(struct m0_fom *fom)
110 {
111  struct m0_fom_simple *simpleton = FOM_SIMPLE(fom);
112  int result;
113  int phase;
114  bool simple;
115 
117  phase = m0_fom_phase(fom);
118  M0_ASSERT(ergo(simple, phase == M0_FOM_PHASE_INIT));
120  result = simpleton->si_tick(fom, simpleton->si_data, &phase);
121  else
122  result = -ENOENT;
123  M0_ASSERT(ergo(simple, phase == M0_FOM_PHASE_INIT));
124  if (result < 0) {
125  phase = M0_FOM_PHASE_FINISH;
126  result = M0_FSO_WAIT;
127  }
128  m0_fom_phase_set(fom, phase);
129  M0_ASSERT(M0_IN(result, (M0_FSO_WAIT, M0_FSO_AGAIN)));
130  return result;
131 }
132 
133 static size_t fom_simple_locality_get(const struct m0_fom *fom)
134 {
135  return FOM_SIMPLE(fom)->si_locality;
136 }
137 
138 static void fom_simple_fini(struct m0_fom *fom)
139 {
140  struct m0_fom_simple *simpleton = FOM_SIMPLE(fom);
141 
142  m0_fom_fini(fom);
143  if (simpleton->si_free != NULL)
144  simpleton->si_free(simpleton);
145 }
146 
147 static const struct m0_fom_ops fom_simple_ops = {
149  .fo_tick = &fom_simple_tick,
150  .fo_home_locality = &fom_simple_locality_get
151 };
152 
153 /* fom state machine. */
154 
155 static const struct m0_fom_type_ops fom_simple_ft_ops = {
156 };
157 
159  [M0_FOM_PHASE_INIT] = {
160  .sd_name = "working",
162  .sd_flags = M0_SDF_INITIAL
163  },
164  [M0_FOM_PHASE_FINISH] = {
165  .sd_name = "done",
166  .sd_flags = M0_SDF_TERMINAL
167  }
168 };
169 
170 static struct m0_sm_conf fom_simple_conf = {
171  .scf_name = "simple fom",
172  .scf_nr_states = ARRAY_SIZE(fom_simple_phases),
173  .scf_state = fom_simple_phases
174 };
175 
176 
177 /* reqh service. */
178 
180 {
181  return 0;
182 }
183 
185 {
186 }
187 
189 {
190 }
191 
193 {
194  m0_free(service);
195 }
196 
199  .rso_prepare_to_stop = &fom_simple_service_prepare_to_stop,
200  .rso_stop = &fom_simple_service_stop,
201  .rso_fini = &fom_simple_service_fini
202 };
203 
205  const struct m0_reqh_service_type *stype)
206 {
207  struct m0_reqh_service *service;
208 
210  if (service != NULL)
212  *out = service;
213  return service != NULL ? 0 : M0_ERR(-ENOMEM);
214 }
215 
218 };
219 
220 static struct m0_reqh_service_type fom_simple_rstype = {
221  .rst_name = "simple-fom-service",
222  .rst_ops = &fom_simple_rsops,
223  .rst_level = M0_RS_LEVEL_NORMAL
224 };
225 
226 #undef M0_TRACE_SUBSYSTEM
227 
230 /*
231  * Local variables:
232  * c-indentation-style: "K&R"
233  * c-basic-offset: 8
234  * tab-width: 8
235  * fill-column: 80
236  * scroll-step: 1
237  * End:
238  */
239 /*
240  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
241  */
static size_t nr
Definition: dump.c:1505
M0_INTERNAL void m0_sm_conf_init(struct m0_sm_conf *conf)
Definition: sm.c:340
static void tick(struct m0_locality_chore *chore, struct m0_locality *loc, void *place)
Definition: locality.c:326
int(* rso_start)(struct m0_reqh_service *service)
Definition: reqh_service.h:360
#define NULL
Definition: misc.h:38
static size_t locality(const struct m0_fom *fom)
Definition: rm_foms.c:269
#define ergo(a, b)
Definition: misc.h:293
Definition: sm.h:350
static struct m0_reqh_service_type fom_simple_rstype
Definition: fom_simple.c:43
static void fom_simple_service_stop(struct m0_reqh_service *service)
Definition: fom_simple.c:188
struct m0_bufvec data
Definition: di.c:40
static const struct m0_reqh_service_type_ops fom_simple_rsops
Definition: fom_simple.c:216
Definition: conf.py:1
#define M0_BITS(...)
Definition: misc.h:236
static void fom_simple_fini(struct m0_fom *fom)
Definition: fom_simple.c:138
int m0_reqh_service_type_register(struct m0_reqh_service_type *rstype)
Definition: reqh_service.c:473
m0_fom_phase
Definition: fom.h:372
const struct m0_fom_type * fo_type
Definition: dump.c:107
M0_INTERNAL void m0_fom_simples_fini(void)
Definition: fom_simple.c:100
M0_INTERNAL void m0_fom_simple_post(struct m0_fom_simple *simpleton, struct m0_reqh *reqh, struct m0_sm_conf *conf, int(*tick)(struct m0_fom *, void *, int *), void(*free)(struct m0_fom_simple *sfom), void *data, size_t locality)
Definition: fom_simple.c:49
static void fom_simple_service_prepare_to_stop(struct m0_reqh_service *service)
Definition: fom_simple.c:184
static int fom_simple_service_allocate(struct m0_reqh_service **out, const struct m0_reqh_service_type *stype)
Definition: fom_simple.c:204
void m0_fom_init(struct m0_fom *fom, const struct m0_fom_type *fom_type, const struct m0_fom_ops *ops, struct m0_fop *fop, struct m0_fop *reply, struct m0_reqh *reqh)
Definition: fom.c:1372
int i
Definition: dir.c:1033
return M0_ERR(-EOPNOTSUPP)
M0_EXTERN struct m0_sm_conf fom_states_conf
Definition: fom_simple.c:47
size_t si_locality
Definition: fom_simple.h:125
static const struct socktype stype[]
Definition: sock.c:1156
static const struct m0_fom_type_ops fom_simple_ft_ops
Definition: fom_simple.c:42
void m0_fom_fini(struct m0_fom *fom)
Definition: fom.c:1324
#define M0_ASSERT(cond)
const char * scf_name
Definition: sm.h:352
struct crate_conf * conf
int(* si_tick)(struct m0_fom *fom, void *data, int *phase)
Definition: fom_simple.h:121
const char * rst_name
Definition: reqh_service.h:447
static int fom_simple_service_start(struct m0_reqh_service *service)
Definition: fom_simple.c:179
static const struct m0_reqh_service_ops fom_simple_service_ops
Definition: fom_simple.c:197
void(* si_free)(struct m0_fom_simple *sfom)
Definition: fom_simple.h:129
#define FOM_SIMPLE(fom)
Definition: fom_simple.c:107
void * si_data
Definition: fom_simple.h:123
Definition: reqh.h:94
Definition: dump.c:103
M0_INTERNAL void m0_fom_simple_hoard(struct m0_fom_simple *cat, size_t nr, struct m0_reqh *reqh, struct m0_sm_conf *conf, int(*tick)(struct m0_fom *, void *, int *), void(*free)(struct m0_fom_simple *sfom), void *data)
Definition: fom_simple.c:81
M0_INTERNAL struct m0_locality * m0_locality_here(void)
Definition: locality.c:146
struct m0_fom_type si_type
Definition: fom_simple.h:127
struct m0_fom si_fom
Definition: fom_simple.h:120
const struct m0_fom_type_ops * ft_ops
Definition: fom.h:614
static size_t fom_simple_locality_get(const struct m0_fom *fom)
Definition: fom_simple.c:133
M0_INTERNAL int m0_reqh_state_get(struct m0_reqh *reqh)
Definition: reqh.c:398
static void fom_simple_service_fini(struct m0_reqh_service *service)
Definition: fom_simple.c:192
Definition: fom.h:481
struct m0_reqh reqh
Definition: rm_foms.c:48
const char * sd_name
Definition: sm.h:383
int(* rsto_service_allocate)(struct m0_reqh_service **service, const struct m0_reqh_service_type *stype)
Definition: reqh_service.h:435
static struct m0_sm_state_descr fom_simple_phases[]
Definition: fom_simple.c:158
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
static int fom_simple_tick(struct m0_fom *fom)
Definition: fom_simple.c:109
M0_INTERNAL int m0_fom_simples_init(void)
Definition: fom_simple.c:95
size_t lo_idx
Definition: locality.h:68
static const struct m0_fom_ops fom_simple_ops
Definition: fom_simple.c:44
struct m0_sm_conf ft_conf
Definition: fom.h:615
M0_INTERNAL void m0_fom_queue(struct m0_fom *fom)
Definition: fom.c:624
void(* fo_fini)(struct m0_fom *fom)
Definition: fom.h:657
#define out(...)
Definition: gen.c:41
void m0_fom_phase_set(struct m0_fom *fom, int phase)
Definition: fom.c:1688
static struct m0_sm_conf fom_simple_conf
Definition: fom_simple.c:45
void m0_free(void *data)
Definition: memory.c:146
void m0_reqh_service_type_unregister(struct m0_reqh_service_type *rstype)
Definition: reqh_service.c:490
static struct m0_reqh_service * service[REQH_IN_UT_MAX]
Definition: long_lock_ut.c:46
#define ARRAY_SIZE(a)
Definition: misc.h:45
M0_INTERNAL struct m0_reqh * m0_fom_reqh(const struct m0_fom *fom)
Definition: fom.c:283
const struct m0_reqh_service_ops * rs_ops
Definition: reqh_service.h:254