Whamcloud - gitweb
LU-15279 ptlrpc: use a cached value
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpc_internal.h
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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 /* Intramodule declarations for ptlrpc. */
33
34 #ifndef PTLRPC_INTERNAL_H
35 #define PTLRPC_INTERNAL_H
36
37 #include "../ldlm/ldlm_internal.h"
38 #include "heap.h"
39
40 struct ldlm_namespace;
41 struct obd_import;
42 struct ldlm_res_id;
43 struct ptlrpc_request_set;
44 extern int test_req_buffer_pressure;
45 extern struct list_head ptlrpc_all_services;
46 extern struct mutex ptlrpc_all_services_mutex;
47 extern struct ptlrpc_nrs_pol_conf nrs_conf_fifo;
48 extern struct ptlrpc_nrs_pol_conf nrs_conf_delay;
49
50 #ifdef HAVE_SERVER_SUPPORT
51 extern struct ptlrpc_nrs_pol_conf nrs_conf_crrn;
52 extern struct ptlrpc_nrs_pol_conf nrs_conf_orr;
53 extern struct ptlrpc_nrs_pol_conf nrs_conf_trr;
54 extern struct ptlrpc_nrs_pol_conf nrs_conf_tbf;
55 #endif /* HAVE_SERVER_SUPPORT */
56
57 /**
58  * \addtogoup nrs
59  * @{
60  */
61 extern struct nrs_core nrs_core;
62
63 extern struct mutex ptlrpcd_mutex;
64 extern struct mutex pinger_mutex;
65
66 extern lnet_handler_t ptlrpc_handler;
67 extern struct percpu_ref ptlrpc_pending;
68
69 /* ptlrpcd.c */
70 int ptlrpcd_start(struct ptlrpcd_ctl *pc);
71
72 /* client.c */
73 void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
74                                timeout_t service_timeout);
75 struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned npages, unsigned max_brw,
76                                          enum ptlrpc_bulk_op_type type,
77                                          unsigned portal,
78                                          const struct ptlrpc_bulk_frag_ops
79                                                 *ops);
80 int ptlrpc_request_cache_init(void);
81 void ptlrpc_request_cache_fini(void);
82 struct ptlrpc_request *ptlrpc_request_cache_alloc(gfp_t flags);
83 void ptlrpc_request_cache_free(struct ptlrpc_request *req);
84 void ptlrpc_init_xid(void);
85 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
86                             struct ptlrpc_request *req);
87 void ptlrpc_expired_set(struct ptlrpc_request_set *set);
88 time64_t ptlrpc_set_next_timeout(struct ptlrpc_request_set *);
89 void ptlrpc_resend_req(struct ptlrpc_request *request);
90 void ptlrpc_set_mbits(struct ptlrpc_request *req);
91 void ptlrpc_assign_next_xid_nolock(struct ptlrpc_request *req);
92 __u64 ptlrpc_known_replied_xid(struct obd_import *imp);
93 void ptlrpc_add_unreplied(struct ptlrpc_request *req);
94
95 /* events.c */
96 int ptlrpc_init_portals(void);
97 void ptlrpc_exit_portals(void);
98
99 void ptlrpc_request_handle_notconn(struct ptlrpc_request *);
100 void lustre_assert_wire_constants(void);
101 int ptlrpc_import_in_recovery(struct obd_import *imp);
102 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt,
103                              bool invalid);
104 void ptlrpc_handle_failed_import(struct obd_import *imp);
105 int ptlrpc_replay_next(struct obd_import *imp, int *inflight);
106 void ptlrpc_initiate_recovery(struct obd_import *imp);
107
108 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset);
109 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset);
110
111 int ptlrpc_sysfs_register_service(struct kset *parent,
112                                   struct ptlrpc_service *svc);
113 void ptlrpc_sysfs_unregister_service(struct ptlrpc_service *svc);
114
115 void ptlrpc_ldebugfs_register_service(struct dentry *debugfs_entry,
116                                       struct ptlrpc_service *svc);
117 #ifdef CONFIG_PROC_FS
118 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc);
119 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req, long amount);
120 void ptlrpc_lprocfs_do_request_stat (struct ptlrpc_request *req,
121                                      long q_usec, long work_usec);
122 #else
123 #define ptlrpc_lprocfs_unregister_service(params...) do{}while(0)
124 #define ptlrpc_lprocfs_rpc_sent(params...) do{}while(0)
125 #define ptlrpc_lprocfs_do_request_stat(params...) do{}while(0)
126 #endif /* CONFIG_PROC_FS */
127
128 /* NRS */
129
130 /**
131  * NRS core object.
132  *
133  * Holds NRS core fields.
134  */
135 struct nrs_core {
136         /**
137          * Protects nrs_core::nrs_policies, serializes external policy
138          * registration/unregistration, and NRS core lprocfs operations.
139          */
140         struct mutex nrs_mutex;
141         /**
142          * List of all policy descriptors registered with NRS core; protected
143          * by nrs_core::nrs_mutex.
144          */
145         struct list_head nrs_policies;
146 };
147
148 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc);
149 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc);
150
151 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
152                                struct ptlrpc_request *req, bool hp);
153 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req);
154 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req);
155 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
156                         struct ptlrpc_request *req, bool hp);
157
158 struct ptlrpc_request *
159 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
160                            bool peek, bool force);
161
162 static inline struct ptlrpc_request *
163 ptlrpc_nrs_req_get_nolock(struct ptlrpc_service_part *svcpt, bool hp,
164                           bool force)
165 {
166         return ptlrpc_nrs_req_get_nolock0(svcpt, hp, false, force);
167 }
168
169 static inline struct ptlrpc_request *
170 ptlrpc_nrs_req_peek_nolock(struct ptlrpc_service_part *svcpt, bool hp)
171 {
172         return ptlrpc_nrs_req_get_nolock0(svcpt, hp, true, false);
173 }
174
175 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req);
176 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp);
177 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
178                                       bool hp);
179
180 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
181                               enum ptlrpc_nrs_queue_type queue, char *name,
182                               enum ptlrpc_nrs_ctl opc, bool single, void *arg);
183
184 int ptlrpc_nrs_init(void);
185 void ptlrpc_nrs_fini(void);
186
187 static inline bool nrs_svcpt_has_hp(const struct ptlrpc_service_part *svcpt)
188 {
189         return svcpt->scp_nrs_hp != NULL;
190 }
191
192 static inline bool nrs_svc_has_hp(const struct ptlrpc_service *svc)
193 {
194         /**
195          * If the first service partition has an HP NRS head, all service
196          * partitions will.
197          */
198         return nrs_svcpt_has_hp(svc->srv_parts[0]);
199 }
200
201 static inline
202 struct ptlrpc_nrs *nrs_svcpt2nrs(struct ptlrpc_service_part *svcpt, bool hp)
203 {
204         LASSERT(ergo(hp, nrs_svcpt_has_hp(svcpt)));
205         return hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
206 }
207
208 static inline int nrs_pol2cptid(const struct ptlrpc_nrs_policy *policy)
209 {
210         return policy->pol_nrs->nrs_svcpt->scp_cpt;
211 }
212
213 static inline
214 struct ptlrpc_service *nrs_pol2svc(struct ptlrpc_nrs_policy *policy)
215 {
216         return policy->pol_nrs->nrs_svcpt->scp_service;
217 }
218
219 static inline
220 struct ptlrpc_service_part *nrs_pol2svcpt(struct ptlrpc_nrs_policy *policy)
221 {
222         return policy->pol_nrs->nrs_svcpt;
223 }
224
225 static inline
226 struct cfs_cpt_table *nrs_pol2cptab(struct ptlrpc_nrs_policy *policy)
227 {
228         return nrs_pol2svc(policy)->srv_cptable;
229 }
230
231 static inline struct ptlrpc_nrs_resource *
232 nrs_request_resource(struct ptlrpc_nrs_request *nrq)
233 {
234         LASSERT(nrq->nr_initialized);
235         LASSERT(!nrq->nr_finalized);
236
237         return nrq->nr_res_ptrs[nrq->nr_res_idx];
238 }
239
240 static inline
241 struct ptlrpc_nrs_policy *nrs_request_policy(struct ptlrpc_nrs_request *nrq)
242 {
243         return nrs_request_resource(nrq)->res_policy;
244 }
245
246 #define NRS_LPROCFS_QUANTUM_NAME_REG    "reg_quantum:"
247 #define NRS_LPROCFS_QUANTUM_NAME_HP     "hp_quantum:"
248
249 /**
250  * the maximum size of nrs_crrn_client::cc_quantum and nrs_orr_data::od_quantum.
251  */
252 #define LPROCFS_NRS_QUANTUM_MAX         65535
253
254 /**
255  * Max valid command string is the size of the labels, plus "65535" twice, plus
256  * a separating space character.
257  */
258 #define LPROCFS_NRS_WR_QUANTUM_MAX_CMD                                         \
259  sizeof(NRS_LPROCFS_QUANTUM_NAME_REG __stringify(LPROCFS_NRS_QUANTUM_MAX) " "  \
260         NRS_LPROCFS_QUANTUM_NAME_HP __stringify(LPROCFS_NRS_QUANTUM_MAX))
261
262 /* recovd_thread.c */
263
264 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink);
265
266 /* pers.c */
267 void ptlrpc_fill_bulk_md(struct lnet_md *md, struct ptlrpc_bulk_desc *desc,
268                          int mdcnt);
269
270 /* pack_generic.c */
271 struct ptlrpc_reply_state *
272 lustre_get_emerg_rs(struct ptlrpc_service_part *svcpt);
273 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs);
274 void lustre_msg_early_size_init(void); /* just for init */
275
276 /* pinger.c */
277 int ptlrpc_start_pinger(void);
278 int ptlrpc_stop_pinger(void);
279 void ptlrpc_pinger_sending_on_import(struct obd_import *imp);
280 void ptlrpc_pinger_commit_expected(struct obd_import *imp);
281 void ptlrpc_pinger_wake_up(void);
282 void ptlrpc_ping_import_soon(struct obd_import *imp);
283 int ping_evictor_wake(struct obd_export *exp);
284
285 /* sec_null.c */
286 int  sptlrpc_null_init(void);
287 void sptlrpc_null_fini(void);
288
289 /* sec_plain.c */
290 int  sptlrpc_plain_init(void);
291 void sptlrpc_plain_fini(void);
292
293 /* sec_bulk.c */
294 int  sptlrpc_enc_pool_init(void);
295 void sptlrpc_enc_pool_fini(void);
296 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v);
297
298 /* sec_lproc.c */
299 int  sptlrpc_lproc_init(void);
300 void sptlrpc_lproc_fini(void);
301
302 /* sec_gc.c */
303 int sptlrpc_gc_init(void);
304 void sptlrpc_gc_fini(void);
305
306 /* sec_config.c */
307 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
308                                 enum lustre_sec_part to,
309                                 struct obd_uuid *target,
310                                 lnet_nid_t nid,
311                                 struct sptlrpc_flavor *sf);
312 int  sptlrpc_conf_init(void);
313 void sptlrpc_conf_fini(void);
314
315 /* sec.c */
316 int  sptlrpc_init(void);
317 void sptlrpc_fini(void);
318
319 /* layout.c */
320 __u32 __req_capsule_offset(const struct req_capsule *pill,
321                            const struct req_msg_field *field,
322                            enum req_location loc);
323
324 static inline bool ptlrpc_recoverable_error(int rc)
325 {
326         return (rc == -ENOTCONN || rc == -ENODEV);
327 }
328
329 #ifdef HAVE_SERVER_SUPPORT
330 int tgt_mod_init(void);
331 void tgt_mod_exit(void);
332 int nodemap_mod_init(void);
333 void nodemap_mod_exit(void);
334 #else /* HAVE_SERVER_SUPPORT */
335 static inline int tgt_mod_init(void)
336 {
337         return 0;
338 }
339
340 static inline void tgt_mod_exit(void)
341 {
342         return;
343 }
344
345 static inline int nodemap_mod_init(void)
346 {
347         return 0;
348 }
349
350 static inline void nodemap_mod_exit(void)
351 {
352         return;
353 }
354 #endif /* !HAVE_SERVER_SUPPORT */
355
356 static inline void ptlrpc_reqset_put(struct ptlrpc_request_set *set)
357 {
358         if (atomic_dec_and_test(&set->set_refcount))
359                 OBD_FREE_PTR(set);
360 }
361
362 /** initialise ptlrpc common fields */
363 static inline void ptlrpc_req_comm_init(struct ptlrpc_request *req)
364 {
365         spin_lock_init(&req->rq_lock);
366         spin_lock_init(&req->rq_early_free_lock);
367         atomic_set(&req->rq_refcount, 1);
368         INIT_LIST_HEAD(&req->rq_list);
369         INIT_LIST_HEAD(&req->rq_replay_list);
370 }
371
372 /** initialise client side ptlrpc request */
373 static inline void ptlrpc_cli_req_init(struct ptlrpc_request *req)
374 {
375         struct ptlrpc_cli_req *cr = &req->rq_cli;
376
377         ptlrpc_req_comm_init(req);
378
379         req->rq_receiving_reply = 0;
380         req->rq_req_unlinked = req->rq_reply_unlinked = 1;
381
382         INIT_LIST_HEAD(&cr->cr_set_chain);
383         INIT_LIST_HEAD(&cr->cr_ctx_chain);
384         INIT_LIST_HEAD(&cr->cr_unreplied_list);
385         init_waitqueue_head(&cr->cr_reply_waitq);
386         init_waitqueue_head(&cr->cr_set_waitq);
387 }
388
389 /** initialise server side ptlrpc request */
390 static inline void ptlrpc_srv_req_init(struct ptlrpc_request *req)
391 {
392         struct ptlrpc_srv_req *sr = &req->rq_srv;
393
394         ptlrpc_req_comm_init(req);
395         req->rq_srv_req = 1;
396         INIT_LIST_HEAD(&sr->sr_exp_list);
397         INIT_LIST_HEAD(&sr->sr_timed_list);
398         INIT_LIST_HEAD(&sr->sr_hist_list);
399 }
400
401 static inline bool ptlrpc_req_is_connect(struct ptlrpc_request *req)
402 {
403         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CONNECT ||
404             lustre_msg_get_opc(req->rq_reqmsg) == OST_CONNECT ||
405             lustre_msg_get_opc(req->rq_reqmsg) == MGS_CONNECT)
406                 return true;
407         else
408                 return false;
409 }
410
411 static inline bool ptlrpc_req_is_disconnect(struct ptlrpc_request *req)
412 {
413         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_DISCONNECT ||
414             lustre_msg_get_opc(req->rq_reqmsg) == OST_DISCONNECT ||
415             lustre_msg_get_opc(req->rq_reqmsg) == MGS_DISCONNECT)
416                 return true;
417         else
418                 return false;
419 }
420
421 static inline void do_pack_body(struct ptlrpc_request *req)
422 {
423         struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
424                                                     &RMF_MDT_BODY);
425
426         if (b == NULL)
427                 return;
428
429         b->mbo_valid = 0;
430         b->mbo_eadatasize = 0;
431         b->mbo_flags = 0;
432         b->mbo_suppgid = -1;
433         b->mbo_uid = from_kuid(&init_user_ns, current_uid());
434         b->mbo_gid = from_kgid(&init_user_ns, current_gid());
435         b->mbo_fsuid = from_kuid(&init_user_ns, current_fsuid());
436         b->mbo_fsgid = from_kgid(&init_user_ns, current_fsgid());
437         b->mbo_capability = current_cap().cap[0];
438 }
439
440 #endif /* PTLRPC_INTERNAL_H */