Whamcloud - gitweb
LU-6485 libcfs: embed kr_data into kkuc_reg
[fs/lustre-release.git] / lustre / include / lustre_mdc.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * Copyright (c) 2011, 2014, Intel Corporation.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Sun Microsystems, Inc.
36  *
37  * lustre/include/lustre_mdc.h
38  *
39  * MDS data structures.
40  * See also lustre_idl.h for wire formats of requests.
41  */
42
43 #ifndef _LUSTRE_MDC_H
44 #define _LUSTRE_MDC_H
45
46 /** \defgroup mdc mdc
47  *
48  * @{
49  */
50
51 #include <linux/fs.h>
52 #include <linux/dcache.h>
53 #ifdef CONFIG_FS_POSIX_ACL
54 # include <linux/posix_acl_xattr.h>
55 #endif /* CONFIG_FS_POSIX_ACL */
56 #include <lustre_handles.h>
57 #include <lustre_intent.h>
58 #include <libcfs/libcfs.h>
59 #include <obd_class.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_lib.h>
62 #include <lustre_dlm.h>
63 #include <lustre_export.h>
64
65 struct ptlrpc_client;
66 struct obd_export;
67 struct ptlrpc_request;
68 struct obd_device;
69
70 /**
71  * Serializes in-flight MDT-modifying RPC requests to preserve idempotency.
72  *
73  * This mutex is used to implement execute-once semantics on the MDT.
74  * The MDT stores the last transaction ID and result for every client in
75  * its last_rcvd file. If the client doesn't get a reply, it can safely
76  * resend the request and the MDT will reconstruct the reply being aware
77  * that the request has already been executed. Without this lock,
78  * execution status of concurrent in-flight requests would be
79  * overwritten.
80  *
81  * This design limits the extent to which we can keep a full pipeline of
82  * in-flight requests from a single client.  This limitation could be
83  * overcome by allowing multiple slots per client in the last_rcvd file.
84  */
85 struct mdc_rpc_lock {
86         /** Lock protecting in-flight RPC concurrency. */
87         struct mutex            rpcl_mutex;
88         /** Intent associated with currently executing request. */
89         struct lookup_intent    *rpcl_it;
90         /** Used for MDS/RPC load testing purposes. */
91         int                     rpcl_fakes;
92 };
93
94 #define MDC_FAKE_RPCL_IT ((void *)0x2c0012bfUL)
95
96 static inline void mdc_init_rpc_lock(struct mdc_rpc_lock *lck)
97 {
98         mutex_init(&lck->rpcl_mutex);
99         lck->rpcl_it = NULL;
100 }
101
102 static inline void mdc_get_rpc_lock(struct mdc_rpc_lock *lck,
103                                     struct lookup_intent *it)
104 {
105         ENTRY;
106
107         if (it != NULL && (it->it_op == IT_GETATTR || it->it_op == IT_LOOKUP ||
108                            it->it_op == IT_LAYOUT || it->it_op == IT_READDIR))
109                 return;
110
111         /* This would normally block until the existing request finishes.
112          * If fail_loc is set it will block until the regular request is
113          * done, then set rpcl_it to MDC_FAKE_RPCL_IT.  Once that is set
114          * it will only be cleared when all fake requests are finished.
115          * Only when all fake requests are finished can normal requests
116          * be sent, to ensure they are recoverable again. */
117  again:
118         mutex_lock(&lck->rpcl_mutex);
119
120         if (CFS_FAIL_CHECK_QUIET(OBD_FAIL_MDC_RPCS_SEM)) {
121                 lck->rpcl_it = MDC_FAKE_RPCL_IT;
122                 lck->rpcl_fakes++;
123                 mutex_unlock(&lck->rpcl_mutex);
124                 return;
125         }
126
127         /* This will only happen when the CFS_FAIL_CHECK() was
128          * just turned off but there are still requests in progress.
129          * Wait until they finish.  It doesn't need to be efficient
130          * in this extremely rare case, just have low overhead in
131          * the common case when it isn't true. */
132         while (unlikely(lck->rpcl_it == MDC_FAKE_RPCL_IT)) {
133                 mutex_unlock(&lck->rpcl_mutex);
134                 schedule_timeout(cfs_time_seconds(1) / 4);
135                 goto again;
136         }
137
138         LASSERT(lck->rpcl_it == NULL);
139         lck->rpcl_it = it;
140 }
141
142 static inline void mdc_put_rpc_lock(struct mdc_rpc_lock *lck,
143                                     struct lookup_intent *it)
144 {
145         if (it != NULL && (it->it_op == IT_GETATTR || it->it_op == IT_LOOKUP ||
146                            it->it_op == IT_LAYOUT || it->it_op == IT_READDIR))
147                 goto out;
148
149         if (lck->rpcl_it == MDC_FAKE_RPCL_IT) { /* OBD_FAIL_MDC_RPCS_SEM */
150                 mutex_lock(&lck->rpcl_mutex);
151
152                 LASSERTF(lck->rpcl_fakes > 0, "%d\n", lck->rpcl_fakes);
153                 lck->rpcl_fakes--;
154
155                 if (lck->rpcl_fakes == 0)
156                         lck->rpcl_it = NULL;
157
158         } else {
159                 LASSERTF(it == lck->rpcl_it, "%p != %p\n", it, lck->rpcl_it);
160                 lck->rpcl_it = NULL;
161         }
162
163         mutex_unlock(&lck->rpcl_mutex);
164  out:
165         EXIT;
166 }
167
168 static inline void mdc_get_mod_rpc_slot(struct ptlrpc_request *req,
169                                         struct lookup_intent *it)
170 {
171         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
172         __u32 opc;
173         __u16 tag;
174
175         opc = lustre_msg_get_opc(req->rq_reqmsg);
176         tag = obd_get_mod_rpc_slot(cli, opc, it);
177         lustre_msg_set_tag(req->rq_reqmsg, tag);
178 }
179
180 static inline void mdc_put_mod_rpc_slot(struct ptlrpc_request *req,
181                                         struct lookup_intent *it)
182 {
183         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
184         __u32 opc;
185         __u16 tag;
186
187         opc = lustre_msg_get_opc(req->rq_reqmsg);
188         tag = lustre_msg_get_tag(req->rq_reqmsg);
189         obd_put_mod_rpc_slot(cli, opc, it, tag);
190 }
191
192
193 /**
194  * Update the maximum possible easize.
195  *
196  * This value is learned from ptlrpc replies sent by the MDT.  The
197  * default easize is initialized to the minimum value but allowed to
198  * grow up to a single page in size if required to handle the common
199  * case.
200  *
201  * \see client_obd::cl_default_mds_easize
202  *
203  * \param[in] exp       export for MDC device
204  * \param[in] body      body of ptlrpc reply from MDT
205  *
206  */
207 static inline void mdc_update_max_ea_from_body(struct obd_export *exp,
208                                                struct mdt_body *body)
209 {
210         if (body->mbo_valid & OBD_MD_FLMODEASIZE) {
211                 struct client_obd *cli = &exp->exp_obd->u.cli;
212                 __u32 def_easize;
213
214                 if (cli->cl_max_mds_easize < body->mbo_max_mdsize)
215                         cli->cl_max_mds_easize = body->mbo_max_mdsize;
216
217                 def_easize = min_t(__u32, body->mbo_max_mdsize,
218                                    OBD_MAX_DEFAULT_EA_SIZE);
219                 cli->cl_default_mds_easize = def_easize;
220         }
221 }
222
223
224 /* mdc/mdc_locks.c */
225 int it_open_error(int phase, struct lookup_intent *it);
226
227 static inline bool cl_is_lov_delay_create(unsigned int flags)
228 {
229         return  (flags & O_LOV_DELAY_CREATE_1_8) != 0 ||
230                 (flags & O_LOV_DELAY_CREATE_MASK) == O_LOV_DELAY_CREATE_MASK;
231 }
232
233 static inline void cl_lov_delay_create_clear(unsigned int *flags)
234 {
235         if ((*flags & O_LOV_DELAY_CREATE_1_8) != 0)
236                 *flags &= ~O_LOV_DELAY_CREATE_1_8;
237         if ((*flags & O_LOV_DELAY_CREATE_MASK) == O_LOV_DELAY_CREATE_MASK)
238                 *flags &= ~O_LOV_DELAY_CREATE_MASK;
239 }
240
241 /** @} mdc */
242
243 #endif