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