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);
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 int out_check_resent(const struct lu_env *env,
69 struct dt_object *obj,
70 struct ptlrpc_request *req,
71 out_reconstruct_t reconstruct,
72 struct object_update_reply *reply,
75 if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
78 if (req_xid_is_last(req)) {
79 struct lsd_client_data *lcd;
81 /* XXX this does not support mulitple transactions yet, i.e.
82 * only 1 update RPC each time betwee MDTs */
83 lcd = req->rq_export->exp_target_data.ted_lcd;
85 req->rq_transno = lcd->lcd_last_transno;
86 req->rq_status = lcd->lcd_last_result;
87 if (req->rq_status != 0)
89 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
90 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
92 DEBUG_REQ(D_RPCTRACE, req, "restoring resent RPC");
94 reconstruct(env, dt, obj, reply, index);
97 DEBUG_REQ(D_HA, req, "no reply for RESENT req (have %lld)",
98 req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
102 static int out_create(struct tgt_session_info *tsi)
104 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
105 struct object_update *update = tti->tti_u.update.tti_update;
106 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
107 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
108 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
109 struct lu_attr *attr = &tti->tti_attr;
110 struct lu_fid *fid = NULL;
117 wobdo = object_update_param_get(update, 0, &size);
118 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
119 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
120 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
121 RETURN(PTR_ERR(wobdo));
124 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
125 lustre_swab_obdo(wobdo);
126 lustre_get_wire_obdo(NULL, lobdo, wobdo);
127 la_from_obdo(attr, lobdo, lobdo->o_valid);
129 dof->dof_type = dt_mode_to_dft(attr->la_mode);
130 if (update->ou_params_count > 1) {
131 fid = object_update_param_get(update, 1, &size);
132 if (IS_ERR(fid) || size != sizeof(*fid)) {
133 CERROR("%s: invalid fid: rc = %ld\n",
134 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
135 RETURN(PTR_ERR(fid));
137 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
138 lustre_swab_lu_fid(fid);
139 if (!fid_is_sane(fid)) {
140 CERROR("%s: invalid fid "DFID": rc = %d\n",
141 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
146 if (lu_object_exists(&obj->do_lu))
149 rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
150 &tti->tti_tea, tti->tti_tea.ta_handle,
151 tti->tti_u.update.tti_update_reply,
152 tti->tti_u.update.tti_update_reply_index);
157 static int out_attr_set(struct tgt_session_info *tsi)
159 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
160 struct object_update *update = tti->tti_u.update.tti_update;
161 struct lu_attr *attr = &tti->tti_attr;
162 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
163 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
170 wobdo = object_update_param_get(update, 0, &size);
171 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
172 CERROR("%s: empty obdo in the update: rc = %ld\n",
173 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
174 RETURN(PTR_ERR(wobdo));
180 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
181 lustre_swab_obdo(wobdo);
182 lustre_get_wire_obdo(NULL, lobdo, wobdo);
183 la_from_obdo(attr, lobdo, lobdo->o_valid);
185 rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
186 tti->tti_tea.ta_handle,
187 tti->tti_u.update.tti_update_reply,
188 tti->tti_u.update.tti_update_reply_index);
193 static int out_attr_get(struct tgt_session_info *tsi)
195 const struct lu_env *env = tsi->tsi_env;
196 struct tgt_thread_info *tti = tgt_th_info(env);
197 struct object_update *update = tti->tti_u.update.tti_update;
198 struct obdo *obdo = &tti->tti_u.update.tti_obdo;
199 struct lu_attr *la = &tti->tti_attr;
200 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
201 int idx = tti->tti_u.update.tti_update_reply_index;
206 if (unlikely(update->ou_result_size < sizeof(*obdo)))
209 if (!lu_object_exists(&obj->do_lu)) {
210 /* Usually, this will be called when the master MDT try
211 * to init a remote object(see osp_object_init), so if
212 * the object does not exist on slave, we need set BANSHEE flag,
213 * so the object can be removed from the cache immediately */
214 set_bit(LU_OBJECT_HEARD_BANSHEE,
215 &obj->do_lu.lo_header->loh_flags);
219 dt_read_lock(env, obj, DT_TGT_CHILD);
220 rc = dt_attr_get(env, obj, la);
222 GOTO(out_unlock, rc);
225 obdo_from_la(obdo, la, la->la_valid);
228 dt_read_unlock(env, obj);
230 CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
231 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
234 object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
235 sizeof(*obdo), idx, rc);
240 static int out_xattr_get(struct tgt_session_info *tsi)
242 const struct lu_env *env = tsi->tsi_env;
243 struct tgt_thread_info *tti = tgt_th_info(env);
244 struct object_update *update = tti->tti_u.update.tti_update;
245 struct lu_buf *lbuf = &tti->tti_buf;
246 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
247 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
249 struct object_update_result *update_result;
250 int idx = tti->tti_u.update.tti_update_reply_index;
255 if (!lu_object_exists(&obj->do_lu)) {
256 set_bit(LU_OBJECT_HEARD_BANSHEE,
257 &obj->do_lu.lo_header->loh_flags);
261 name = object_update_param_get(update, 0, NULL);
263 CERROR("%s: empty name for xattr get: rc = %ld\n",
264 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
265 RETURN(PTR_ERR(name));
268 update_result = object_update_result_get(reply, idx, NULL);
269 if (update_result == NULL) {
270 CERROR("%s: empty name for xattr get: rc = %d\n",
271 tgt_name(tsi->tsi_tgt), -EPROTO);
275 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
276 if (lbuf->lb_len == 0)
279 lbuf->lb_buf = update_result->our_data;
281 dt_read_lock(env, obj, DT_TGT_CHILD);
282 rc = dt_xattr_get(env, obj, lbuf, name);
283 dt_read_unlock(env, obj);
288 } else if (lbuf->lb_buf) {
291 CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
292 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
298 object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
302 static int out_xattr_list(struct tgt_session_info *tsi)
304 const struct lu_env *env = tsi->tsi_env;
305 struct tgt_thread_info *tti = tgt_th_info(env);
306 struct lu_buf *lbuf = &tti->tti_buf;
307 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
308 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
309 struct object_update_result *update_result;
310 int idx = tti->tti_u.update.tti_update_reply_index;
315 if (!lu_object_exists(&obj->do_lu)) {
316 set_bit(LU_OBJECT_HEARD_BANSHEE,
317 &obj->do_lu.lo_header->loh_flags);
321 update_result = object_update_result_get(reply, 0, NULL);
322 if (!update_result) {
324 CERROR("%s: empty buf for xattr list: rc = %d\n",
325 tgt_name(tsi->tsi_tgt), rc);
329 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
330 lbuf->lb_buf = update_result->our_data;
331 if (lbuf->lb_len == 0)
334 dt_read_lock(env, obj, DT_TGT_CHILD);
335 rc = dt_xattr_list(env, obj, lbuf);
336 dt_read_unlock(env, obj);
341 } else if (lbuf->lb_buf) {
345 CDEBUG(D_INFO, "%s: "DFID" list xattr len %d\n",
346 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)), rc);
348 /* Since we directly use update_result->our_data as the lbuf->lb_buf,
349 * then use NULL for result_insert to avoid unnecessary memory copy. */
350 object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
355 static int out_index_lookup(struct tgt_session_info *tsi)
357 const struct lu_env *env = tsi->tsi_env;
358 struct tgt_thread_info *tti = tgt_th_info(env);
359 struct object_update *update = tti->tti_u.update.tti_update;
360 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
366 if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
369 if (!lu_object_exists(&obj->do_lu))
372 name = object_update_param_get(update, 0, NULL);
374 CERROR("%s: empty name for lookup: rc = %ld\n",
375 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
376 RETURN(PTR_ERR(name));
379 dt_read_lock(env, obj, DT_TGT_CHILD);
380 if (!dt_try_as_dir(env, obj))
381 GOTO(out_unlock, rc = -ENOTDIR);
383 rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
384 (struct dt_key *)name);
387 GOTO(out_unlock, rc);
393 dt_read_unlock(env, obj);
395 CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
396 PFID(lu_object_fid(&obj->do_lu)), name,
397 PFID(&tti->tti_fid1), rc);
399 CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
400 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
403 object_update_result_insert(tti->tti_u.update.tti_update_reply,
404 &tti->tti_fid1, sizeof(tti->tti_fid1),
405 tti->tti_u.update.tti_update_reply_index, rc);
409 static int out_xattr_set(struct tgt_session_info *tsi)
411 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
412 struct object_update *update = tti->tti_u.update.tti_update;
413 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
414 struct lu_buf *lbuf = &tti->tti_buf;
424 name = object_update_param_get(update, 0, NULL);
426 CERROR("%s: empty name for xattr set: rc = %ld\n",
427 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
428 RETURN(PTR_ERR(name));
431 /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
432 buf = object_update_param_get(update, 1, &buf_len);
433 if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
434 RETURN(PTR_ERR(buf));
437 lbuf->lb_len = buf_len;
439 tmp = object_update_param_get(update, 2, &size);
440 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
441 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
442 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
443 RETURN(PTR_ERR(tmp));
446 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
450 rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
451 &tti->tti_tea, tti->tti_tea.ta_handle,
452 tti->tti_u.update.tti_update_reply,
453 tti->tti_u.update.tti_update_reply_index);
457 static int out_xattr_del(struct tgt_session_info *tsi)
459 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
460 struct object_update *update = tti->tti_u.update.tti_update;
461 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
466 name = object_update_param_get(update, 0, NULL);
468 CERROR("%s: empty name for xattr set: rc = %ld\n",
469 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
470 RETURN(PTR_ERR(name));
473 rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
474 tti->tti_tea.ta_handle,
475 tti->tti_u.update.tti_update_reply,
476 tti->tti_u.update.tti_update_reply_index);
481 * increase ref of the object
483 static int out_ref_add(struct tgt_session_info *tsi)
485 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
486 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
491 rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
492 tti->tti_tea.ta_handle,
493 tti->tti_u.update.tti_update_reply,
494 tti->tti_u.update.tti_update_reply_index);
498 static int out_ref_del(struct tgt_session_info *tsi)
500 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
501 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
506 if (!lu_object_exists(&obj->do_lu))
509 rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
510 tti->tti_tea.ta_handle,
511 tti->tti_u.update.tti_update_reply,
512 tti->tti_u.update.tti_update_reply_index);
516 static int out_index_insert(struct tgt_session_info *tsi)
518 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
519 struct object_update *update = tti->tti_u.update.tti_update;
520 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
521 struct dt_insert_rec *rec = &tti->tti_rec;
529 name = object_update_param_get(update, 0, NULL);
531 CERROR("%s: empty name for index insert: rc = %ld\n",
532 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
533 RETURN(PTR_ERR(name));
536 fid = object_update_param_get(update, 1, &size);
537 if (IS_ERR(fid) || size != sizeof(*fid)) {
538 CERROR("%s: invalid fid: rc = %ld\n",
539 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
540 RETURN(PTR_ERR(fid));
543 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
544 lustre_swab_lu_fid(fid);
546 if (!fid_is_sane(fid)) {
547 CERROR("%s: invalid FID "DFID": rc = %d\n",
548 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
552 ptype = object_update_param_get(update, 2, &size);
553 if (IS_ERR(ptype) || size != sizeof(*ptype)) {
554 CERROR("%s: invalid type for index insert: rc = %ld\n",
555 tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
556 RETURN(PTR_ERR(ptype));
559 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
563 rec->rec_type = *ptype;
565 rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
566 (const struct dt_key *)name, &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);
571 CDEBUG(D_INFO, "%s: "DFID" index insert %s: rc = %d\n",
572 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
578 static int out_index_delete(struct tgt_session_info *tsi)
580 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
581 struct object_update *update = tti->tti_u.update.tti_update;
582 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
586 if (!lu_object_exists(&obj->do_lu))
589 name = object_update_param_get(update, 0, NULL);
591 CERROR("%s: empty name for index delete: rc = %ld\n",
592 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
593 RETURN(PTR_ERR(name));
596 rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
597 &tti->tti_tea, tti->tti_tea.ta_handle,
598 tti->tti_u.update.tti_update_reply,
599 tti->tti_u.update.tti_update_reply_index);
603 static int out_destroy(struct tgt_session_info *tsi)
605 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
606 struct object_update *update = tti->tti_u.update.tti_update;
607 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
612 fid = &update->ou_fid;
613 if (!fid_is_sane(fid)) {
614 CERROR("%s: invalid FID "DFID": rc = %d\n",
615 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
619 if (!lu_object_exists(&obj->do_lu))
622 rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
623 tti->tti_tea.ta_handle,
624 tti->tti_u.update.tti_update_reply,
625 tti->tti_u.update.tti_update_reply_index);
630 static int out_write(struct tgt_session_info *tsi)
632 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
633 struct object_update *update = tti->tti_u.update.tti_update;
634 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
635 struct lu_buf *lbuf = &tti->tti_buf;
644 buf = object_update_param_get(update, 0, &buf_len);
645 if (IS_ERR(buf) || buf_len == 0) {
646 CERROR("%s: empty buf for xattr set: rc = %ld\n",
647 tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
648 RETURN(PTR_ERR(buf));
651 lbuf->lb_len = buf_len;
653 tmp = object_update_param_get(update, 1, &size);
654 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
655 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
656 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
657 RETURN(PTR_ERR(tmp));
660 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
664 rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
665 &tti->tti_tea, tti->tti_tea.ta_handle,
666 tti->tti_u.update.tti_update_reply,
667 tti->tti_u.update.tti_update_reply_index);
671 static int out_read(struct tgt_session_info *tsi)
673 const struct lu_env *env = tsi->tsi_env;
674 struct tgt_thread_info *tti = tgt_th_info(env);
675 struct object_update *update = tti->tti_u.update.tti_update;
676 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
677 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
678 int index = tti->tti_u.update.tti_update_reply_index;
679 struct lu_rdbuf *rdbuf;
680 struct object_update_result *update_result;
681 struct out_read_reply *orr;
684 size_t total_size = 0;
691 update_result = object_update_result_get(reply, index, NULL);
692 LASSERT(update_result != NULL);
693 update_result->our_datalen = sizeof(*orr);
695 if (!lu_object_exists(&obj->do_lu))
696 GOTO(out, rc = -ENOENT);
698 tmp = object_update_param_get(update, 0, NULL);
700 CERROR("%s: empty size for read: rc = %ld\n",
701 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
702 GOTO(out, rc = PTR_ERR(tmp));
704 size = le64_to_cpu(*(size_t *)(tmp));
706 tmp = object_update_param_get(update, 1, NULL);
708 CERROR("%s: empty pos for read: rc = %ld\n",
709 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
710 GOTO(out, rc = PTR_ERR(tmp));
712 pos = le64_to_cpu(*(__u64 *)(tmp));
714 /* Put the offset into the begining of the buffer in reply */
715 orr = (struct out_read_reply *)update_result->our_data;
717 nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
718 OBD_ALLOC(rdbuf, sizeof(*rdbuf) + nbufs * sizeof(rdbuf->rb_bufs[0]));
720 GOTO(out, rc = -ENOMEM);
724 for (i = 0; i < nbufs; i++) {
727 read_size = size > OUT_BULK_BUFFER_SIZE ?
728 OUT_BULK_BUFFER_SIZE : size;
729 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
730 if (rdbuf->rb_bufs[i].lb_buf == NULL)
731 GOTO(out_free, rc = -ENOMEM);
733 rdbuf->rb_bufs[i].lb_len = read_size;
734 dt_read_lock(env, obj, DT_TGT_CHILD);
735 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
736 dt_read_unlock(env, obj);
738 total_size += rc < 0 ? 0 : rc;
746 /* send pages to client */
747 rc = tgt_send_buffer(tsi, rdbuf);
751 orr->orr_size = total_size;
752 orr->orr_offset = pos;
754 orr_cpu_to_le(orr, orr);
755 update_result->our_datalen += orr->orr_size;
757 for (i = 0; i < nbufs; i++) {
758 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
759 OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
760 rdbuf->rb_bufs[i].lb_len);
763 OBD_FREE(rdbuf, sizeof(*rdbuf) +
764 nbufs * sizeof(rdbuf->rb_bufs[0]));
766 /* Insert read buffer */
767 update_result->our_rc = ptlrpc_status_hton(rc);
768 reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
769 sizeof(*update_result));
773 static int out_noop(struct tgt_session_info *tsi)
778 #define DEF_OUT_HNDL(opc, name, flags, fn) \
779 [opc - OUT_CREATE] = { \
789 static struct tgt_handler out_update_ops[] = {
790 DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
792 DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
794 DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
796 DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
798 DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set", IS_MUTABLE | HAS_REPLY,
800 DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get", HAS_REPLY,
802 DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
804 DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
806 DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
808 DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
810 DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
811 IS_MUTABLE | HAS_REPLY, out_index_insert),
812 DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
813 IS_MUTABLE | HAS_REPLY, out_index_delete),
814 DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
815 DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
816 DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
817 DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
821 static struct tgt_handler *out_handler_find(__u32 opc)
823 struct tgt_handler *h;
826 if (OUT_CREATE <= opc && opc < OUT_LAST) {
827 h = &out_update_ops[opc - OUT_CREATE];
828 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
831 h = NULL; /* unsupported opc */
836 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
837 struct thandle_exec_args *ta, struct obd_export *exp)
840 ta->ta_handle = dt_trans_create(env, dt);
841 if (IS_ERR(ta->ta_handle)) {
844 rc = PTR_ERR(ta->ta_handle);
845 ta->ta_handle = NULL;
846 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
850 if (exp->exp_need_sync)
851 ta->ta_handle->th_sync = 1;
856 static int out_trans_start(const struct lu_env *env,
857 struct thandle_exec_args *ta)
859 return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
862 static int out_trans_stop(const struct lu_env *env,
863 struct thandle_exec_args *ta, int err)
868 ta->ta_handle->th_result = err;
869 rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
870 for (i = 0; i < ta->ta_argno; i++) {
871 if (ta->ta_args[i]->object != NULL) {
872 dt_object_put(env, ta->ta_args[i]->object);
873 ta->ta_args[i]->object = NULL;
876 ta->ta_handle = NULL;
882 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
885 struct tgt_session_info *tsi = tgt_ses_info(env);
891 if (ta->ta_handle == NULL)
894 if (declare_ret != 0 || ta->ta_argno == 0)
895 GOTO(stop, rc = declare_ret);
897 LASSERT(ta->ta_handle->th_dev != NULL);
898 rc = out_trans_start(env, ta);
899 if (unlikely(rc != 0))
902 for (i = 0; i < ta->ta_argno; i++) {
903 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
905 if (unlikely(rc != 0)) {
906 CDEBUG(D_INFO, "error during execution of #%u from"
907 " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
908 ta->ta_args[i]->line, rc);
910 if (ta->ta_args[i]->undo_fn != NULL)
911 ta->ta_args[i]->undo_fn(env,
915 CERROR("%s: undo for %s:%d: rc = %d\n",
916 dt_obd_name(ta->ta_handle->th_dev),
917 ta->ta_args[i]->file,
918 ta->ta_args[i]->line, -ENOTSUPP);
922 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
923 dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
926 /* Only fail for real updates, XXX right now llog updates will be
927 * ignore, whose updates count is usually 1, so failover test
928 * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
929 * be removed after async update patch is landed. */
930 if (ta->ta_argno > 1)
931 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
934 rc1 = out_trans_stop(env, ta, rc);
938 ta->ta_handle = NULL;
945 * Object updates between Targets. Because all the updates has been
946 * dis-assemblied into object updates at sender side, so OUT will
947 * call OSD API directly to execute these updates.
949 * In DNE phase I all of the updates in the request need to be executed
950 * in one transaction, and the transaction has to be synchronously.
952 * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
955 int out_handle(struct tgt_session_info *tsi)
957 const struct lu_env *env = tsi->tsi_env;
958 struct tgt_thread_info *tti = tgt_th_info(env);
959 struct thandle_exec_args *ta = &tti->tti_tea;
960 struct req_capsule *pill = tsi->tsi_pill;
961 struct dt_device *dt = tsi->tsi_tgt->lut_bottom;
962 struct out_update_header *ouh;
963 struct out_update_buffer *oub = NULL;
964 struct object_update *update;
965 struct object_update_reply *reply;
966 struct ptlrpc_bulk_desc *desc = NULL;
968 int current_batchid = -1;
969 __u32 update_buf_count;
971 unsigned int reply_index = 0;
974 int ouh_size, reply_size;
978 req_capsule_set(pill, &RQF_OUT_UPDATE);
979 ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
982 RETURN(err_serious(-EPROTO));
984 ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
986 RETURN(err_serious(-EPROTO));
988 if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
989 CERROR("%s: invalid update buffer magic %x expect %x: "
990 "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
991 UPDATE_REQUEST_MAGIC, -EPROTO);
992 RETURN(err_serious(-EPROTO));
995 update_buf_count = ouh->ouh_count;
996 if (update_buf_count == 0)
997 RETURN(err_serious(-EPROTO));
999 OBD_ALLOC(update_bufs, sizeof(*update_bufs) * update_buf_count);
1000 if (update_bufs == NULL)
1001 RETURN(err_serious(-ENOMEM));
1003 if (ouh->ouh_inline_length > 0) {
1004 update_bufs[0] = ouh->ouh_inline_data;
1006 struct out_update_buffer *tmp;
1009 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
1011 GOTO(out_free, rc = err_serious(-EPROTO));
1013 for (i = 0; i < update_buf_count; i++)
1014 /* First *and* last might be partial pages, hence +1 */
1015 page_count += DIV_ROUND_UP(oub[i].oub_size,
1018 desc = ptlrpc_prep_bulk_exp(pill->rc_req, page_count,
1019 PTLRPC_BULK_OPS_COUNT,
1020 PTLRPC_BULK_GET_SINK,
1022 &ptlrpc_bulk_kiov_nopin_ops);
1024 GOTO(out_free, rc = err_serious(-ENOMEM));
1027 for (i = 0; i < update_buf_count; i++, tmp++) {
1028 if (tmp->oub_size >= OUT_MAXREQSIZE)
1029 GOTO(out_free, rc = err_serious(-EPROTO));
1031 OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1032 if (update_bufs[i] == NULL)
1033 GOTO(out_free, rc = err_serious(-ENOMEM));
1035 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1039 pill->rc_req->rq_bulk_write = 1;
1040 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1042 GOTO(out_free, rc = err_serious(rc));
1044 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1046 GOTO(out_free, rc = err_serious(rc));
1048 /* validate the request and calculate the total update count and
1049 * set it to reply */
1052 for (i = 0; i < update_buf_count; i++) {
1053 struct object_update_request *our;
1056 our = update_bufs[i];
1057 if (ptlrpc_req_need_swab(pill->rc_req))
1058 lustre_swab_object_update_request(our, 0);
1060 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1061 CERROR("%s: invalid update buffer magic %x"
1062 " expect %x: rc = %d\n",
1063 tgt_name(tsi->tsi_tgt), our->ourq_magic,
1064 UPDATE_REQUEST_MAGIC, -EPROTO);
1065 GOTO(out_free, rc = err_serious(-EPROTO));
1067 updates += our->ourq_count;
1069 /* need to calculate reply size */
1070 for (j = 0; j < our->ourq_count; j++) {
1071 update = object_update_request_get(our, j, NULL);
1073 GOTO(out, rc = err_serious(-EPROTO));
1074 if (ptlrpc_req_need_swab(pill->rc_req))
1075 lustre_swab_object_update(update);
1077 if (!fid_is_sane(&update->ou_fid)) {
1078 CERROR("%s: invalid FID "DFID": rc = %d\n",
1079 tgt_name(tsi->tsi_tgt),
1080 PFID(&update->ou_fid), -EPROTO);
1081 GOTO(out, rc = err_serious(-EPROTO));
1084 /* XXX: what ou_result_size can be considered safe? */
1086 reply_size += sizeof(reply->ourp_lens[0]);
1087 reply_size += sizeof(struct object_update_result);
1088 reply_size += update->ou_result_size;
1091 reply_size += sizeof(*reply);
1093 if (unlikely(reply_size > ouh->ouh_reply_size)) {
1094 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1095 tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1096 updates, reply_size);
1097 GOTO(out_free, rc = err_serious(-EPROTO));
1100 req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1101 ouh->ouh_reply_size);
1102 rc = req_capsule_server_pack(pill);
1104 CERROR("%s: Can't pack response: rc = %d\n",
1105 tgt_name(tsi->tsi_tgt), rc);
1106 GOTO(out_free, rc = err_serious(-EPROTO));
1109 /* Prepare the update reply buffer */
1110 reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1112 GOTO(out_free, rc = -EPROTO);
1113 reply->ourp_magic = UPDATE_REPLY_MAGIC;
1114 reply->ourp_count = updates;
1115 tti->tti_u.update.tti_update_reply = reply;
1116 tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1118 /* Walk through updates in the request to execute them */
1119 for (i = 0; i < update_buf_count; i++) {
1120 struct tgt_handler *h;
1121 struct dt_object *dt_obj;
1123 struct object_update_request *our;
1126 our = update_bufs[i];
1127 update_count = our->ourq_count;
1128 for (j = 0; j < update_count; j++) {
1129 struct lu_object_conf conf;
1131 update = object_update_request_get(our, j, NULL);
1132 if (update->ou_type == OUT_CREATE)
1133 conf.loc_flags = LOC_F_NEW;
1137 dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1138 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1140 GOTO(out, rc = PTR_ERR(dt_obj));
1142 if (dt->dd_record_fid_accessed) {
1143 struct lfsck_req_local *lrl = &tti->tti_lrl;
1146 lu_object_fid(&dt_obj->do_lu),
1149 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1152 tti->tti_u.update.tti_dt_object = dt_obj;
1153 tti->tti_u.update.tti_update = update;
1154 tti->tti_u.update.tti_update_reply_index = reply_index;
1156 h = out_handler_find(update->ou_type);
1157 if (unlikely(h == NULL)) {
1158 CERROR("%s: unsupported opc: 0x%x\n",
1159 tgt_name(tsi->tsi_tgt), update->ou_type);
1160 GOTO(next, rc = -ENOTSUPP);
1163 /* Check resend case only for modifying RPC */
1164 if (h->th_flags & IS_MUTABLE) {
1165 struct ptlrpc_request *req = tgt_ses_req(tsi);
1167 if (out_check_resent(env, dt, dt_obj, req,
1168 out_reconstruct, reply,
1173 GOTO(next, rc = -EROFS);
1176 /* start transaction for modification RPC only */
1177 if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1178 current_batchid = update->ou_batchid;
1179 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1183 if (update->ou_flags & UPDATE_FL_SYNC)
1184 ta->ta_handle->th_sync = 1;
1187 /* Stop the current update transaction, if the update
1188 * has different batchid, or read-only update */
1189 if (((current_batchid != update->ou_batchid) ||
1190 !(h->th_flags & IS_MUTABLE)) &&
1191 ta->ta_handle != NULL) {
1192 rc = out_tx_end(env, ta, rc);
1193 current_batchid = -1;
1197 /* start a new transaction if needed */
1198 if (h->th_flags & IS_MUTABLE) {
1199 rc = out_tx_start(env, dt, ta,
1203 if (update->ou_flags & UPDATE_FL_SYNC)
1204 ta->ta_handle->th_sync = 1;
1205 current_batchid = update->ou_batchid;
1209 rc = h->th_act(tsi);
1212 dt_object_put(env, dt_obj);
1218 if (current_batchid != -1) {
1219 rc1 = out_tx_end(env, ta, rc);
1225 if (update_bufs != NULL) {
1227 for (i = 0; i < update_buf_count; i++, oub++) {
1228 if (update_bufs[i] != NULL)
1229 OBD_FREE_LARGE(update_bufs[i],
1234 OBD_FREE(update_bufs, sizeof(*update_bufs) * update_buf_count);
1238 ptlrpc_free_bulk(desc);
1243 struct tgt_handler tgt_out_handlers[] = {
1244 TGT_UPDATE_HDL(IS_MUTABLE, OUT_UPDATE, out_handle),
1246 EXPORT_SYMBOL(tgt_out_handlers);