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_INFO, "%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);
62 typedef void (*out_reconstruct_t)(const struct lu_env *env,
64 struct dt_object *obj,
65 struct object_update_reply *reply,
68 static inline int out_check_resent(const struct lu_env *env,
70 struct dt_object *obj,
71 struct ptlrpc_request *req,
72 out_reconstruct_t reconstruct,
73 struct object_update_reply *reply,
76 if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
79 if (req_xid_is_last(req)) {
80 struct lsd_client_data *lcd;
82 /* XXX this does not support mulitple transactions yet, i.e.
83 * only 1 update RPC each time betwee MDTs */
84 lcd = req->rq_export->exp_target_data.ted_lcd;
86 req->rq_transno = lcd->lcd_last_transno;
87 req->rq_status = lcd->lcd_last_result;
88 if (req->rq_status != 0)
90 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
91 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
93 DEBUG_REQ(D_RPCTRACE, req, "restoring resent RPC");
95 reconstruct(env, dt, obj, reply, index);
98 DEBUG_REQ(D_HA, req, "no reply for RESENT req (have %lld)",
99 req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
103 static int out_create(struct tgt_session_info *tsi)
105 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
106 struct object_update *update = tti->tti_u.update.tti_update;
107 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
108 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
109 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
110 struct lu_attr *attr = &tti->tti_attr;
111 struct lu_fid *fid = NULL;
118 wobdo = object_update_param_get(update, 0, &size);
119 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
120 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
121 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
122 RETURN(PTR_ERR(wobdo));
125 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
126 lustre_swab_obdo(wobdo);
127 lustre_get_wire_obdo(NULL, lobdo, wobdo);
128 la_from_obdo(attr, lobdo, lobdo->o_valid);
130 dof->dof_type = dt_mode_to_dft(attr->la_mode);
131 if (update->ou_params_count > 1) {
132 fid = object_update_param_get(update, 1, &size);
133 if (IS_ERR(fid) || size != sizeof(*fid)) {
134 CERROR("%s: invalid fid: rc = %ld\n",
135 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
136 RETURN(PTR_ERR(fid));
138 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
139 lustre_swab_lu_fid(fid);
140 if (!fid_is_sane(fid)) {
141 CERROR("%s: invalid fid "DFID": rc = %d\n",
142 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
147 if (lu_object_exists(&obj->do_lu))
150 rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
151 &tti->tti_tea, tti->tti_tea.ta_handle,
152 tti->tti_u.update.tti_update_reply,
153 tti->tti_u.update.tti_update_reply_index);
158 static int out_attr_set(struct tgt_session_info *tsi)
160 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
161 struct object_update *update = tti->tti_u.update.tti_update;
162 struct lu_attr *attr = &tti->tti_attr;
163 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
164 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
171 wobdo = object_update_param_get(update, 0, &size);
172 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
173 CERROR("%s: empty obdo in the update: rc = %ld\n",
174 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
175 RETURN(PTR_ERR(wobdo));
181 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
182 lustre_swab_obdo(wobdo);
183 lustre_get_wire_obdo(NULL, lobdo, wobdo);
184 la_from_obdo(attr, lobdo, lobdo->o_valid);
186 rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
187 tti->tti_tea.ta_handle,
188 tti->tti_u.update.tti_update_reply,
189 tti->tti_u.update.tti_update_reply_index);
194 static int out_attr_get(struct tgt_session_info *tsi)
196 const struct lu_env *env = tsi->tsi_env;
197 struct tgt_thread_info *tti = tgt_th_info(env);
198 struct object_update *update = tti->tti_u.update.tti_update;
199 struct obdo *obdo = &tti->tti_u.update.tti_obdo;
200 struct lu_attr *la = &tti->tti_attr;
201 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
202 int idx = tti->tti_u.update.tti_update_reply_index;
207 if (unlikely(update->ou_result_size < sizeof(*obdo)))
210 if (!lu_object_exists(&obj->do_lu)) {
211 /* Usually, this will be called when the master MDT try
212 * to init a remote object(see osp_object_init), so if
213 * the object does not exist on slave, we need set BANSHEE flag,
214 * so the object can be removed from the cache immediately */
215 set_bit(LU_OBJECT_HEARD_BANSHEE,
216 &obj->do_lu.lo_header->loh_flags);
220 dt_read_lock(env, obj, MOR_TGT_CHILD);
221 rc = dt_attr_get(env, obj, la);
223 GOTO(out_unlock, rc);
226 obdo_from_la(obdo, la, la->la_valid);
229 dt_read_unlock(env, obj);
231 CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
232 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
235 object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
236 sizeof(*obdo), idx, rc);
241 static int out_xattr_get(struct tgt_session_info *tsi)
243 const struct lu_env *env = tsi->tsi_env;
244 struct tgt_thread_info *tti = tgt_th_info(env);
245 struct object_update *update = tti->tti_u.update.tti_update;
246 struct lu_buf *lbuf = &tti->tti_buf;
247 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
248 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
250 struct object_update_result *update_result;
251 int idx = tti->tti_u.update.tti_update_reply_index;
256 if (!lu_object_exists(&obj->do_lu)) {
257 set_bit(LU_OBJECT_HEARD_BANSHEE,
258 &obj->do_lu.lo_header->loh_flags);
262 name = object_update_param_get(update, 0, NULL);
264 CERROR("%s: empty name for xattr get: rc = %ld\n",
265 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
266 RETURN(PTR_ERR(name));
269 update_result = object_update_result_get(reply, idx, NULL);
270 if (update_result == NULL) {
271 CERROR("%s: empty name for xattr get: rc = %d\n",
272 tgt_name(tsi->tsi_tgt), -EPROTO);
276 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
277 if (lbuf->lb_len == 0)
280 lbuf->lb_buf = update_result->our_data;
282 dt_read_lock(env, obj, MOR_TGT_CHILD);
283 rc = dt_xattr_get(env, obj, lbuf, name);
284 dt_read_unlock(env, obj);
289 } else if (lbuf->lb_buf) {
293 CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d: rc = %d\n",
294 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
295 name, (int)lbuf->lb_len, rc);
297 /* Since we directly use update_result->our_data as the lbuf->lb_buf,
298 * then use NULL for result_insert to avoid unnecessary memory copy. */
299 object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
304 static int out_index_lookup(struct tgt_session_info *tsi)
306 const struct lu_env *env = tsi->tsi_env;
307 struct tgt_thread_info *tti = tgt_th_info(env);
308 struct object_update *update = tti->tti_u.update.tti_update;
309 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
315 if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
318 if (!lu_object_exists(&obj->do_lu))
321 name = object_update_param_get(update, 0, NULL);
323 CERROR("%s: empty name for lookup: rc = %ld\n",
324 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
325 RETURN(PTR_ERR(name));
328 dt_read_lock(env, obj, MOR_TGT_CHILD);
329 if (!dt_try_as_dir(env, obj))
330 GOTO(out_unlock, rc = -ENOTDIR);
332 rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
333 (struct dt_key *)name);
336 GOTO(out_unlock, rc);
342 dt_read_unlock(env, obj);
344 CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
345 PFID(lu_object_fid(&obj->do_lu)), name,
346 PFID(&tti->tti_fid1), rc);
348 CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
349 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
352 object_update_result_insert(tti->tti_u.update.tti_update_reply,
353 &tti->tti_fid1, sizeof(tti->tti_fid1),
354 tti->tti_u.update.tti_update_reply_index, rc);
358 static int out_xattr_set(struct tgt_session_info *tsi)
360 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
361 struct object_update *update = tti->tti_u.update.tti_update;
362 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
363 struct lu_buf *lbuf = &tti->tti_buf;
373 name = object_update_param_get(update, 0, NULL);
375 CERROR("%s: empty name for xattr set: rc = %ld\n",
376 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
377 RETURN(PTR_ERR(name));
380 /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
381 buf = object_update_param_get(update, 1, &buf_len);
382 if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
383 RETURN(PTR_ERR(buf));
386 lbuf->lb_len = buf_len;
388 tmp = object_update_param_get(update, 2, &size);
389 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
390 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
391 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
392 RETURN(PTR_ERR(tmp));
395 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
399 rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
400 &tti->tti_tea, tti->tti_tea.ta_handle,
401 tti->tti_u.update.tti_update_reply,
402 tti->tti_u.update.tti_update_reply_index);
406 static int out_xattr_del(struct tgt_session_info *tsi)
408 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
409 struct object_update *update = tti->tti_u.update.tti_update;
410 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
415 name = object_update_param_get(update, 0, NULL);
417 CERROR("%s: empty name for xattr set: rc = %ld\n",
418 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
419 RETURN(PTR_ERR(name));
422 rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
423 tti->tti_tea.ta_handle,
424 tti->tti_u.update.tti_update_reply,
425 tti->tti_u.update.tti_update_reply_index);
430 * increase ref of the object
432 static int out_ref_add(struct tgt_session_info *tsi)
434 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
435 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
440 rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
441 tti->tti_tea.ta_handle,
442 tti->tti_u.update.tti_update_reply,
443 tti->tti_u.update.tti_update_reply_index);
447 static int out_ref_del(struct tgt_session_info *tsi)
449 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
450 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
455 if (!lu_object_exists(&obj->do_lu))
458 rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
459 tti->tti_tea.ta_handle,
460 tti->tti_u.update.tti_update_reply,
461 tti->tti_u.update.tti_update_reply_index);
465 static int out_index_insert(struct tgt_session_info *tsi)
467 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
468 struct object_update *update = tti->tti_u.update.tti_update;
469 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
470 struct dt_insert_rec *rec = &tti->tti_rec;
478 name = object_update_param_get(update, 0, NULL);
480 CERROR("%s: empty name for index insert: rc = %ld\n",
481 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
482 RETURN(PTR_ERR(name));
485 fid = object_update_param_get(update, 1, &size);
486 if (IS_ERR(fid) || size != sizeof(*fid)) {
487 CERROR("%s: invalid fid: rc = %ld\n",
488 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
489 RETURN(PTR_ERR(fid));
492 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
493 lustre_swab_lu_fid(fid);
495 if (!fid_is_sane(fid)) {
496 CERROR("%s: invalid FID "DFID": rc = %d\n",
497 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
501 ptype = object_update_param_get(update, 2, &size);
502 if (IS_ERR(ptype) || size != sizeof(*ptype)) {
503 CERROR("%s: invalid type for index insert: rc = %ld\n",
504 tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
505 RETURN(PTR_ERR(ptype));
508 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
512 rec->rec_type = *ptype;
514 rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
515 (const struct dt_key *)name, &tti->tti_tea,
516 tti->tti_tea.ta_handle,
517 tti->tti_u.update.tti_update_reply,
518 tti->tti_u.update.tti_update_reply_index);
522 static int out_index_delete(struct tgt_session_info *tsi)
524 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
525 struct object_update *update = tti->tti_u.update.tti_update;
526 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
530 if (!lu_object_exists(&obj->do_lu))
533 name = object_update_param_get(update, 0, NULL);
535 CERROR("%s: empty name for index delete: rc = %ld\n",
536 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
537 RETURN(PTR_ERR(name));
540 rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
541 &tti->tti_tea, tti->tti_tea.ta_handle,
542 tti->tti_u.update.tti_update_reply,
543 tti->tti_u.update.tti_update_reply_index);
547 static int out_destroy(struct tgt_session_info *tsi)
549 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
550 struct object_update *update = tti->tti_u.update.tti_update;
551 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
556 fid = &update->ou_fid;
557 if (!fid_is_sane(fid)) {
558 CERROR("%s: invalid FID "DFID": rc = %d\n",
559 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
563 if (!lu_object_exists(&obj->do_lu))
566 rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
567 tti->tti_tea.ta_handle,
568 tti->tti_u.update.tti_update_reply,
569 tti->tti_u.update.tti_update_reply_index);
574 static int out_write(struct tgt_session_info *tsi)
576 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
577 struct object_update *update = tti->tti_u.update.tti_update;
578 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
579 struct lu_buf *lbuf = &tti->tti_buf;
588 buf = object_update_param_get(update, 0, &buf_len);
589 if (IS_ERR(buf) || buf_len == 0) {
590 CERROR("%s: empty buf for xattr set: rc = %ld\n",
591 tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
592 RETURN(PTR_ERR(buf));
595 lbuf->lb_len = buf_len;
597 tmp = object_update_param_get(update, 1, &size);
598 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
599 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
600 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
601 RETURN(PTR_ERR(tmp));
604 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
608 rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
609 &tti->tti_tea, tti->tti_tea.ta_handle,
610 tti->tti_u.update.tti_update_reply,
611 tti->tti_u.update.tti_update_reply_index);
615 static int out_read(struct tgt_session_info *tsi)
617 const struct lu_env *env = tsi->tsi_env;
618 struct tgt_thread_info *tti = tgt_th_info(env);
619 struct object_update *update = tti->tti_u.update.tti_update;
620 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
621 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
622 int index = tti->tti_u.update.tti_update_reply_index;
623 struct lu_rdbuf *rdbuf;
624 struct object_update_result *update_result;
625 struct out_read_reply *orr;
628 size_t total_size = 0;
635 update_result = object_update_result_get(reply, index, NULL);
636 LASSERT(update_result != NULL);
637 update_result->our_datalen = sizeof(*orr);
639 if (!lu_object_exists(&obj->do_lu))
640 GOTO(out, rc = -ENOENT);
642 tmp = object_update_param_get(update, 0, NULL);
644 CERROR("%s: empty size for read: rc = %ld\n",
645 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
646 GOTO(out, rc = PTR_ERR(tmp));
648 size = le64_to_cpu(*(size_t *)(tmp));
650 tmp = object_update_param_get(update, 1, NULL);
652 CERROR("%s: empty pos for read: rc = %ld\n",
653 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
654 GOTO(out, rc = PTR_ERR(tmp));
656 pos = le64_to_cpu(*(__u64 *)(tmp));
658 /* Put the offset into the begining of the buffer in reply */
659 orr = (struct out_read_reply *)update_result->our_data;
661 nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
662 OBD_ALLOC(rdbuf, sizeof(struct lu_rdbuf) +
663 nbufs * sizeof(rdbuf->rb_bufs[0]));
665 GOTO(out, rc = -ENOMEM);
669 for (i = 0; i < nbufs; i++) {
672 read_size = size > OUT_BULK_BUFFER_SIZE ?
673 OUT_BULK_BUFFER_SIZE : size;
674 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
675 if (rdbuf->rb_bufs[i].lb_buf == NULL)
676 GOTO(out_free, rc = -ENOMEM);
678 rdbuf->rb_bufs[i].lb_len = read_size;
679 dt_read_lock(env, obj, MOR_TGT_CHILD);
680 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
681 dt_read_unlock(env, obj);
683 total_size += rc < 0 ? 0 : rc;
691 /* send pages to client */
692 rc = tgt_send_buffer(tsi, rdbuf);
696 orr->orr_size = total_size;
697 orr->orr_offset = pos;
699 orr_cpu_to_le(orr, orr);
700 update_result->our_datalen += orr->orr_size;
702 for (i = 0; i < nbufs; i++) {
703 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
704 OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
705 rdbuf->rb_bufs[i].lb_len);
708 OBD_FREE(rdbuf, sizeof(struct lu_rdbuf) +
709 nbufs * sizeof(rdbuf->rb_bufs[0]));
711 /* Insert read buffer */
712 update_result->our_rc = ptlrpc_status_hton(rc);
713 reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
714 sizeof(*update_result));
718 static int out_noop(struct tgt_session_info *tsi)
723 #define DEF_OUT_HNDL(opc, name, flags, fn) \
724 [opc - OUT_CREATE] = { \
734 static struct tgt_handler out_update_ops[] = {
735 DEF_OUT_HNDL(OUT_CREATE, "out_create", MUTABOR | HABEO_REFERO,
737 DEF_OUT_HNDL(OUT_DESTROY, "out_create", MUTABOR | HABEO_REFERO,
739 DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", MUTABOR | HABEO_REFERO,
741 DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", MUTABOR | HABEO_REFERO,
743 DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set", MUTABOR | HABEO_REFERO,
745 DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get", HABEO_REFERO,
747 DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", MUTABOR | HABEO_REFERO,
749 DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", MUTABOR | HABEO_REFERO,
751 DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HABEO_REFERO,
753 DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HABEO_REFERO,
755 DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
756 MUTABOR | HABEO_REFERO, out_index_insert),
757 DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
758 MUTABOR | HABEO_REFERO, out_index_delete),
759 DEF_OUT_HNDL(OUT_WRITE, "out_write", MUTABOR | HABEO_REFERO, out_write),
760 DEF_OUT_HNDL(OUT_READ, "out_read", HABEO_REFERO, out_read),
761 DEF_OUT_HNDL(OUT_NOOP, "out_noop", HABEO_REFERO, out_noop),
764 static struct tgt_handler *out_handler_find(__u32 opc)
766 struct tgt_handler *h;
769 if (OUT_CREATE <= opc && opc < OUT_LAST) {
770 h = &out_update_ops[opc - OUT_CREATE];
771 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
774 h = NULL; /* unsupported opc */
779 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
780 struct thandle_exec_args *ta, struct obd_export *exp)
783 ta->ta_handle = dt_trans_create(env, dt);
784 if (IS_ERR(ta->ta_handle)) {
787 rc = PTR_ERR(ta->ta_handle);
788 ta->ta_handle = NULL;
789 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
793 if (exp->exp_need_sync)
794 ta->ta_handle->th_sync = 1;
799 static int out_trans_start(const struct lu_env *env,
800 struct thandle_exec_args *ta)
802 return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
805 static int out_trans_stop(const struct lu_env *env,
806 struct thandle_exec_args *ta, int err)
811 ta->ta_handle->th_result = err;
812 rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
813 for (i = 0; i < ta->ta_argno; i++) {
814 if (ta->ta_args[i]->object != NULL) {
815 dt_object_put(env, ta->ta_args[i]->object);
816 ta->ta_args[i]->object = NULL;
819 ta->ta_handle = NULL;
825 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
828 struct tgt_session_info *tsi = tgt_ses_info(env);
834 if (ta->ta_handle == NULL)
837 if (declare_ret != 0 || ta->ta_argno == 0)
838 GOTO(stop, rc = declare_ret);
840 LASSERT(ta->ta_handle->th_dev != NULL);
841 rc = out_trans_start(env, ta);
842 if (unlikely(rc != 0))
845 for (i = 0; i < ta->ta_argno; i++) {
846 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
848 if (unlikely(rc != 0)) {
849 CDEBUG(D_INFO, "error during execution of #%u from"
850 " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
851 ta->ta_args[i]->line, rc);
853 if (ta->ta_args[i]->undo_fn != NULL)
854 ta->ta_args[i]->undo_fn(env,
858 CERROR("%s: undo for %s:%d: rc = %d\n",
859 dt_obd_name(ta->ta_handle->th_dev),
860 ta->ta_args[i]->file,
861 ta->ta_args[i]->line, -ENOTSUPP);
865 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
866 dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
869 /* Only fail for real updates, XXX right now llog updates will be
870 * ignore, whose updates count is usually 1, so failover test
871 * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
872 * be removed after async update patch is landed. */
873 if (ta->ta_argno > 1)
874 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
877 rc1 = out_trans_stop(env, ta, rc);
881 ta->ta_handle = NULL;
888 * Object updates between Targets. Because all the updates has been
889 * dis-assemblied into object updates at sender side, so OUT will
890 * call OSD API directly to execute these updates.
892 * In DNE phase I all of the updates in the request need to be executed
893 * in one transaction, and the transaction has to be synchronously.
895 * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
898 int out_handle(struct tgt_session_info *tsi)
900 const struct lu_env *env = tsi->tsi_env;
901 struct tgt_thread_info *tti = tgt_th_info(env);
902 struct thandle_exec_args *ta = &tti->tti_tea;
903 struct req_capsule *pill = tsi->tsi_pill;
904 struct dt_device *dt = tsi->tsi_tgt->lut_bottom;
905 struct out_update_header *ouh;
906 struct out_update_buffer *oub = NULL;
907 struct object_update *update;
908 struct object_update_reply *reply;
909 struct ptlrpc_bulk_desc *desc = NULL;
910 struct l_wait_info lwi;
912 int current_batchid = -1;
913 __u32 update_buf_count;
915 unsigned int reply_index = 0;
918 int ouh_size, reply_size;
922 req_capsule_set(pill, &RQF_OUT_UPDATE);
923 ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
926 RETURN(err_serious(-EPROTO));
928 ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
930 RETURN(err_serious(-EPROTO));
932 if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
933 CERROR("%s: invalid update buffer magic %x expect %x: "
934 "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
935 UPDATE_REQUEST_MAGIC, -EPROTO);
936 RETURN(err_serious(-EPROTO));
939 update_buf_count = ouh->ouh_count;
940 if (update_buf_count == 0)
941 RETURN(err_serious(-EPROTO));
943 OBD_ALLOC(update_bufs, sizeof(*update_bufs) * update_buf_count);
944 if (update_bufs == NULL)
945 RETURN(err_serious(-ENOMEM));
947 if (ouh->ouh_inline_length > 0) {
948 update_bufs[0] = ouh->ouh_inline_data;
950 struct out_update_buffer *tmp;
952 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
954 GOTO(out_free, rc = err_serious(-EPROTO));
956 desc = ptlrpc_prep_bulk_exp(pill->rc_req, update_buf_count,
957 PTLRPC_BULK_OPS_COUNT,
958 PTLRPC_BULK_GET_SINK |
959 PTLRPC_BULK_BUF_KVEC,
961 &ptlrpc_bulk_kvec_ops);
963 GOTO(out_free, rc = err_serious(-ENOMEM));
966 for (i = 0; i < update_buf_count; i++, tmp++) {
967 if (tmp->oub_size >= OUT_MAXREQSIZE)
968 GOTO(out_free, rc = err_serious(-EPROTO));
970 OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
971 if (update_bufs[i] == NULL)
972 GOTO(out_free, rc = err_serious(-ENOMEM));
974 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
978 pill->rc_req->rq_bulk_write = 1;
979 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
981 GOTO(out_free, rc = err_serious(rc));
983 rc = target_bulk_io(pill->rc_req->rq_export, desc, &lwi);
985 GOTO(out_free, rc = err_serious(rc));
987 /* validate the request and calculate the total update count and
991 for (i = 0; i < update_buf_count; i++) {
992 struct object_update_request *our;
995 our = update_bufs[i];
996 if (ptlrpc_req_need_swab(pill->rc_req))
997 lustre_swab_object_update_request(our);
999 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1000 CERROR("%s: invalid update buffer magic %x"
1001 " expect %x: rc = %d\n",
1002 tgt_name(tsi->tsi_tgt), our->ourq_magic,
1003 UPDATE_REQUEST_MAGIC, -EPROTO);
1004 GOTO(out_free, rc = err_serious(-EPROTO));
1006 updates += our->ourq_count;
1008 /* need to calculate reply size */
1009 for (j = 0; j < our->ourq_count; j++) {
1010 update = object_update_request_get(our, j, NULL);
1012 GOTO(out, rc = err_serious(-EPROTO));
1013 if (ptlrpc_req_need_swab(pill->rc_req))
1014 lustre_swab_object_update(update);
1016 if (!fid_is_sane(&update->ou_fid)) {
1017 CERROR("%s: invalid FID "DFID": rc = %d\n",
1018 tgt_name(tsi->tsi_tgt),
1019 PFID(&update->ou_fid), -EPROTO);
1020 GOTO(out, rc = err_serious(-EPROTO));
1023 /* XXX: what ou_result_size can be considered safe? */
1025 reply_size += sizeof(reply->ourp_lens[0]);
1026 reply_size += sizeof(struct object_update_result);
1027 reply_size += update->ou_result_size;
1030 reply_size += sizeof(*reply);
1032 if (unlikely(reply_size > ouh->ouh_reply_size)) {
1033 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1034 tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1035 updates, reply_size);
1036 GOTO(out_free, rc = err_serious(-EPROTO));
1039 req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1040 ouh->ouh_reply_size);
1041 rc = req_capsule_server_pack(pill);
1043 CERROR("%s: Can't pack response: rc = %d\n",
1044 tgt_name(tsi->tsi_tgt), rc);
1045 GOTO(out_free, rc = err_serious(-EPROTO));
1048 /* Prepare the update reply buffer */
1049 reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1051 GOTO(out_free, rc = -EPROTO);
1052 reply->ourp_magic = UPDATE_REPLY_MAGIC;
1053 reply->ourp_count = updates;
1054 tti->tti_u.update.tti_update_reply = reply;
1055 tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1057 /* Walk through updates in the request to execute them */
1058 for (i = 0; i < update_buf_count; i++) {
1059 struct tgt_handler *h;
1060 struct dt_object *dt_obj;
1062 struct object_update_request *our;
1065 our = update_bufs[i];
1066 update_count = our->ourq_count;
1067 for (j = 0; j < update_count; j++) {
1068 struct lu_object_conf conf;
1070 update = object_update_request_get(our, j, NULL);
1071 if (update->ou_type == OUT_CREATE)
1072 conf.loc_flags = LOC_F_NEW;
1076 dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1077 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1079 GOTO(out, rc = PTR_ERR(dt_obj));
1081 if (dt->dd_record_fid_accessed) {
1082 struct lfsck_req_local *lrl = &tti->tti_lrl;
1085 lu_object_fid(&dt_obj->do_lu),
1088 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1091 tti->tti_u.update.tti_dt_object = dt_obj;
1092 tti->tti_u.update.tti_update = update;
1093 tti->tti_u.update.tti_update_reply_index = reply_index;
1095 h = out_handler_find(update->ou_type);
1096 if (unlikely(h == NULL)) {
1097 CERROR("%s: unsupported opc: 0x%x\n",
1098 tgt_name(tsi->tsi_tgt), update->ou_type);
1099 GOTO(next, rc = -ENOTSUPP);
1102 /* Check resend case only for modifying RPC */
1103 if (h->th_flags & MUTABOR) {
1104 struct ptlrpc_request *req = tgt_ses_req(tsi);
1106 if (out_check_resent(env, dt, dt_obj, req,
1107 out_reconstruct, reply,
1112 GOTO(next, rc = -EROFS);
1115 /* start transaction for modification RPC only */
1116 if (h->th_flags & MUTABOR && current_batchid == -1) {
1117 current_batchid = update->ou_batchid;
1118 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1122 if (update->ou_flags & UPDATE_FL_SYNC)
1123 ta->ta_handle->th_sync = 1;
1126 /* Stop the current update transaction, if the update
1127 * has different batchid, or read-only update */
1128 if (((current_batchid != update->ou_batchid) ||
1129 !(h->th_flags & MUTABOR)) &&
1130 ta->ta_handle != NULL) {
1131 rc = out_tx_end(env, ta, rc);
1132 current_batchid = -1;
1136 /* start a new transaction if needed */
1137 if (h->th_flags & MUTABOR) {
1138 rc = out_tx_start(env, dt, ta,
1142 if (update->ou_flags & UPDATE_FL_SYNC)
1143 ta->ta_handle->th_sync = 1;
1144 current_batchid = update->ou_batchid;
1148 rc = h->th_act(tsi);
1151 dt_object_put(env, dt_obj);
1157 if (current_batchid != -1) {
1158 rc1 = out_tx_end(env, ta, rc);
1164 if (update_bufs != NULL) {
1166 for (i = 0; i < update_buf_count; i++, oub++) {
1167 if (update_bufs[i] != NULL)
1168 OBD_FREE_LARGE(update_bufs[i],
1173 OBD_FREE(update_bufs, sizeof(*update_bufs) * update_buf_count);
1177 ptlrpc_free_bulk(desc);
1182 struct tgt_handler tgt_out_handlers[] = {
1183 TGT_UPDATE_HDL(MUTABOR, OUT_UPDATE, out_handle),
1185 EXPORT_SYMBOL(tgt_out_handlers);