Whamcloud - gitweb
66e6f769f918cb02654341c0f693c0faac1492a6
[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, 2012, 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_disk.h>
43
44 struct lu_target {
45         struct obd_device       *lut_obd;
46         struct dt_device        *lut_bottom;
47
48         /* supported opcodes and handlers for this target */
49         struct tgt_opc_slice    *lut_slice;
50         __u32                    lut_reply_fail_id;
51         __u32                    lut_request_fail_id;
52
53         /* sptlrpc rules */
54         rwlock_t                 lut_sptlrpc_lock;
55         struct sptlrpc_rule_set  lut_sptlrpc_rset;
56         int                      lut_sec_level;
57         unsigned int             lut_mds_capa:1,
58                                  lut_oss_capa:1;
59
60         /* LAST_RCVD parameters */
61         /** last_rcvd file */
62         struct dt_object        *lut_last_rcvd;
63         /* transaction callbacks */
64         struct dt_txn_callback   lut_txn_cb;
65         /** server data in last_rcvd file */
66         struct lr_server_data    lut_lsd;
67         /** Server last transaction number */
68         __u64                    lut_last_transno;
69         /** Lock protecting last transaction number */
70         spinlock_t               lut_translock;
71         /** Lock protecting client bitmap */
72         spinlock_t               lut_client_bitmap_lock;
73         /** Bitmap of known clients */
74         unsigned long           *lut_client_bitmap;
75 };
76
77 extern struct lu_context_key tgt_session_key;
78
79 struct tgt_session_info {
80         /*
81          * The following members will be filled explicitly
82          * with specific data in tgt_ses_init().
83          */
84         struct req_capsule      *tsi_pill;
85
86         /*
87          * Lock request for "habeo clavis" operations.
88          */
89         struct ldlm_request     *tsi_dlm_req;
90
91         /* although we have export in req, there are cases when it is not
92          * available, e.g. closing files upon export destroy */
93         struct obd_export       *tsi_exp;
94         const struct lu_env     *tsi_env;
95         struct lu_target        *tsi_tgt;
96         /*
97          * Additional fail id that can be set by handler.
98          */
99         int                      tsi_reply_fail_id;
100         int                      tsi_request_fail_id;
101 };
102
103 static inline struct tgt_session_info *tgt_ses_info(const struct lu_env *env)
104 {
105         struct tgt_session_info *tsi;
106
107         LASSERT(env->le_ses != NULL);
108         tsi = lu_context_key_get(env->le_ses, &tgt_session_key);
109         LASSERT(tsi);
110         return tsi;
111 }
112
113 /*
114  * Generic unified target support.
115  */
116 enum tgt_handler_flags {
117         /*
118          * struct *_body is passed in the incoming message, and object
119          * identified by this fid exists on disk.
120          *                            *
121          * "habeo corpus" == "I have a body"
122          */
123         HABEO_CORPUS = (1 << 0),
124         /*
125          * struct ldlm_request is passed in the incoming message.
126          *
127          * "habeo clavis" == "I have a key"
128          *                                     */
129         HABEO_CLAVIS = (1 << 1),
130         /*
131          * this request has fixed reply format, so that reply message can be
132          * packed by generic code.
133          *
134          * "habeo refero" == "I have a reply"
135          */
136         HABEO_REFERO = (1 << 2),
137         /*
138          * this request will modify something, so check whether the file system
139          * is readonly or not, then return -EROFS to client asap if necessary.
140          *
141          * "mutabor" == "I shall modify"
142          */
143         MUTABOR      = (1 << 3)
144 };
145
146 struct tgt_handler {
147         /* The name of this handler. */
148         const char              *th_name;
149         /* Fail id, check at the beginning */
150         int                      th_fail_id;
151         /* Operation code */
152         __u32                    th_opc;
153         /* Flags in enum tgt_handler_flags */
154         __u32                    th_flags;
155         /* Request version for this opcode */
156         int                      th_version;
157         /* Handler function */
158         int                     (*th_act)(struct tgt_session_info *tti);
159         /* Request format for this request */
160         const struct req_format *th_fmt;
161 };
162
163 struct tgt_opc_slice {
164         __u32                    tos_opc_start; /* First op code */
165         __u32                    tos_opc_end; /* Last op code */
166         struct tgt_handler      *tos_hs; /* Registered handler */
167 };
168
169 static inline struct ptlrpc_request *tgt_ses_req(struct tgt_session_info *tsi)
170 {
171         return tsi->tsi_pill ? tsi->tsi_pill->rc_req : NULL;
172 }
173
174 static inline __u64 tgt_conn_flags(struct tgt_session_info *tsi)
175 {
176         LASSERT(tsi->tsi_exp);
177         return exp_connect_flags(tsi->tsi_exp);
178 }
179
180 /* target/tgt_handler.c */
181 int tgt_request_handle(struct ptlrpc_request *req);
182 char *tgt_name(struct lu_target *tgt);
183 void tgt_counter_incr(struct obd_export *exp, int opcode);
184 int tgt_connect_check_sptlrpc(struct ptlrpc_request *req,
185                               struct obd_export *exp);
186 int tgt_connect(struct tgt_session_info *tsi);
187 int tgt_disconnect(struct tgt_session_info *uti);
188 int tgt_obd_ping(struct tgt_session_info *tsi);
189 int tgt_enqueue(struct tgt_session_info *tsi);
190 int tgt_convert(struct tgt_session_info *tsi);
191 int tgt_bl_callback(struct tgt_session_info *tsi);
192 int tgt_cp_callback(struct tgt_session_info *tsi);
193 int tgt_llog_open(struct tgt_session_info *tsi);
194 int tgt_llog_close(struct tgt_session_info *tsi);
195 int tgt_llog_destroy(struct tgt_session_info *tsi);
196 int tgt_llog_read_header(struct tgt_session_info *tsi);
197 int tgt_llog_next_block(struct tgt_session_info *tsi);
198 int tgt_llog_prev_block(struct tgt_session_info *tsi);
199 int tgt_sec_ctx_init(struct tgt_session_info *tsi);
200 int tgt_sec_ctx_init_cont(struct tgt_session_info *tsi);
201 int tgt_sec_ctx_fini(struct tgt_session_info *tsi);
202
203 typedef void (*tgt_cb_t)(struct lu_target *lut, __u64 transno,
204                          void *data, int err);
205 struct tgt_commit_cb {
206         tgt_cb_t  tgt_cb_func;
207         void     *tgt_cb_data;
208 };
209
210 /* target/tgt_main.c */
211 void tgt_boot_epoch_update(struct lu_target *lut);
212 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *lut,
213                            struct obd_export *exp, __u64 transno);
214 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp);
215 int tgt_init(const struct lu_env *env, struct lu_target *lut,
216              struct obd_device *obd, struct dt_device *dt,
217              struct tgt_opc_slice *slice,
218              int request_fail_id, int reply_fail_id);
219 void tgt_fini(const struct lu_env *env, struct lu_target *lut);
220 int tgt_client_alloc(struct obd_export *exp);
221 void tgt_client_free(struct obd_export *exp);
222 int tgt_client_del(const struct lu_env *env, struct obd_export *exp);
223 int tgt_client_add(const struct lu_env *env, struct obd_export *exp, int);
224 int tgt_client_new(const struct lu_env *env, struct obd_export *exp);
225 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tg,
226                          struct lsd_client_data *lcd, loff_t *off, int index);
227 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tg,
228                           struct lsd_client_data *lcd, loff_t *off, struct thandle *th);
229 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tg);
230 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tg,
231                           struct thandle *th);
232 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tg, int sync);
233 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tg, loff_t off);
234
235 enum {
236         ESERIOUS = 0x0001000
237 };
238
239 static inline int err_serious(int rc)
240 {
241         LASSERT(rc < 0);
242         return -(-rc | ESERIOUS);
243 }
244
245 static inline int clear_serious(int rc)
246 {
247         if (rc < 0)
248                 rc = -(-rc & ~ESERIOUS);
249         return rc;
250 }
251
252 static inline int is_serious(int rc)
253 {
254         return (rc < 0 && -rc & ESERIOUS);
255 }
256
257 /*
258  * Unified target generic handers macros and generic functions.
259  */
260 #define TGT_RPC_HANDLER(base, flags, opc, fn, fmt, version)             \
261 [opc - base] = {                                                        \
262         .th_name        = #opc,                                         \
263         .th_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
264         .th_opc         = opc,                                          \
265         .th_flags       = flags,                                        \
266         .th_act         = fn,                                           \
267         .th_fmt         = fmt,                                          \
268         .th_version     = version                                       \
269 }
270
271 /* MGS request with a format known in advance */
272 #define TGT_MGS_HDL(flags, name, fn)                                    \
273         TGT_RPC_HANDLER(MGS_FIRST_OPC, flags, name, fn, &RQF_ ## name,\
274                            LUSTRE_MGS_VERSION)
275 #define TGT_MGS_HDL_VAR(flags, name, fn)                                \
276         TGT_RPC_HANDLER(MGS_FIRST_OPC, flags, name, fn, NULL,   \
277                            LUSTRE_MGS_VERSION)
278
279 /*
280  * OBD handler macros and generic functions.
281  */
282 #define TGT_OBD_HDL(flags, name, fn)                                    \
283         TGT_RPC_HANDLER(OBD_FIRST_OPC, flags, name, fn, &RQF_ ## name,\
284                            LUSTRE_OBD_VERSION)
285
286 /*
287  * DLM handler macros and generic functions.
288  */
289 #define TGT_DLM_HDL_VAR(flags, name, fn)                                \
290         TGT_RPC_HANDLER(LDLM_FIRST_OPC, flags, name, fn, NULL,  \
291                            LUSTRE_DLM_VERSION)
292 #define TGT_DLM_HDL(flags, name, fn)                                    \
293         TGT_RPC_HANDLER(LDLM_FIRST_OPC, flags, name, fn, &RQF_ ## name,\
294                            LUSTRE_DLM_VERSION)
295
296 /*
297  * LLOG handler macros and generic functions.
298  */
299 #define TGT_LLOG_HDL_VAR(flags, name, fn)                               \
300         TGT_RPC_HANDLER(LLOG_FIRST_OPC, flags, name, fn, NULL,  \
301                            LUSTRE_LOG_VERSION)
302 #define TGT_LLOG_HDL(flags, name, fn)                                   \
303         TGT_RPC_HANDLER(LLOG_FIRST_OPC, flags, name, fn, &RQF_ ## name,\
304                            LUSTRE_LOG_VERSION)
305
306 /*
307  * Sec context handler macros and generic functions.
308  */
309 #define TGT_SEC_HDL_VAR(flags, name, fn)                                \
310         TGT_RPC_HANDLER(SEC_FIRST_OPC, flags, name, fn, NULL,   \
311                            LUSTRE_OBD_VERSION)
312
313 #endif /* __LUSTRE_LU_TARGET_H */