Whamcloud - gitweb
cfd6aa182ca4ed8a3b61331d1ddb03f5f6909b1e
[fs/lustre-release.git] / lustre / mdt / mdt_batch.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, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2020, DDN Storage Corporation.
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  */
28 /*
29  * lustre/mdt/mdt_batch.c
30  *
31  * Batch Metadata Updating on the server (MDT)
32  *
33  * Author: Qian Yingjin <qian@ddn.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_MDS
37
38 #include <linux/module.h>
39
40 #include <lustre_mds.h>
41 #include "mdt_internal.h"
42
43 static struct ldlm_callback_suite mdt_dlm_cbs = {
44         .lcs_completion = ldlm_server_completion_ast,
45         .lcs_blocking   = tgt_blocking_ast,
46         .lcs_glimpse    = ldlm_server_glimpse_ast
47 };
48
49 static int mdt_batch_unpack(struct mdt_thread_info *info, __u32 opc)
50 {
51         int rc = 0;
52
53         switch (opc) {
54         case BUT_GETATTR:
55                 info->mti_dlm_req = req_capsule_client_get(info->mti_pill,
56                                                            &RMF_DLM_REQ);
57                 if (info->mti_dlm_req == NULL)
58                         RETURN(-EFAULT);
59                 break;
60         default:
61                 rc = -EOPNOTSUPP;
62                 CERROR("%s: Unexpected opcode %d: rc = %d\n",
63                        mdt_obd_name(info->mti_mdt), opc, rc);
64                 break;
65         }
66
67         RETURN(rc);
68 }
69
70 static int mdt_batch_pack_repmsg(struct mdt_thread_info *info)
71 {
72         return 0;
73 }
74
75 typedef int (*mdt_batch_reconstructor)(struct tgt_session_info *tsi);
76
77 static mdt_batch_reconstructor reconstructors[BUT_LAST_OPC];
78
79 static int mdt_batch_reconstruct(struct tgt_session_info *tsi, long opc)
80 {
81         mdt_batch_reconstructor reconst;
82         int rc;
83
84         ENTRY;
85
86         if (opc >= BUT_LAST_OPC)
87                 RETURN(-EOPNOTSUPP);
88
89         reconst = reconstructors[opc];
90         LASSERT(reconst != NULL);
91         rc = reconst(tsi);
92         RETURN(rc);
93 }
94
95 static int mdt_batch_getattr(struct tgt_session_info *tsi)
96 {
97         struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
98         struct req_capsule *pill = &info->mti_sub_pill;
99         int rc;
100
101         ENTRY;
102
103         rc = ldlm_handle_enqueue(info->mti_exp->exp_obd->obd_namespace,
104                                  pill, info->mti_dlm_req, &mdt_dlm_cbs);
105
106         RETURN(rc);
107 }
108
109 /* Batch UpdaTe Request with a format known in advance */
110 #define TGT_BUT_HDL(flags, opc, fn)                     \
111 [opc - BUT_FIRST_OPC] = {                               \
112         .th_name        = #opc,                         \
113         .th_fail_id     = 0,                            \
114         .th_opc         = opc,                          \
115         .th_flags       = flags,                        \
116         .th_act         = fn,                           \
117         .th_fmt         = &RQF_ ## opc,                 \
118         .th_version     = LUSTRE_MDS_VERSION,           \
119         .th_hp          = NULL,                         \
120 }
121
122 static struct tgt_handler mdt_batch_handlers[] = {
123 TGT_BUT_HDL(HAS_KEY | HAS_REPLY,        BUT_GETATTR,    mdt_batch_getattr),
124 };
125
126 static struct tgt_handler *mdt_batch_handler_find(__u32 opc)
127 {
128         struct tgt_handler *h;
129
130         h = NULL;
131         if (opc >= BUT_FIRST_OPC && opc < BUT_LAST_OPC) {
132                 h = &mdt_batch_handlers[opc - BUT_FIRST_OPC];
133                 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
134                          h->th_opc, opc);
135         } else {
136                 h = NULL; /* unsupported opc */
137         }
138         return h;
139 }
140
141 int mdt_batch(struct tgt_session_info *tsi)
142 {
143         struct mdt_thread_info *info = tsi2mdt_info(tsi);
144         struct req_capsule *pill = &info->mti_sub_pill;
145         struct ptlrpc_request *req = tgt_ses_req(tsi);
146         struct but_update_header *buh;
147         struct but_update_buffer *bub = NULL;
148         struct batch_update_reply *reply = NULL;
149         struct ptlrpc_bulk_desc *desc = NULL;
150         struct tg_reply_data *trd = NULL;
151         struct lustre_msg *repmsg = NULL;
152         bool need_reconstruct;
153         __u32 handled_update_count = 0;
154         __u32 update_buf_count;
155         __u32 packed_replen;
156         void **update_bufs;
157         int buh_size;
158         int rc;
159         int i;
160
161         ENTRY;
162
163         buh_size = req_capsule_get_size(&req->rq_pill, &RMF_BUT_HEADER,
164                                         RCL_CLIENT);
165         if (buh_size <= 0)
166                 RETURN(err_serious(-EPROTO));
167
168         buh = req_capsule_client_get(&req->rq_pill, &RMF_BUT_HEADER);
169         if (buh == NULL)
170                 RETURN(err_serious(-EPROTO));
171
172         if (buh->buh_magic != BUT_HEADER_MAGIC) {
173                 CERROR("%s: invalid update header magic %x expect %x: "
174                        "rc = %d\n", tgt_name(tsi->tsi_tgt), buh->buh_magic,
175                        BUT_HEADER_MAGIC, -EPROTO);
176                 RETURN(err_serious(-EPROTO));
177         }
178
179         update_buf_count = buh->buh_count;
180         if (update_buf_count == 0)
181                 RETURN(err_serious(-EPROTO));
182
183         OBD_ALLOC_PTR_ARRAY(update_bufs, update_buf_count);
184         if (update_bufs == NULL)
185                 RETURN(err_serious(-ENOMEM));
186
187         if (buh->buh_inline_length > 0) {
188                 update_bufs[0] = buh->buh_inline_data;
189         } else {
190                 struct but_update_buffer *tmp;
191                 int page_count = 0;
192
193                 bub = req_capsule_client_get(&req->rq_pill, &RMF_BUT_BUF);
194                 if (bub == NULL)
195                         GOTO(out, rc = err_serious(-EPROTO));
196
197                 for (i = 0; i < update_buf_count; i++)
198                         /* First *and* last might be partial pages, hence +1 */
199                         page_count += DIV_ROUND_UP(bub[i].bub_size,
200                                                    PAGE_SIZE) + 1;
201
202                 desc = ptlrpc_prep_bulk_exp(req, page_count,
203                                             PTLRPC_BULK_OPS_COUNT,
204                                             PTLRPC_BULK_GET_SINK,
205                                             MDS_BULK_PORTAL,
206                                             &ptlrpc_bulk_kiov_nopin_ops);
207                 if (desc == NULL)
208                         GOTO(out, rc = err_serious(-ENOMEM));
209
210                 tmp = bub;
211                 for (i = 0; i < update_buf_count; i++, tmp++) {
212                         if (tmp->bub_size >= OUT_MAXREQSIZE)
213                                 GOTO(out, rc = err_serious(-EPROTO));
214
215                         OBD_ALLOC_LARGE(update_bufs[i], tmp->bub_size);
216                         if (update_bufs[i] == NULL)
217                                 GOTO(out, rc = err_serious(-ENOMEM));
218
219                         desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
220                                                         tmp->bub_size);
221                 }
222
223                 req->rq_bulk_write = 1;
224                 rc = sptlrpc_svc_prep_bulk(req, desc);
225                 if (rc != 0)
226                         GOTO(out, rc = err_serious(rc));
227
228                 rc = target_bulk_io(req->rq_export, desc);
229                 if (rc < 0)
230                         GOTO(out, rc = err_serious(rc));
231         }
232
233         req_capsule_set_size(&req->rq_pill, &RMF_BUT_REPLY, RCL_SERVER,
234                              buh->buh_reply_size);
235         rc = req_capsule_server_pack(&req->rq_pill);
236         if (rc != 0) {
237                 DEBUG_REQ(D_ERROR, req, "%s: Can't pack response: rc = %d\n",
238                        tgt_name(tsi->tsi_tgt), rc);
239                 GOTO(out, rc);
240         }
241
242         /* Prepare the update reply buffer */
243         reply = req_capsule_server_get(&req->rq_pill, &RMF_BUT_REPLY);
244         if (reply == NULL)
245                 GOTO(out, rc = -EPROTO);
246
247         reply->burp_magic = BUT_REPLY_MAGIC;
248         packed_replen = sizeof(*reply);
249         info->mti_batch_env = 1;
250         info->mti_pill = pill;
251         tsi->tsi_batch_env = true;
252
253         OBD_ALLOC_PTR(trd);
254         if (trd == NULL)
255                 GOTO(out, rc = -ENOMEM);
256
257         need_reconstruct = tgt_check_resent(req, trd);
258         /* Walk through sub requests in the batch request to execute them. */
259         for (i = 0; i < update_buf_count; i++) {
260                 struct batch_update_request *bur;
261                 struct lustre_msg *reqmsg = NULL;
262                 struct tgt_handler *h;
263                 int update_count;
264                 int j;
265
266                 bur = update_bufs[i];
267                 update_count = bur->burq_count;
268                 for (j = 0; j < update_count; j++) {
269                         __u32 replen;
270
271                         reqmsg = batch_update_reqmsg_next(bur, reqmsg);
272                         repmsg = batch_update_repmsg_next(reply, repmsg);
273
274                         if (handled_update_count > buh->buh_update_count)
275                                 GOTO(out, rc = -EOVERFLOW);
276
277                         LASSERT(reqmsg != NULL && repmsg != NULL);
278                         LASSERTF(reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2,
279                                  "Invalid reqmsg magic %x expected %x\n",
280                                  reqmsg->lm_magic, LUSTRE_MSG_MAGIC_V2);
281
282                         h = mdt_batch_handler_find(reqmsg->lm_opc);
283                         if (unlikely(h == NULL)) {
284                                 CERROR("%s: unsupported opc: 0x%x\n",
285                                        tgt_name(tsi->tsi_tgt), reqmsg->lm_opc);
286                                 GOTO(out, rc = -ENOTSUPP);
287                         }
288
289                         LASSERT(h->th_fmt != NULL);
290                         req_capsule_subreq_init(pill, h->th_fmt, req,
291                                                 reqmsg, repmsg, RCL_SERVER);
292
293                         rc = mdt_batch_unpack(info, reqmsg->lm_opc);
294                         if (rc) {
295                                 CERROR("%s: Can't unpack subreq, rc = %d\n",
296                                        mdt_obd_name(info->mti_mdt), rc);
297                                 GOTO(out, rc);
298                         }
299
300                         rc = mdt_batch_pack_repmsg(info);
301                         if (rc)
302                                 GOTO(out, rc);
303
304                         /* Need to reconstruct the reply for committed sub
305                          * requests in a batched RPC.
306                          * It only calls reconstruct for modification sub
307                          * requests.
308                          * For uncommitted or read-only sub requests, the server
309                          * should re-execute them via the ->th_act() below.
310                          */
311                         if ((h->th_flags & IS_MUTABLE) && need_reconstruct &&
312                             handled_update_count <=
313                             trd->trd_reply.lrd_batch_idx) {
314                                 rc = mdt_batch_reconstruct(tsi, reqmsg->lm_opc);
315                                 if (rc)
316                                         GOTO(out, rc);
317                                 GOTO(next, rc);
318                         }
319
320                         tsi->tsi_batch_idx = handled_update_count;
321                         rc = h->th_act(tsi);
322                         if (rc)
323                                 GOTO(out, rc);
324 next:
325                         /*
326                          * As @repmsg may be changed if the reply buffer is
327                          * too small to grow, thus it needs to reload it here.
328                          */
329                         repmsg = pill->rc_repmsg;
330                         repmsg->lm_result = rc;
331                         mdt_thread_info_reset(info);
332
333                         replen = lustre_packed_msg_size(repmsg);
334                         packed_replen += replen;
335                         handled_update_count++;
336                 }
337         }
338
339         CDEBUG(D_INFO, "reply size %u packed replen %u\n",
340                buh->buh_reply_size, packed_replen);
341         if (buh->buh_reply_size > packed_replen)
342                 req_capsule_shrink(&req->rq_pill, &RMF_BUT_REPLY,
343                                    packed_replen, RCL_SERVER);
344 out:
345         if (reply != NULL)
346                 reply->burp_count = handled_update_count;
347
348         if (update_bufs != NULL) {
349                 if (bub != NULL) {
350                         for (i = 0; i < update_buf_count; i++, bub++) {
351                                 if (update_bufs[i] != NULL)
352                                         OBD_FREE_LARGE(update_bufs[i],
353                                                        bub->bub_size);
354                         }
355                 }
356
357                 OBD_FREE_PTR_ARRAY(update_bufs, update_buf_count);
358         }
359
360         if (trd)
361                 OBD_FREE_PTR(trd);
362
363         if (desc != NULL)
364                 ptlrpc_free_bulk(desc);
365
366         mdt_thread_info_fini(info);
367         tsi->tsi_reply_fail_id = OBD_FAIL_BUT_UPDATE_NET_REP;
368         RETURN(rc);
369 }
370