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) 2013, 2017, Intel Corporation.
25 * lustre/target/out_handler.c
27 * Object update handler between targets.
29 * Author: di.wang <di.wang@intel.com>
32 #define DEBUG_SUBSYSTEM S_CLASS
34 #include <llog_swab.h>
35 #include <lustre_obdo.h>
36 #include <lustre_swab.h>
37 #include <lustre_update.h>
38 #include <md_object.h>
39 #include <obd_class.h>
40 #include "tgt_internal.h"
42 static inline void orr_cpu_to_le(struct out_read_reply *orr_dst,
43 const struct out_read_reply *orr_src)
45 orr_dst->orr_size = cpu_to_le32(orr_src->orr_size);
46 orr_dst->orr_padding = cpu_to_le32(orr_src->orr_padding);
47 orr_dst->orr_offset = cpu_to_le64(orr_dst->orr_offset);
50 static void out_reconstruct(const struct lu_env *env, struct dt_device *dt,
51 struct dt_object *obj,
52 struct object_update_reply *reply,
55 CDEBUG(D_HA, "%s: fork reply reply %p index %d: rc = %d\n",
56 dt_obd_name(dt), reply, index, 0);
58 object_update_result_insert(reply, NULL, 0, index, 0);
61 typedef void (*out_reconstruct_t)(const struct lu_env *env,
63 struct dt_object *obj,
64 struct object_update_reply *reply,
67 static inline bool out_check_resent(struct ptlrpc_request *req,
68 struct tg_reply_data *trd)
70 struct lsd_reply_data *lrd;
71 bool reconstruct = false;
73 if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
76 if (req_can_reconstruct(req, trd)) {
77 lrd = &trd->trd_reply;
78 req->rq_transno = lrd->lrd_transno;
79 req->rq_status = lrd->lrd_result;
81 if (req->rq_status != 0)
83 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
84 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
86 DEBUG_REQ(D_HA, req, "reconstruct resent RPC");
89 DEBUG_REQ(D_HA, req, "no reply for RESENT req");
95 static int out_create(struct tgt_session_info *tsi)
97 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
98 struct object_update *update = tti->tti_u.update.tti_update;
99 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
100 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
101 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
102 struct lu_attr *attr = &tti->tti_attr;
103 struct lu_fid *fid = NULL;
110 wobdo = object_update_param_get(update, 0, &size);
111 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
112 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
113 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
114 RETURN(PTR_ERR(wobdo));
117 if (req_capsule_req_need_swab(tsi->tsi_pill))
118 lustre_swab_obdo(wobdo);
119 lustre_get_wire_obdo(NULL, lobdo, wobdo);
120 la_from_obdo(attr, lobdo, lobdo->o_valid);
122 dof->dof_type = dt_mode_to_dft(attr->la_mode);
123 if (update->ou_params_count > 1) {
124 fid = object_update_param_get(update, 1, &size);
125 if (IS_ERR(fid) || size != sizeof(*fid)) {
126 CERROR("%s: invalid fid: rc = %ld\n",
127 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
128 RETURN(PTR_ERR(fid));
130 if (req_capsule_req_need_swab(tsi->tsi_pill))
131 lustre_swab_lu_fid(fid);
132 if (!fid_is_sane(fid)) {
133 CERROR("%s: invalid fid "DFID": rc = %d\n",
134 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
139 if (lu_object_exists(&obj->do_lu))
142 rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
143 &tti->tti_tea, tti->tti_tea.ta_handle,
144 tti->tti_u.update.tti_update_reply,
145 tti->tti_u.update.tti_update_reply_index);
150 static int out_attr_set(struct tgt_session_info *tsi)
152 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
153 struct object_update *update = tti->tti_u.update.tti_update;
154 struct lu_attr *attr = &tti->tti_attr;
155 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
156 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
163 wobdo = object_update_param_get(update, 0, &size);
164 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
165 CERROR("%s: empty obdo in the update: rc = %ld\n",
166 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
167 RETURN(PTR_ERR(wobdo));
173 if (req_capsule_req_need_swab(tsi->tsi_pill))
174 lustre_swab_obdo(wobdo);
175 lustre_get_wire_obdo(NULL, lobdo, wobdo);
176 la_from_obdo(attr, lobdo, lobdo->o_valid);
178 rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
179 tti->tti_tea.ta_handle,
180 tti->tti_u.update.tti_update_reply,
181 tti->tti_u.update.tti_update_reply_index);
186 static int out_attr_get(struct tgt_session_info *tsi)
188 const struct lu_env *env = tsi->tsi_env;
189 struct tgt_thread_info *tti = tgt_th_info(env);
190 struct object_update *update = tti->tti_u.update.tti_update;
191 struct obdo *obdo = &tti->tti_u.update.tti_obdo;
192 struct lu_attr *la = &tti->tti_attr;
193 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
194 int idx = tti->tti_u.update.tti_update_reply_index;
199 if (unlikely(update->ou_result_size < sizeof(*obdo)))
202 if (!lu_object_exists(&obj->do_lu)) {
203 /* Usually, this will be called when the master MDT try
204 * to init a remote object(see osp_object_init), so if
205 * the object does not exist on slave, we need set BANSHEE flag,
206 * so the object can be removed from the cache immediately */
207 set_bit(LU_OBJECT_HEARD_BANSHEE,
208 &obj->do_lu.lo_header->loh_flags);
212 dt_read_lock(env, obj, DT_TGT_CHILD);
213 rc = dt_attr_get(env, obj, la);
215 GOTO(out_unlock, rc);
218 obdo_from_la(obdo, la, la->la_valid);
221 dt_read_unlock(env, obj);
223 CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
224 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
227 object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
228 sizeof(*obdo), idx, rc);
233 static int out_xattr_get(struct tgt_session_info *tsi)
235 const struct lu_env *env = tsi->tsi_env;
236 struct tgt_thread_info *tti = tgt_th_info(env);
237 struct object_update *update = tti->tti_u.update.tti_update;
238 struct lu_buf *lbuf = &tti->tti_buf;
239 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
240 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
242 struct object_update_result *update_result;
243 int idx = tti->tti_u.update.tti_update_reply_index;
248 if (!lu_object_exists(&obj->do_lu)) {
249 set_bit(LU_OBJECT_HEARD_BANSHEE,
250 &obj->do_lu.lo_header->loh_flags);
254 name = object_update_param_get(update, 0, NULL);
256 CERROR("%s: empty name for xattr get: rc = %ld\n",
257 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
258 RETURN(PTR_ERR(name));
261 update_result = object_update_result_get(reply, idx, NULL);
262 if (update_result == NULL) {
263 CERROR("%s: empty name for xattr get: rc = %d\n",
264 tgt_name(tsi->tsi_tgt), -EPROTO);
268 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
269 if (lbuf->lb_len == 0)
272 lbuf->lb_buf = update_result->our_data;
274 dt_read_lock(env, obj, DT_TGT_CHILD);
275 rc = dt_xattr_get(env, obj, lbuf, name);
276 dt_read_unlock(env, obj);
281 } else if (lbuf->lb_buf) {
284 CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
285 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
291 object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
295 static int out_xattr_list(struct tgt_session_info *tsi)
297 const struct lu_env *env = tsi->tsi_env;
298 struct tgt_thread_info *tti = tgt_th_info(env);
299 struct lu_buf *lbuf = &tti->tti_buf;
300 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
301 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
302 struct object_update_result *update_result;
303 int idx = tti->tti_u.update.tti_update_reply_index;
308 if (!lu_object_exists(&obj->do_lu)) {
309 set_bit(LU_OBJECT_HEARD_BANSHEE,
310 &obj->do_lu.lo_header->loh_flags);
314 update_result = object_update_result_get(reply, 0, NULL);
315 if (!update_result) {
317 CERROR("%s: empty buf for xattr list: rc = %d\n",
318 tgt_name(tsi->tsi_tgt), rc);
322 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
323 lbuf->lb_buf = update_result->our_data;
324 if (lbuf->lb_len == 0)
327 dt_read_lock(env, obj, DT_TGT_CHILD);
328 rc = dt_xattr_list(env, obj, lbuf);
329 dt_read_unlock(env, obj);
334 } else if (lbuf->lb_buf) {
338 CDEBUG(D_INFO, "%s: "DFID" list xattr len %d\n",
339 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)), rc);
341 /* Since we directly use update_result->our_data as the lbuf->lb_buf,
342 * then use NULL for result_insert to avoid unnecessary memory copy. */
343 object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
348 static int out_index_lookup(struct tgt_session_info *tsi)
350 const struct lu_env *env = tsi->tsi_env;
351 struct tgt_thread_info *tti = tgt_th_info(env);
352 struct object_update *update = tti->tti_u.update.tti_update;
353 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
359 if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
362 if (!lu_object_exists(&obj->do_lu))
365 name = object_update_param_get(update, 0, NULL);
367 CERROR("%s: empty name for lookup: rc = %ld\n",
368 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
369 RETURN(PTR_ERR(name));
372 dt_read_lock(env, obj, DT_TGT_CHILD);
373 if (!dt_try_as_dir(env, obj, true))
374 GOTO(out_unlock, rc = -ENOTDIR);
376 rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
377 (struct dt_key *)name);
380 GOTO(out_unlock, rc);
386 dt_read_unlock(env, obj);
388 CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
389 PFID(lu_object_fid(&obj->do_lu)), name,
390 PFID(&tti->tti_fid1), rc);
392 CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
393 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
396 object_update_result_insert(tti->tti_u.update.tti_update_reply,
397 &tti->tti_fid1, sizeof(tti->tti_fid1),
398 tti->tti_u.update.tti_update_reply_index, rc);
402 static int out_xattr_set(struct tgt_session_info *tsi)
404 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
405 struct object_update *update = tti->tti_u.update.tti_update;
406 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
407 struct lu_buf *lbuf = &tti->tti_buf;
417 name = object_update_param_get(update, 0, NULL);
419 CERROR("%s: empty name for xattr set: rc = %ld\n",
420 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
421 RETURN(PTR_ERR(name));
424 /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
425 buf = object_update_param_get(update, 1, &buf_len);
426 if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
427 RETURN(PTR_ERR(buf));
430 lbuf->lb_len = buf_len;
432 tmp = object_update_param_get(update, 2, &size);
433 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
434 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
435 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
436 RETURN(PTR_ERR(tmp));
439 if (req_capsule_req_need_swab(tsi->tsi_pill))
443 rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
444 &tti->tti_tea, tti->tti_tea.ta_handle,
445 tti->tti_u.update.tti_update_reply,
446 tti->tti_u.update.tti_update_reply_index);
450 static int out_xattr_del(struct tgt_session_info *tsi)
452 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
453 struct object_update *update = tti->tti_u.update.tti_update;
454 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
459 name = object_update_param_get(update, 0, NULL);
461 CERROR("%s: empty name for xattr set: rc = %ld\n",
462 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
463 RETURN(PTR_ERR(name));
466 rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
467 tti->tti_tea.ta_handle,
468 tti->tti_u.update.tti_update_reply,
469 tti->tti_u.update.tti_update_reply_index);
474 * increase ref of the object
476 static int out_ref_add(struct tgt_session_info *tsi)
478 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
479 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
484 rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
485 tti->tti_tea.ta_handle,
486 tti->tti_u.update.tti_update_reply,
487 tti->tti_u.update.tti_update_reply_index);
491 static int out_ref_del(struct tgt_session_info *tsi)
493 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
494 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
499 if (!lu_object_exists(&obj->do_lu))
502 rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
503 tti->tti_tea.ta_handle,
504 tti->tti_u.update.tti_update_reply,
505 tti->tti_u.update.tti_update_reply_index);
509 static int out_index_insert(struct tgt_session_info *tsi)
511 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
512 struct object_update *update = tti->tti_u.update.tti_update;
513 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
514 struct dt_insert_rec *rec = &tti->tti_rec;
522 name = object_update_param_get(update, 0, NULL);
524 CERROR("%s: empty name for index insert: rc = %ld\n",
525 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
526 RETURN(PTR_ERR(name));
529 fid = object_update_param_get(update, 1, &size);
530 if (IS_ERR(fid) || size != sizeof(*fid)) {
531 CERROR("%s: invalid fid: rc = %ld\n",
532 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
533 RETURN(PTR_ERR(fid));
536 if (req_capsule_req_need_swab(tsi->tsi_pill))
537 lustre_swab_lu_fid(fid);
539 if (!fid_is_sane(fid)) {
540 CERROR("%s: invalid FID "DFID": rc = %d\n",
541 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
545 ptype = object_update_param_get(update, 2, &size);
546 if (IS_ERR(ptype) || size != sizeof(*ptype)) {
547 CERROR("%s: invalid type for index insert: rc = %ld\n",
548 tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
549 RETURN(PTR_ERR(ptype));
552 if (req_capsule_req_need_swab(tsi->tsi_pill))
556 rec->rec_type = *ptype;
558 rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
559 (const struct dt_key *)name, &tti->tti_tea,
560 tti->tti_tea.ta_handle,
561 tti->tti_u.update.tti_update_reply,
562 tti->tti_u.update.tti_update_reply_index);
564 CDEBUG(D_INFO, "%s: "DFID" index insert %s: rc = %d\n",
565 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
571 static int out_index_delete(struct tgt_session_info *tsi)
573 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
574 struct object_update *update = tti->tti_u.update.tti_update;
575 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
579 if (!lu_object_exists(&obj->do_lu))
582 name = object_update_param_get(update, 0, NULL);
584 CERROR("%s: empty name for index delete: rc = %ld\n",
585 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
586 RETURN(PTR_ERR(name));
589 rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
590 &tti->tti_tea, tti->tti_tea.ta_handle,
591 tti->tti_u.update.tti_update_reply,
592 tti->tti_u.update.tti_update_reply_index);
596 static int out_destroy(struct tgt_session_info *tsi)
598 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
599 struct object_update *update = tti->tti_u.update.tti_update;
600 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
605 fid = &update->ou_fid;
606 if (!fid_is_sane(fid)) {
607 CERROR("%s: invalid FID "DFID": rc = %d\n",
608 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
612 if (!lu_object_exists(&obj->do_lu))
615 rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
616 tti->tti_tea.ta_handle,
617 tti->tti_u.update.tti_update_reply,
618 tti->tti_u.update.tti_update_reply_index);
623 static int out_write(struct tgt_session_info *tsi)
625 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
626 struct object_update *update = tti->tti_u.update.tti_update;
627 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
628 struct lu_buf *lbuf = &tti->tti_buf;
637 buf = object_update_param_get(update, 0, &buf_len);
638 if (IS_ERR(buf) || buf_len == 0) {
639 CERROR("%s: empty buf for xattr set: rc = %ld\n",
640 tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
641 RETURN(PTR_ERR(buf));
644 lbuf->lb_len = buf_len;
646 tmp = object_update_param_get(update, 1, &size);
647 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
648 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
649 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
650 RETURN(PTR_ERR(tmp));
653 if (req_capsule_req_need_swab(tsi->tsi_pill))
657 rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
658 &tti->tti_tea, tti->tti_tea.ta_handle,
659 tti->tti_u.update.tti_update_reply,
660 tti->tti_u.update.tti_update_reply_index);
664 static int out_read(struct tgt_session_info *tsi)
666 const struct lu_env *env = tsi->tsi_env;
667 struct tgt_thread_info *tti = tgt_th_info(env);
668 struct object_update *update = tti->tti_u.update.tti_update;
669 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
670 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
671 int index = tti->tti_u.update.tti_update_reply_index;
672 struct lu_rdbuf *rdbuf;
673 struct object_update_result *update_result;
674 struct out_read_reply *orr;
677 size_t total_size = 0;
684 update_result = object_update_result_get(reply, index, NULL);
685 LASSERT(update_result != NULL);
686 update_result->our_datalen = sizeof(*orr);
688 if (!lu_object_exists(&obj->do_lu))
689 GOTO(out, rc = -ENOENT);
691 tmp = object_update_param_get(update, 0, NULL);
693 CERROR("%s: empty size for read: rc = %ld\n",
694 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
695 GOTO(out, rc = PTR_ERR(tmp));
697 size = le64_to_cpu(*(size_t *)(tmp));
699 tmp = object_update_param_get(update, 1, NULL);
701 CERROR("%s: empty pos for read: rc = %ld\n",
702 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
703 GOTO(out, rc = PTR_ERR(tmp));
705 pos = le64_to_cpu(*(__u64 *)(tmp));
707 /* Put the offset into the begining of the buffer in reply */
708 orr = (struct out_read_reply *)update_result->our_data;
710 nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
711 OBD_ALLOC(rdbuf, sizeof(*rdbuf) + nbufs * sizeof(rdbuf->rb_bufs[0]));
713 GOTO(out, rc = -ENOMEM);
717 for (i = 0; i < nbufs; i++) {
720 read_size = size > OUT_BULK_BUFFER_SIZE ?
721 OUT_BULK_BUFFER_SIZE : size;
722 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
723 if (rdbuf->rb_bufs[i].lb_buf == NULL)
724 GOTO(out_free, rc = -ENOMEM);
726 rdbuf->rb_bufs[i].lb_len = read_size;
727 dt_read_lock(env, obj, DT_TGT_CHILD);
728 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
729 dt_read_unlock(env, obj);
731 total_size += rc < 0 ? 0 : rc;
739 /* send pages to client */
740 rc = tgt_send_buffer(tsi, rdbuf);
744 orr->orr_size = total_size;
745 orr->orr_offset = pos;
747 orr_cpu_to_le(orr, orr);
748 update_result->our_datalen += orr->orr_size;
750 for (i = 0; i < nbufs; i++) {
751 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
752 OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
753 rdbuf->rb_bufs[i].lb_len);
756 OBD_FREE(rdbuf, sizeof(*rdbuf) +
757 nbufs * sizeof(rdbuf->rb_bufs[0]));
759 /* Insert read buffer */
760 update_result->our_rc = ptlrpc_status_hton(rc);
761 reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
762 sizeof(*update_result));
766 static int out_noop(struct tgt_session_info *tsi)
771 #define DEF_OUT_HNDL(opc, name, flags, fn) \
772 [opc - OUT_CREATE] = { \
782 static struct tgt_handler out_update_ops[] = {
783 DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
785 DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
787 DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
789 DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
791 DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set", IS_MUTABLE | HAS_REPLY,
793 DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get", HAS_REPLY,
795 DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
797 DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
799 DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
801 DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
803 DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
804 IS_MUTABLE | HAS_REPLY, out_index_insert),
805 DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
806 IS_MUTABLE | HAS_REPLY, out_index_delete),
807 DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
808 DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
809 DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
810 DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
814 static struct tgt_handler *out_handler_find(__u32 opc)
816 struct tgt_handler *h;
819 if (OUT_CREATE <= opc && opc < OUT_LAST) {
820 h = &out_update_ops[opc - OUT_CREATE];
821 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
824 h = NULL; /* unsupported opc */
829 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
830 struct thandle_exec_args *ta, struct obd_export *exp)
833 ta->ta_handle = dt_trans_create(env, dt);
834 if (IS_ERR(ta->ta_handle)) {
837 rc = PTR_ERR(ta->ta_handle);
838 ta->ta_handle = NULL;
839 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
843 if (exp->exp_need_sync)
844 ta->ta_handle->th_sync = 1;
849 static int out_trans_start(const struct lu_env *env,
850 struct thandle_exec_args *ta)
852 return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
855 static int out_trans_stop(const struct lu_env *env,
856 struct thandle_exec_args *ta, int err)
861 ta->ta_handle->th_result = err;
862 rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
863 for (i = 0; i < ta->ta_argno; i++) {
864 if (ta->ta_args[i]->object != NULL) {
865 dt_object_put(env, ta->ta_args[i]->object);
866 ta->ta_args[i]->object = NULL;
869 ta->ta_handle = NULL;
875 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
878 struct tgt_session_info *tsi = tgt_ses_info(env);
884 if (ta->ta_handle == NULL)
887 if (declare_ret != 0 || ta->ta_argno == 0)
888 GOTO(stop, rc = declare_ret);
890 LASSERT(ta->ta_handle->th_dev != NULL);
891 rc = out_trans_start(env, ta);
892 if (unlikely(rc != 0))
895 for (i = 0; i < ta->ta_argno; i++) {
896 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
898 if (unlikely(rc != 0)) {
899 CDEBUG(D_INFO, "error during execution of #%u from"
900 " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
901 ta->ta_args[i]->line, rc);
903 if (ta->ta_args[i]->undo_fn != NULL)
904 ta->ta_args[i]->undo_fn(env,
908 CERROR("%s: undo for %s:%d: rc = %d\n",
909 dt_obd_name(ta->ta_handle->th_dev),
910 ta->ta_args[i]->file,
911 ta->ta_args[i]->line, -ENOTSUPP);
915 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
916 dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
919 /* Only fail for real updates, XXX right now llog updates will be
920 * ignore, whose updates count is usually 1, so failover test
921 * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
922 * be removed after async update patch is landed. */
923 if (ta->ta_argno > 1)
924 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
927 rc1 = out_trans_stop(env, ta, rc);
931 ta->ta_handle = NULL;
938 * Object updates between Targets. Because all the updates has been
939 * dis-assemblied into object updates at sender side, so OUT will
940 * call OSD API directly to execute these updates.
942 * In DNE phase I all of the updates in the request need to be executed
943 * in one transaction, and the transaction has to be synchronously.
945 * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
948 int out_handle(struct tgt_session_info *tsi)
950 const struct lu_env *env = tsi->tsi_env;
951 struct tgt_thread_info *tti = tgt_th_info(env);
952 struct thandle_exec_args *ta = &tti->tti_tea;
953 struct req_capsule *pill = tsi->tsi_pill;
954 struct dt_device *dt = tsi->tsi_tgt->lut_bottom;
955 struct out_update_header *ouh;
956 struct out_update_buffer *oub = NULL;
957 struct object_update *update;
958 struct object_update_reply *reply;
959 struct ptlrpc_bulk_desc *desc = NULL;
960 struct tg_reply_data *trd = NULL;
962 int current_batchid = -1;
963 __u32 update_buf_count;
965 unsigned int reply_index = 0;
968 int ouh_size, reply_size;
970 bool need_reconstruct;
974 req_capsule_set(pill, &RQF_OUT_UPDATE);
975 ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
978 RETURN(err_serious(-EPROTO));
980 ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
982 RETURN(err_serious(-EPROTO));
984 if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
985 CERROR("%s: invalid update buffer magic %x expect %x: "
986 "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
987 UPDATE_REQUEST_MAGIC, -EPROTO);
988 RETURN(err_serious(-EPROTO));
991 update_buf_count = ouh->ouh_count;
992 if (update_buf_count == 0)
993 RETURN(err_serious(-EPROTO));
995 OBD_ALLOC_PTR_ARRAY(update_bufs, update_buf_count);
996 if (update_bufs == NULL)
997 RETURN(err_serious(-ENOMEM));
999 if (ouh->ouh_inline_length > 0) {
1000 update_bufs[0] = ouh->ouh_inline_data;
1002 struct out_update_buffer *tmp;
1005 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
1007 GOTO(out_free, rc = err_serious(-EPROTO));
1009 for (i = 0; i < update_buf_count; i++)
1010 /* First *and* last might be partial pages, hence +1 */
1011 page_count += DIV_ROUND_UP(oub[i].oub_size,
1014 desc = ptlrpc_prep_bulk_exp(pill->rc_req, page_count,
1015 PTLRPC_BULK_OPS_COUNT,
1016 PTLRPC_BULK_GET_SINK,
1018 &ptlrpc_bulk_kiov_nopin_ops);
1020 GOTO(out_free, rc = err_serious(-ENOMEM));
1023 for (i = 0; i < update_buf_count; i++, tmp++) {
1024 if (tmp->oub_size >= OUT_MAXREQSIZE)
1025 GOTO(out_free, rc = err_serious(-EPROTO));
1027 OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1028 if (update_bufs[i] == NULL)
1029 GOTO(out_free, rc = err_serious(-ENOMEM));
1031 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1035 pill->rc_req->rq_bulk_write = 1;
1036 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1038 GOTO(out_free, rc = err_serious(rc));
1040 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1042 GOTO(out_free, rc = err_serious(rc));
1044 /* validate the request and calculate the total update count and
1045 * set it to reply */
1048 for (i = 0; i < update_buf_count; i++) {
1049 struct object_update_request *our;
1052 our = update_bufs[i];
1053 if (req_capsule_req_need_swab(pill))
1054 lustre_swab_object_update_request(our, 0);
1056 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1057 CERROR("%s: invalid update buffer magic %x"
1058 " expect %x: rc = %d\n",
1059 tgt_name(tsi->tsi_tgt), our->ourq_magic,
1060 UPDATE_REQUEST_MAGIC, -EPROTO);
1061 GOTO(out_free, rc = err_serious(-EPROTO));
1063 updates += our->ourq_count;
1065 /* need to calculate reply size */
1066 for (j = 0; j < our->ourq_count; j++) {
1067 update = object_update_request_get(our, j, NULL);
1069 GOTO(out, rc = err_serious(-EPROTO));
1070 if (req_capsule_req_need_swab(pill))
1071 lustre_swab_object_update(update);
1073 if (!fid_is_sane(&update->ou_fid)) {
1074 CERROR("%s: invalid FID "DFID": rc = %d\n",
1075 tgt_name(tsi->tsi_tgt),
1076 PFID(&update->ou_fid), -EPROTO);
1077 GOTO(out, rc = err_serious(-EPROTO));
1080 /* XXX: what ou_result_size can be considered safe? */
1082 reply_size += sizeof(reply->ourp_lens[0]);
1083 reply_size += sizeof(struct object_update_result);
1084 reply_size += update->ou_result_size;
1087 reply_size += sizeof(*reply);
1089 if (unlikely(reply_size > ouh->ouh_reply_size)) {
1090 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1091 tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1092 updates, reply_size);
1093 GOTO(out_free, rc = err_serious(-EPROTO));
1096 req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1097 ouh->ouh_reply_size);
1098 rc = req_capsule_server_pack(pill);
1100 CERROR("%s: Can't pack response: rc = %d\n",
1101 tgt_name(tsi->tsi_tgt), rc);
1102 GOTO(out_free, rc = err_serious(-EPROTO));
1105 /* Prepare the update reply buffer */
1106 reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1108 GOTO(out_free, rc = -EPROTO);
1109 reply->ourp_magic = UPDATE_REPLY_MAGIC;
1110 reply->ourp_count = updates;
1111 tti->tti_u.update.tti_update_reply = reply;
1112 tsi->tsi_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1116 GOTO(out_free, rc = -ENOMEM);
1118 need_reconstruct = out_check_resent(pill->rc_req, trd);
1120 /* Walk through updates in the request to execute them */
1121 for (i = 0; i < update_buf_count; i++) {
1122 struct tgt_handler *h;
1123 struct dt_object *dt_obj;
1125 struct object_update_request *our;
1128 our = update_bufs[i];
1129 update_count = our->ourq_count;
1130 for (j = 0; j < update_count; j++) {
1131 struct lu_object_conf conf;
1133 update = object_update_request_get(our, j, NULL);
1134 if (update->ou_type == OUT_CREATE)
1135 conf.loc_flags = LOC_F_NEW;
1139 dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1140 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1142 GOTO(out, rc = PTR_ERR(dt_obj));
1144 if (dt->dd_record_fid_accessed) {
1145 struct lfsck_req_local *lrl = &tti->tti_lrl;
1148 lu_object_fid(&dt_obj->do_lu),
1151 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1154 tti->tti_u.update.tti_dt_object = dt_obj;
1155 tti->tti_u.update.tti_update = update;
1156 tti->tti_u.update.tti_update_reply_index = reply_index;
1158 h = out_handler_find(update->ou_type);
1159 if (unlikely(h == NULL)) {
1160 CERROR("%s: unsupported opc: 0x%x\n",
1161 tgt_name(tsi->tsi_tgt), update->ou_type);
1162 GOTO(next, rc = -ENOTSUPP);
1165 /* Check resend case only for modifying RPC */
1166 if (h->th_flags & IS_MUTABLE) {
1167 /* sanity check for last XID changing */
1168 if (unlikely(!need_reconstruct &&
1169 req_xid_is_last(pill->rc_req))) {
1170 DEBUG_REQ(D_ERROR, pill->rc_req,
1171 "unexpected last XID change");
1172 GOTO(next, rc = -EINVAL);
1175 if (need_reconstruct) {
1176 out_reconstruct(env, dt, dt_obj, reply,
1182 GOTO(next, rc = -EROFS);
1185 /* start transaction for modification RPC only */
1186 if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1187 current_batchid = update->ou_batchid;
1189 if (reply_index == 0)
1190 CFS_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
1192 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1196 if (update->ou_flags & UPDATE_FL_SYNC)
1197 ta->ta_handle->th_sync = 1;
1200 /* Stop the current update transaction, if the update
1201 * has different batchid, or read-only update */
1202 if (((current_batchid != update->ou_batchid) ||
1203 !(h->th_flags & IS_MUTABLE)) &&
1204 ta->ta_handle != NULL) {
1205 rc = out_tx_end(env, ta, rc);
1206 current_batchid = -1;
1210 /* start a new transaction if needed */
1211 if (h->th_flags & IS_MUTABLE) {
1212 rc = out_tx_start(env, dt, ta,
1216 if (update->ou_flags & UPDATE_FL_SYNC)
1217 ta->ta_handle->th_sync = 1;
1218 current_batchid = update->ou_batchid;
1222 if (OBD_FAIL_CHECK(OBD_FAIL_OUT_EIO))
1225 rc = h->th_act(tsi);
1228 dt_object_put(env, dt_obj);
1234 if (current_batchid != -1) {
1235 rc1 = out_tx_end(env, ta, rc);
1241 if (update_bufs != NULL) {
1243 for (i = 0; i < update_buf_count; i++, oub++) {
1244 if (update_bufs[i] != NULL)
1245 OBD_FREE_LARGE(update_bufs[i],
1250 OBD_FREE_PTR_ARRAY(update_bufs, update_buf_count);
1257 ptlrpc_free_bulk(desc);
1262 struct tgt_handler tgt_out_handlers[] = {
1263 TGT_UPDATE_HDL(IS_MUTABLE, OUT_UPDATE, out_handle),
1265 EXPORT_SYMBOL(tgt_out_handlers);