Whamcloud - gitweb
LU-14462 gss: fix support for namespace in lgss_keyring
[fs/lustre-release.git] / lustre / quota / qsd_request.c
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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2016, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include <lustre_net.h>
34 #include <lustre_import.h>
35 #include <lustre_dlm.h>
36 #include <obd_class.h>
37
38 #include "qsd_internal.h"
39
40 struct qsd_async_args {
41         struct obd_export     *aa_exp;
42         struct qsd_qtype_info *aa_qqi;
43         void                  *aa_arg;
44         struct lquota_lvb     *aa_lvb;
45         struct lustre_handle   aa_lockh;
46         qsd_req_completion_t   aa_completion;
47 };
48
49 /*
50  * non-intent quota request interpret callback.
51  *
52  * \param env    - the environment passed by the caller
53  * \param req    - the non-intent quota request
54  * \param arg    - qsd_async_args
55  * \param rc     - request status
56  *
57  * \retval 0     - success
58  * \retval -ve   - appropriate errors
59  */
60 static int qsd_dqacq_interpret(const struct lu_env *env,
61                                struct ptlrpc_request *req, void *arg, int rc)
62 {
63         struct quota_body     *rep_qbody = NULL, *req_qbody;
64         struct qsd_async_args *aa = (struct qsd_async_args *)arg;
65         ENTRY;
66
67         req_qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
68         if (rc == 0 || rc == -EDQUOT || rc == -EINPROGRESS)
69                 rep_qbody = req_capsule_server_get(&req->rq_pill,
70                                                    &RMF_QUOTA_BODY);
71         aa->aa_completion(env, aa->aa_qqi, req_qbody, rep_qbody, &aa->aa_lockh,
72                           NULL, aa->aa_arg, rc);
73         RETURN(rc);
74 }
75
76 /*
77  * Send non-intent quota request to master.
78  *
79  * \param env    - the environment passed by the caller
80  * \param exp    - is the export to use to send the acquire RPC
81  * \param qbody  - quota body to be packed in request
82  * \param sync   - synchronous or asynchronous
83  * \param completion - completion callback
84  * \param qqi    - is the qsd_qtype_info structure to pass to the completion
85  *                 function
86  * \param lqe    - is the qid entry to be processed
87  *
88  * \retval 0     - success
89  * \retval -ve   - appropriate errors
90  */
91 int qsd_send_dqacq(const struct lu_env *env, struct obd_export *exp,
92                    struct quota_body *qbody, bool sync,
93                    qsd_req_completion_t completion, struct qsd_qtype_info *qqi,
94                    struct lustre_handle *lockh, struct lquota_entry *lqe)
95 {
96         struct ptlrpc_request   *req;
97         struct quota_body       *req_qbody;
98         struct qsd_async_args   *aa;
99         int                      rc;
100         ENTRY;
101
102         LASSERT(exp);
103
104         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_QUOTA_DQACQ);
105         if (req == NULL)
106                 GOTO(out, rc = -ENOMEM);
107
108         req->rq_no_resend = req->rq_no_delay = 1;
109         req->rq_no_retry_einprogress = 1;
110         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, QUOTA_DQACQ);
111         if (rc) {
112                 ptlrpc_request_free(req);
113                 GOTO(out, rc);
114         }
115
116         req->rq_request_portal = MDS_READPAGE_PORTAL;
117         req_qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
118         *req_qbody = *qbody;
119
120         ptlrpc_request_set_replen(req);
121
122         aa = ptlrpc_req_async_args(aa, req);
123         aa->aa_exp = exp;
124         aa->aa_qqi = qqi;
125         aa->aa_arg = (void *)lqe;
126         aa->aa_completion = completion;
127         lustre_handle_copy(&aa->aa_lockh, lockh);
128
129         if (sync) {
130                 rc = ptlrpc_queue_wait(req);
131                 rc = qsd_dqacq_interpret(env, req, aa, rc);
132                 ptlrpc_req_finished(req);
133         } else {
134                 req->rq_interpret_reply = qsd_dqacq_interpret;
135                 ptlrpcd_add_req(req);
136         }
137
138         RETURN(rc);
139 out:
140         completion(env, qqi, qbody, NULL, lockh, NULL, lqe, rc);
141         return rc;
142 }
143
144 /*
145  * intent quota request interpret callback.
146  *
147  * \param env    - the environment passed by the caller
148  * \param req    - the intent quota request
149  * \param arg    - qsd_async_args
150  * \param rc     - request status
151  *
152  * \retval 0     - success
153  * \retval -ve   - appropriate errors
154  */
155 static int qsd_intent_interpret(const struct lu_env *env,
156                                 struct ptlrpc_request *req, void *arg, int rc)
157 {
158         struct lustre_handle     *lockh;
159         struct quota_body        *rep_qbody = NULL, *req_qbody;
160         struct qsd_async_args    *aa = (struct qsd_async_args *)arg;
161         struct ldlm_reply        *lockrep;
162         __u64                     flags = LDLM_FL_HAS_INTENT;
163         struct ldlm_enqueue_info  einfo = {
164                 .ei_type = LDLM_PLAIN,
165                 .ei_mode = LCK_CR,
166         };
167         ENTRY;
168
169         LASSERT(aa->aa_exp);
170         lockh = &aa->aa_lockh;
171         req_qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
172         req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
173
174         rc = ldlm_cli_enqueue_fini(aa->aa_exp, req, &einfo, 0, &flags,
175                                    aa->aa_lvb, sizeof(*(aa->aa_lvb)),
176                                    lockh, rc);
177         if (rc < 0) {
178                 /* the lock has been destroyed, forget about the lock handle */
179                 memset(lockh, 0, sizeof(*lockh));
180                 /*
181                  * To avoid the server being fullfilled by LDLM locks, server
182                  * may reject the locking request by returning -EINPROGRESS,
183                  * this is different from the -EINPROGRESS returned by quota
184                  * code.
185                  */
186                 if (rc == -EINPROGRESS)
187                         rc = -EAGAIN;
188                 GOTO(out, rc);
189         }
190
191         lockrep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
192         LASSERT(lockrep != NULL);
193         rc = ptlrpc_status_ntoh(lockrep->lock_policy_res2);
194
195         if (rc == 0 || rc == -EDQUOT || rc == -EINPROGRESS)
196                 rep_qbody = req_capsule_server_get(&req->rq_pill,
197                                                    &RMF_QUOTA_BODY);
198 out:
199         aa->aa_completion(env, aa->aa_qqi, req_qbody, rep_qbody, lockh,
200                           aa->aa_lvb, aa->aa_arg, rc);
201         RETURN(rc);
202 }
203
204 /*
205  * Get intent per-ID lock or global-index lock from master.
206  *
207  * \param env    - the environment passed by the caller
208  * \param exp    - is the export to use to send the intent RPC
209  * \param qbody  - quota body to be packed in request
210  * \param sync   - synchronous or asynchronous (pre-acquire)
211  * \param it_op  - IT_QUOTA_DQACQ or IT_QUOTA_CONN
212  * \param completion - completion callback
213  * \param qqi    - is the qsd_qtype_info structure to pass to the completion
214  *                 function
215  * \param lvb    - is the lvb associated with the lock and returned by the
216  *                 server
217  * \param arg    - is an opaq argument passed to the completion callback
218  *
219  * \retval 0     - success
220  * \retval -ve   - appropriate errors
221  */
222 int qsd_intent_lock(const struct lu_env *env, struct obd_export *exp,
223                     struct quota_body *qbody, bool sync, int it_op,
224                     qsd_req_completion_t completion, struct qsd_qtype_info *qqi,
225                     struct lquota_lvb *lvb, void *arg)
226 {
227         struct qsd_thread_info  *qti = qsd_info(env);
228         struct ptlrpc_request   *req;
229         struct qsd_async_args   *aa = NULL;
230         struct ldlm_intent      *lit;
231         struct quota_body       *req_qbody;
232         __u64                    flags = LDLM_FL_HAS_INTENT;
233         int                      rc;
234         ENTRY;
235
236         LASSERT(exp != NULL);
237         LASSERT(!lustre_handle_is_used(&qbody->qb_lockh));
238
239         memset(&qti->qti_lockh, 0, sizeof(qti->qti_lockh));
240
241         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
242                                    &RQF_LDLM_INTENT_QUOTA);
243         if (req == NULL)
244                 GOTO(out, rc = -ENOMEM);
245
246         req->rq_no_retry_einprogress = 1;
247         rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
248         if (rc) {
249                 ptlrpc_request_free(req);
250                 GOTO(out, rc);
251         }
252         req->rq_request_portal = MDS_READPAGE_PORTAL;
253
254         lit = req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
255         lit->opc = (__u64)it_op;
256
257         req_qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
258         *req_qbody = *qbody;
259
260         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
261                              sizeof(*lvb));
262         ptlrpc_request_set_replen(req);
263
264         switch(it_op) {
265         case IT_QUOTA_CONN:
266                 /* build resource name associated with global index */
267                 fid_build_reg_res_name(&qbody->qb_fid, &qti->qti_resid);
268
269                 /* copy einfo template and fill ei_cbdata with qqi pointer */
270                 memcpy(&qti->qti_einfo, &qsd_glb_einfo, sizeof(qti->qti_einfo));
271                 qti->qti_einfo.ei_cbdata = qqi;
272
273                 /* don't cancel global lock on memory pressure */
274                 flags |= LDLM_FL_NO_LRU;
275                 break;
276         case IT_QUOTA_DQACQ:
277                 /* build resource name associated for per-ID quota lock */
278                 fid_build_quota_res_name(&qbody->qb_fid, &qbody->qb_id,
279                                          &qti->qti_resid);
280
281                 /* copy einfo template and fill ei_cbdata with lqe pointer */
282                 memcpy(&qti->qti_einfo, &qsd_id_einfo, sizeof(qti->qti_einfo));
283                 qti->qti_einfo.ei_cbdata = arg;
284                 break;
285         default:
286                 LASSERTF(0, "invalid it_op %d\n", it_op);
287         }
288
289         /* build lock enqueue request */
290         rc = ldlm_cli_enqueue(exp, &req, &qti->qti_einfo, &qti->qti_resid, NULL,
291                               &flags, (void *)lvb, sizeof(*lvb), LVB_T_LQUOTA,
292                               &qti->qti_lockh, 1);
293         if (rc < 0) {
294                 ptlrpc_req_finished(req);
295                 GOTO(out, rc);
296         }
297
298         /* grab reference on backend structure for the new lock */
299         switch(it_op) {
300         case IT_QUOTA_CONN:
301                 /* grab reference on qqi for new lock */
302 #ifdef USE_LU_REF
303         {
304                 struct ldlm_lock        *lock;
305
306                 lock = ldlm_handle2lock(&qti->qti_lockh);
307                 if (lock == NULL) {
308                         ptlrpc_req_finished(req);
309                         GOTO(out, rc = -ENOLCK);
310                 }
311                 lu_ref_add(&qqi->qqi_reference, "glb_lock", lock);
312                 LDLM_LOCK_PUT(lock);
313         }
314 #endif
315                 qqi_getref(qqi);
316                 break;
317         case IT_QUOTA_DQACQ:
318                 /* grab reference on lqe for new lock */
319                 lqe_getref((struct lquota_entry *)arg);
320                 /* all acquire/release request are sent with no_resend and
321                  * no_delay flag */
322                 req->rq_no_resend = req->rq_no_delay = 1;
323                 break;
324         default:
325                 break;
326         }
327
328         aa = ptlrpc_req_async_args(aa, req);
329         aa->aa_exp = exp;
330         aa->aa_qqi = qqi;
331         aa->aa_arg = arg;
332         aa->aa_lvb = lvb;
333         aa->aa_completion = completion;
334         lustre_handle_copy(&aa->aa_lockh, &qti->qti_lockh);
335
336         if (sync) {
337                 /* send lock enqueue request and wait for completion */
338                 rc = ptlrpc_queue_wait(req);
339                 rc = qsd_intent_interpret(env, req, aa, rc);
340                 ptlrpc_req_finished(req);
341         } else {
342                 /* queue lock request and return */
343                 req->rq_interpret_reply = qsd_intent_interpret;
344                 ptlrpcd_add_req(req);
345         }
346
347         RETURN(rc);
348 out:
349         completion(env, qqi, qbody, NULL, &qti->qti_lockh, lvb, arg, rc);
350         return rc;
351 }
352
353 /*
354  * Fetch a global or slave index from the QMT.
355  *
356  * \param env    - the environment passed by the caller
357  * \param exp    - is the export to use to issue the OBD_IDX_READ RPC
358  * \param ii     - is the index information to be packed in the request
359  *                 on success, the index information returned by the server
360  *                 is copied there.
361  * \param npages - is the number of pages in the pages array
362  * \param pages  - is an array of @npages pages
363  *
364  * \retval 0     - success
365  * \retval -ve   - appropriate errors
366  */
367 int qsd_fetch_index(const struct lu_env *env, struct obd_export *exp,
368                     struct idx_info *ii, unsigned int npages,
369                     struct page **pages, bool *need_swab)
370 {
371         struct ptlrpc_request   *req;
372         struct idx_info         *req_ii;
373         struct ptlrpc_bulk_desc *desc;
374         int                      rc, i;
375         ENTRY;
376
377         LASSERT(exp);
378
379         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_OBD_IDX_READ);
380         if (req == NULL)
381                 RETURN(-ENOMEM);
382
383         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
384         if (rc) {
385                 ptlrpc_request_free(req);
386                 RETURN(rc);
387         }
388
389         req->rq_request_portal = MDS_READPAGE_PORTAL;
390         ptlrpc_at_set_req_timeout(req);
391
392         /* allocate bulk descriptor */
393         desc = ptlrpc_prep_bulk_imp(req, npages, 1,
394                                     PTLRPC_BULK_PUT_SINK,
395                                     MDS_BULK_PORTAL,
396                                     &ptlrpc_bulk_kiov_pin_ops);
397         if (desc == NULL)
398                 GOTO(out, rc = -ENOMEM);
399
400         /* req now owns desc and will free it when it gets freed */
401         for (i = 0; i < npages; i++)
402                 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
403                                                  PAGE_SIZE);
404
405         /* pack index information in request */
406         req_ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
407         *req_ii = *ii;
408
409         ptlrpc_request_set_replen(req);
410
411         /* send request to master and wait for RPC to complete */
412         rc = ptlrpc_queue_wait(req);
413         if (rc)
414                 GOTO(out, rc);
415
416         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
417                                           req->rq_bulk->bd_nob_transferred);
418         if (rc < 0)
419                 GOTO(out, rc);
420         else
421                 /* sptlrpc_cli_unwrap_bulk_read() returns the number of bytes
422                  * transferred*/
423                 rc = 0;
424
425         req_ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
426         *ii = *req_ii;
427
428         *need_swab = ptlrpc_rep_need_swab(req);
429
430         EXIT;
431 out:
432         ptlrpc_req_finished(req);
433         return rc;
434 }