4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2020, DDN Storage Corporation.
26 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/mdt/mdt_batch.c
31 * Batch Metadata Updating on the server (MDT)
33 * Author: Qian Yingjin <qian@ddn.com>
36 #define DEBUG_SUBSYSTEM S_MDS
38 #include <linux/module.h>
40 #include <lustre_mds.h>
41 #include "mdt_internal.h"
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
49 static int mdt_batch_unpack(struct mdt_thread_info *info, __u32 opc)
55 info->mti_dlm_req = req_capsule_client_get(info->mti_pill,
57 if (info->mti_dlm_req == NULL)
62 CERROR("%s: Unexpected opcode %d: rc = %d\n",
63 mdt_obd_name(info->mti_mdt), opc, rc);
70 static int mdt_batch_pack_repmsg(struct mdt_thread_info *info)
75 typedef int (*mdt_batch_reconstructor)(struct tgt_session_info *tsi);
77 static mdt_batch_reconstructor reconstructors[BUT_LAST_OPC];
79 static int mdt_batch_reconstruct(struct tgt_session_info *tsi, long opc)
81 mdt_batch_reconstructor reconst;
86 if (opc >= BUT_LAST_OPC)
89 reconst = reconstructors[opc];
90 LASSERT(reconst != NULL);
95 static int mdt_batch_getattr(struct tgt_session_info *tsi)
97 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
98 struct req_capsule *pill = &info->mti_sub_pill;
103 rc = ldlm_handle_enqueue(info->mti_exp->exp_obd->obd_namespace,
104 pill, info->mti_dlm_req, &mdt_dlm_cbs);
109 /* Batch UpdaTe Request with a format known in advance */
110 #define TGT_BUT_HDL(flags, opc, fn) \
111 [opc - BUT_FIRST_OPC] = { \
117 .th_fmt = &RQF_ ## opc, \
118 .th_version = LUSTRE_MDS_VERSION, \
122 static struct tgt_handler mdt_batch_handlers[] = {
123 TGT_BUT_HDL(HAS_KEY | HAS_REPLY, BUT_GETATTR, mdt_batch_getattr),
126 static struct tgt_handler *mdt_batch_handler_find(__u32 opc)
128 struct tgt_handler *h;
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",
136 h = NULL; /* unsupported opc */
141 int mdt_batch(struct tgt_session_info *tsi)
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;
163 buh_size = req_capsule_get_size(&req->rq_pill, &RMF_BUT_HEADER,
166 RETURN(err_serious(-EPROTO));
168 buh = req_capsule_client_get(&req->rq_pill, &RMF_BUT_HEADER);
170 RETURN(err_serious(-EPROTO));
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));
179 update_buf_count = buh->buh_count;
180 if (update_buf_count == 0)
181 RETURN(err_serious(-EPROTO));
183 OBD_ALLOC_PTR_ARRAY(update_bufs, update_buf_count);
184 if (update_bufs == NULL)
185 RETURN(err_serious(-ENOMEM));
187 if (buh->buh_inline_length > 0) {
188 update_bufs[0] = buh->buh_inline_data;
190 struct but_update_buffer *tmp;
193 bub = req_capsule_client_get(&req->rq_pill, &RMF_BUT_BUF);
195 GOTO(out, rc = err_serious(-EPROTO));
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,
202 desc = ptlrpc_prep_bulk_exp(req, page_count,
203 PTLRPC_BULK_OPS_COUNT,
204 PTLRPC_BULK_GET_SINK,
206 &ptlrpc_bulk_kiov_nopin_ops);
208 GOTO(out, rc = err_serious(-ENOMEM));
211 for (i = 0; i < update_buf_count; i++, tmp++) {
212 if (tmp->bub_size >= OUT_MAXREQSIZE)
213 GOTO(out, rc = err_serious(-EPROTO));
215 OBD_ALLOC_LARGE(update_bufs[i], tmp->bub_size);
216 if (update_bufs[i] == NULL)
217 GOTO(out, rc = err_serious(-ENOMEM));
219 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
223 req->rq_bulk_write = 1;
224 rc = sptlrpc_svc_prep_bulk(req, desc);
226 GOTO(out, rc = err_serious(rc));
228 rc = target_bulk_io(req->rq_export, desc);
230 GOTO(out, rc = err_serious(rc));
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);
237 DEBUG_REQ(D_ERROR, req, "%s: Can't pack response: rc = %d\n",
238 tgt_name(tsi->tsi_tgt), rc);
242 /* Prepare the update reply buffer */
243 reply = req_capsule_server_get(&req->rq_pill, &RMF_BUT_REPLY);
245 GOTO(out, rc = -EPROTO);
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;
255 GOTO(out, rc = -ENOMEM);
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;
266 bur = update_bufs[i];
267 update_count = bur->burq_count;
268 for (j = 0; j < update_count; j++) {
271 reqmsg = batch_update_reqmsg_next(bur, reqmsg);
272 repmsg = batch_update_repmsg_next(reply, repmsg);
274 if (handled_update_count > buh->buh_update_count)
275 GOTO(out, rc = -EOVERFLOW);
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);
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);
289 LASSERT(h->th_fmt != NULL);
290 req_capsule_subreq_init(pill, h->th_fmt, req,
291 reqmsg, repmsg, RCL_SERVER);
293 rc = mdt_batch_unpack(info, reqmsg->lm_opc);
295 CERROR("%s: Can't unpack subreq, rc = %d\n",
296 mdt_obd_name(info->mti_mdt), rc);
300 rc = mdt_batch_pack_repmsg(info);
304 /* Need to reconstruct the reply for committed sub
305 * requests in a batched RPC.
306 * It only calls reconstruct for modification sub
308 * For uncommitted or read-only sub requests, the server
309 * should re-execute them via the ->th_act() below.
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);
320 tsi->tsi_batch_idx = handled_update_count;
326 * As @repmsg may be changed if the reply buffer is
327 * too small to grow, thus it needs to reload it here.
329 repmsg = pill->rc_repmsg;
330 repmsg->lm_result = rc;
331 mdt_thread_info_reset(info);
333 replen = lustre_packed_msg_size(repmsg);
334 packed_replen += replen;
335 handled_update_count++;
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);
346 reply->burp_count = handled_update_count;
348 if (update_bufs != NULL) {
350 for (i = 0; i < update_buf_count; i++, bub++) {
351 if (update_bufs[i] != NULL)
352 OBD_FREE_LARGE(update_bufs[i],
357 OBD_FREE_PTR_ARRAY(update_bufs, update_buf_count);
364 ptlrpc_free_bulk(desc);
366 mdt_thread_info_fini(info);
367 tsi->tsi_reply_fail_id = OBD_FAIL_BUT_UPDATE_NET_REP;