Whamcloud - gitweb
d0580c2c24736d1f23776213bee6f6fddf445bbb
[fs/lustre-release.git] / lustre / include / lu_target.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef _LUSTRE_LU_TARGET_H
38 #define _LUSTRE_LU_TARGET_H
39
40 #include <dt_object.h>
41 #include <lustre_export.h>
42 #include <lustre_update.h>
43 #include <lustre_disk.h>
44 #include <lustre_lfsck.h>
45
46 struct lu_target {
47         struct obd_device       *lut_obd;
48         struct dt_device        *lut_bottom;
49
50         /* supported opcodes and handlers for this target */
51         struct tgt_opc_slice    *lut_slice;
52         __u32                    lut_reply_fail_id;
53         __u32                    lut_request_fail_id;
54
55         /* sptlrpc rules */
56         rwlock_t                 lut_sptlrpc_lock;
57         struct sptlrpc_rule_set  lut_sptlrpc_rset;
58         spinlock_t               lut_flags_lock;
59         int                      lut_sec_level;
60         unsigned int             lut_syncjournal:1,
61                                  lut_sync_lock_cancel:2,
62                                  /* e.g. OST node */
63                                  lut_no_reconstruct:1;
64         /** last_rcvd file */
65         struct dt_object        *lut_last_rcvd;
66         /* transaction callbacks */
67         struct dt_txn_callback   lut_txn_cb;
68         /** server data in last_rcvd file */
69         struct lr_server_data    lut_lsd;
70         /** Server last transaction number */
71         __u64                    lut_last_transno;
72         /** Lock protecting last transaction number */
73         spinlock_t               lut_translock;
74         /** Lock protecting client bitmap */
75         spinlock_t               lut_client_bitmap_lock;
76         /** Bitmap of known clients */
77         unsigned long           *lut_client_bitmap;
78 };
79
80 extern struct lu_context_key tgt_session_key;
81
82 struct tgt_session_info {
83         /*
84          * The following members will be filled explicitly
85          * with specific data in tgt_ses_init().
86          */
87         struct req_capsule      *tsi_pill;
88
89         /*
90          * Lock request for "habeo clavis" operations.
91          */
92         struct ldlm_request     *tsi_dlm_req;
93
94         /* although we have export in req, there are cases when it is not
95          * available, e.g. closing files upon export destroy */
96         struct obd_export       *tsi_exp;
97         const struct lu_env     *tsi_env;
98         struct lu_target        *tsi_tgt;
99
100         const struct mdt_body   *tsi_mdt_body;
101         struct ost_body         *tsi_ost_body;
102         struct lu_object        *tsi_corpus;
103
104         struct lu_fid            tsi_fid;
105         struct ldlm_res_id       tsi_resid;
106
107         /* object affected by VBR, for last_rcvd_update */
108         struct dt_object        *tsi_vbr_obj;
109         /* opdata for mdt_reint_open(), has the same value as
110          * ldlm_reply:lock_policy_res1.  The tgt_update_last_rcvd() stores
111          * this value onto disk for recovery when tgt_txn_stop_cb() is called.
112          */
113         __u64                    tsi_opdata;
114
115         /*
116          * Additional fail id that can be set by handler.
117          */
118         int                      tsi_reply_fail_id;
119         bool                     tsi_preprocessed;
120         /* request JobID */
121         char                    *tsi_jobid;
122 };
123
124 static inline struct tgt_session_info *tgt_ses_info(const struct lu_env *env)
125 {
126         struct tgt_session_info *tsi;
127
128         LASSERT(env->le_ses != NULL);
129         tsi = lu_context_key_get(env->le_ses, &tgt_session_key);
130         LASSERT(tsi);
131         return tsi;
132 }
133
134 static inline void tgt_vbr_obj_set(const struct lu_env *env,
135                                    struct dt_object *obj)
136 {
137         struct tgt_session_info *tsi;
138
139         if (env->le_ses != NULL) {
140                 tsi = tgt_ses_info(env);
141                 tsi->tsi_vbr_obj = obj;
142         }
143 }
144
145 static inline void tgt_opdata_set(const struct lu_env *env, __u64 flags)
146 {
147         struct tgt_session_info *tsi;
148
149         if (env->le_ses != NULL) {
150                 tsi = tgt_ses_info(env);
151                 tsi->tsi_opdata |= flags;
152         }
153 }
154
155 static inline void tgt_opdata_clear(const struct lu_env *env, __u64 flags)
156 {
157         struct tgt_session_info *tsi;
158
159         if (env->le_ses != NULL) {
160                 tsi = tgt_ses_info(env);
161                 tsi->tsi_opdata &= ~flags;
162         }
163 }
164
165 /*
166  * Generic unified target support.
167  */
168 enum tgt_handler_flags {
169         /*
170          * struct *_body is passed in the incoming message, and object
171          * identified by this fid exists on disk.
172          *                            *
173          * "habeo corpus" == "I have a body"
174          */
175         HABEO_CORPUS = (1 << 0),
176         /*
177          * struct ldlm_request is passed in the incoming message.
178          *
179          * "habeo clavis" == "I have a key"
180          *                                     */
181         HABEO_CLAVIS = (1 << 1),
182         /*
183          * this request has fixed reply format, so that reply message can be
184          * packed by generic code.
185          *
186          * "habeo refero" == "I have a reply"
187          */
188         HABEO_REFERO = (1 << 2),
189         /*
190          * this request will modify something, so check whether the file system
191          * is readonly or not, then return -EROFS to client asap if necessary.
192          *
193          * "mutabor" == "I shall modify"
194          */
195         MUTABOR      = (1 << 3)
196 };
197
198 struct tgt_handler {
199         /* The name of this handler. */
200         const char              *th_name;
201         /* Fail id, check at the beginning */
202         int                      th_fail_id;
203         /* Operation code */
204         __u32                    th_opc;
205         /* Flags in enum tgt_handler_flags */
206         __u32                    th_flags;
207         /* Request version for this opcode */
208         int                      th_version;
209         /* Handler function */
210         int                     (*th_act)(struct tgt_session_info *tsi);
211         /* Handler function for high priority requests */
212         void                    (*th_hp)(struct tgt_session_info *tsi);
213         /* Request format for this request */
214         const struct req_format *th_fmt;
215 };
216
217 struct tgt_opc_slice {
218         __u32                    tos_opc_start; /* First op code */
219         __u32                    tos_opc_end; /* Last op code */
220         struct tgt_handler      *tos_hs; /* Registered handler */
221 };
222
223 static inline struct ptlrpc_request *tgt_ses_req(struct tgt_session_info *tsi)
224 {
225         return tsi->tsi_pill ? tsi->tsi_pill->rc_req : NULL;
226 }
227
228 static inline __u64 tgt_conn_flags(struct tgt_session_info *tsi)
229 {
230         LASSERT(tsi->tsi_exp);
231         return exp_connect_flags(tsi->tsi_exp);
232 }
233
234 static inline int req_is_replay(struct ptlrpc_request *req)
235 {
236         LASSERT(req->rq_reqmsg);
237         return !!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY);
238 }
239
240 /* target/tgt_handler.c */
241 int tgt_request_handle(struct ptlrpc_request *req);
242 char *tgt_name(struct lu_target *tgt);
243 void tgt_counter_incr(struct obd_export *exp, int opcode);
244 int tgt_connect_check_sptlrpc(struct ptlrpc_request *req,
245                               struct obd_export *exp);
246 int tgt_adapt_sptlrpc_conf(struct lu_target *tgt, int initial);
247 int tgt_connect(struct tgt_session_info *tsi);
248 int tgt_disconnect(struct tgt_session_info *uti);
249 int tgt_obd_ping(struct tgt_session_info *tsi);
250 int tgt_enqueue(struct tgt_session_info *tsi);
251 int tgt_convert(struct tgt_session_info *tsi);
252 int tgt_bl_callback(struct tgt_session_info *tsi);
253 int tgt_cp_callback(struct tgt_session_info *tsi);
254 int tgt_llog_open(struct tgt_session_info *tsi);
255 int tgt_llog_close(struct tgt_session_info *tsi);
256 int tgt_llog_destroy(struct tgt_session_info *tsi);
257 int tgt_llog_read_header(struct tgt_session_info *tsi);
258 int tgt_llog_next_block(struct tgt_session_info *tsi);
259 int tgt_llog_prev_block(struct tgt_session_info *tsi);
260 int tgt_sec_ctx_init(struct tgt_session_info *tsi);
261 int tgt_sec_ctx_init_cont(struct tgt_session_info *tsi);
262 int tgt_sec_ctx_fini(struct tgt_session_info *tsi);
263 int tgt_sendpage(struct tgt_session_info *tsi, struct lu_rdpg *rdpg, int nob);
264 int tgt_validate_obdo(struct tgt_session_info *tsi, struct obdo *oa);
265 int tgt_sync(const struct lu_env *env, struct lu_target *tgt,
266              struct dt_object *obj, __u64 start, __u64 end);
267
268 int tgt_io_thread_init(struct ptlrpc_thread *thread);
269 void tgt_io_thread_done(struct ptlrpc_thread *thread);
270
271 int tgt_extent_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
272                     __u64 start, __u64 end, struct lustre_handle *lh,
273                     int mode, __u64 *flags);
274 void tgt_extent_unlock(struct lustre_handle *lh, ldlm_mode_t mode);
275 int tgt_brw_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
276                  struct obd_ioobj *obj, struct niobuf_remote *nb,
277                  struct lustre_handle *lh, int mode);
278 void tgt_brw_unlock(struct obd_ioobj *obj, struct niobuf_remote *niob,
279                     struct lustre_handle *lh, int mode);
280 int tgt_brw_read(struct tgt_session_info *tsi);
281 int tgt_brw_write(struct tgt_session_info *tsi);
282 int tgt_hpreq_handler(struct ptlrpc_request *req);
283 void tgt_register_lfsck_in_notify(int (*notify)(const struct lu_env *,
284                                                 struct dt_device *,
285                                                 struct lfsck_request *,
286                                                 struct thandle *));
287 void tgt_register_lfsck_query(int (*query)(const struct lu_env *,
288                                            struct dt_device *,
289                                            struct lfsck_request *));
290
291 extern struct tgt_handler tgt_sec_ctx_handlers[];
292 extern struct tgt_handler tgt_lfsck_handlers[];
293 extern struct tgt_handler tgt_obd_handlers[];
294 extern struct tgt_handler tgt_dlm_handlers[];
295 extern struct tgt_handler tgt_llog_handlers[];
296 extern struct tgt_handler tgt_out_handlers[];
297 extern struct tgt_handler fld_handlers[];
298 extern struct tgt_handler seq_handlers[];
299
300 typedef void (*tgt_cb_t)(struct lu_target *lut, __u64 transno,
301                          void *data, int err);
302 struct tgt_commit_cb {
303         tgt_cb_t  tgt_cb_func;
304         void     *tgt_cb_data;
305 };
306
307 int tgt_hpreq_handler(struct ptlrpc_request *req);
308
309 /* target/tgt_main.c */
310 void tgt_boot_epoch_update(struct lu_target *lut);
311 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *lut,
312                            struct obd_export *exp, __u64 transno);
313 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp);
314 int tgt_init(const struct lu_env *env, struct lu_target *lut,
315              struct obd_device *obd, struct dt_device *dt,
316              struct tgt_opc_slice *slice,
317              int request_fail_id, int reply_fail_id);
318 void tgt_fini(const struct lu_env *env, struct lu_target *lut);
319 int tgt_client_alloc(struct obd_export *exp);
320 void tgt_client_free(struct obd_export *exp);
321 int tgt_client_del(const struct lu_env *env, struct obd_export *exp);
322 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int);
323 int tgt_client_new(const struct lu_env *env, struct obd_export *exp);
324 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tg,
325                          struct lsd_client_data *lcd, loff_t *off, int index);
326 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tg,
327                           struct lsd_client_data *lcd, loff_t *off, struct thandle *th);
328 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tg);
329 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tg,
330                           struct thandle *th);
331 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tg,
332                            int sync);
333 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tg,
334                            loff_t off);
335
336 enum {
337         ESERIOUS = 0x0001000
338 };
339
340 static inline int err_serious(int rc)
341 {
342         LASSERT(rc < 0);
343         return -(-rc | ESERIOUS);
344 }
345
346 static inline int clear_serious(int rc)
347 {
348         if (rc < 0)
349                 rc = -(-rc & ~ESERIOUS);
350         return rc;
351 }
352
353 static inline int is_serious(int rc)
354 {
355         return (rc < 0 && -rc & ESERIOUS);
356 }
357
358 /**
359  * Do not return server-side uid/gid to remote client
360  */
361 static inline void tgt_drop_id(struct obd_export *exp, struct obdo *oa)
362 {
363         if (unlikely(exp_connect_rmtclient(exp))) {
364                 oa->o_uid = -1;
365                 oa->o_gid = -1;
366                 oa->o_valid &= ~(OBD_MD_FLUID | OBD_MD_FLGID);
367         }
368 }
369
370 /*
371  * Unified target generic handers macros and generic functions.
372  */
373 #define TGT_RPC_HANDLER_HP(base, flags, opc, fn, hp, fmt, version)      \
374 [opc - base] = {                                                        \
375         .th_name        = #opc,                                         \
376         .th_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
377         .th_opc         = opc,                                          \
378         .th_flags       = flags,                                        \
379         .th_act         = fn,                                           \
380         .th_fmt         = fmt,                                          \
381         .th_version     = version,                                      \
382         .th_hp          = hp,                                           \
383 }
384 #define TGT_RPC_HANDLER(base, flags, opc, fn, fmt, version)             \
385         TGT_RPC_HANDLER_HP(base, flags, opc, fn, NULL, fmt, version)
386
387 /* MDT Request with a format known in advance */
388 #define TGT_MDT_HDL(flags, name, fn)                                    \
389         TGT_RPC_HANDLER(MDS_FIRST_OPC, flags, name, fn, &RQF_ ## name,  \
390                         LUSTRE_MDS_VERSION)
391 /* Request with a format we do not yet know */
392 #define TGT_MDT_HDL_VAR(flags, name, fn)                                \
393         TGT_RPC_HANDLER(MDS_FIRST_OPC, flags, name, fn, NULL,           \
394                         LUSTRE_MDS_VERSION)
395
396 /* OST Request with a format known in advance */
397 #define TGT_OST_HDL(flags, name, fn)                                    \
398         TGT_RPC_HANDLER(OST_FIRST_OPC, flags, name, fn, &RQF_ ## name,  \
399                         LUSTRE_OST_VERSION)
400 #define TGT_OST_HDL_HP(flags, name, fn, hp)                             \
401         TGT_RPC_HANDLER_HP(OST_FIRST_OPC, flags, name, fn, hp,          \
402                            &RQF_ ## name, LUSTRE_OST_VERSION)
403
404 /* MGS request with a format known in advance */
405 #define TGT_MGS_HDL(flags, name, fn)                                    \
406         TGT_RPC_HANDLER(MGS_FIRST_OPC, flags, name, fn, &RQF_ ## name,  \
407                         LUSTRE_MGS_VERSION)
408 #define TGT_MGS_HDL_VAR(flags, name, fn)                                \
409         TGT_RPC_HANDLER(MGS_FIRST_OPC, flags, name, fn, NULL,           \
410                         LUSTRE_MGS_VERSION)
411
412 /*
413  * OBD handler macros and generic functions.
414  */
415 #define TGT_OBD_HDL(flags, name, fn)                                    \
416         TGT_RPC_HANDLER(OBD_FIRST_OPC, flags, name, fn, &RQF_ ## name,  \
417                         LUSTRE_OBD_VERSION)
418 #define TGT_OBD_HDL_VAR(flags, name, fn)                                \
419         TGT_RPC_HANDLER(OBD_FIRST_OPC, flags, name, fn, NULL,           \
420                         LUSTRE_OBD_VERSION)
421
422 /*
423  * DLM handler macros and generic functions.
424  */
425 #define TGT_DLM_HDL_VAR(flags, name, fn)                                \
426         TGT_RPC_HANDLER(LDLM_FIRST_OPC, flags, name, fn, NULL,          \
427                         LUSTRE_DLM_VERSION)
428 #define TGT_DLM_HDL(flags, name, fn)                                    \
429         TGT_RPC_HANDLER(LDLM_FIRST_OPC, flags, name, fn, &RQF_ ## name, \
430                         LUSTRE_DLM_VERSION)
431
432 /*
433  * LLOG handler macros and generic functions.
434  */
435 #define TGT_LLOG_HDL_VAR(flags, name, fn)                               \
436         TGT_RPC_HANDLER(LLOG_FIRST_OPC, flags, name, fn, NULL,          \
437                         LUSTRE_LOG_VERSION)
438 #define TGT_LLOG_HDL(flags, name, fn)                                   \
439         TGT_RPC_HANDLER(LLOG_FIRST_OPC, flags, name, fn, &RQF_ ## name, \
440                         LUSTRE_LOG_VERSION)
441
442 /*
443  * Sec context handler macros and generic functions.
444  */
445 #define TGT_SEC_HDL_VAR(flags, name, fn)                                \
446         TGT_RPC_HANDLER(SEC_FIRST_OPC, flags, name, fn, NULL,           \
447                         LUSTRE_OBD_VERSION)
448
449 #define TGT_QUOTA_HDL(flags, name, fn)                                  \
450         TGT_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name,    \
451                         LUSTRE_MDS_VERSION)
452
453 /* Sequence service handlers */
454 #define TGT_SEQ_HDL(flags, name, fn)                                    \
455         TGT_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name,      \
456                         LUSTRE_MDS_VERSION)
457
458 /* FID Location Database handlers */
459 #define TGT_FLD_HDL_VAR(flags, name, fn)                                \
460         TGT_RPC_HANDLER(FLD_QUERY, flags, name, fn, NULL,               \
461                         LUSTRE_MDS_VERSION)
462
463 /* LFSCK handlers */
464 #define TGT_LFSCK_HDL(flags, name, fn)                                  \
465         TGT_RPC_HANDLER(LFSCK_FIRST_OPC, flags, name, fn,               \
466                         &RQF_ ## name, LUSTRE_OBD_VERSION)
467
468 /* Request with a format known in advance */
469 #define TGT_UPDATE_HDL(flags, name, fn)                                 \
470         TGT_RPC_HANDLER(OUT_UPDATE, flags, name, fn, &RQF_ ## name,     \
471                         LUSTRE_MDS_VERSION)
472
473 #endif /* __LUSTRE_LU_TARGET_H */