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)
69 if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
72 if (req_xid_is_last(req)) {
73 struct lsd_client_data *lcd;
75 /* XXX this does not support mulitple transactions yet, i.e.
76 * only 1 update RPC each time betwee MDTs */
77 lcd = req->rq_export->exp_target_data.ted_lcd;
79 req->rq_transno = lcd->lcd_last_transno;
80 req->rq_status = lcd->lcd_last_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, "reprocess RESENT req, last_xid is %lld",
90 req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
94 static int out_create(struct tgt_session_info *tsi)
96 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
97 struct object_update *update = tti->tti_u.update.tti_update;
98 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
99 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
100 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
101 struct lu_attr *attr = &tti->tti_attr;
102 struct lu_fid *fid = NULL;
109 wobdo = object_update_param_get(update, 0, &size);
110 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
111 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
112 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
113 RETURN(PTR_ERR(wobdo));
116 if (req_capsule_req_need_swab(tsi->tsi_pill))
117 lustre_swab_obdo(wobdo);
118 lustre_get_wire_obdo(NULL, lobdo, wobdo);
119 la_from_obdo(attr, lobdo, lobdo->o_valid);
121 dof->dof_type = dt_mode_to_dft(attr->la_mode);
122 if (update->ou_params_count > 1) {
123 fid = object_update_param_get(update, 1, &size);
124 if (IS_ERR(fid) || size != sizeof(*fid)) {
125 CERROR("%s: invalid fid: rc = %ld\n",
126 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
127 RETURN(PTR_ERR(fid));
129 if (req_capsule_req_need_swab(tsi->tsi_pill))
130 lustre_swab_lu_fid(fid);
131 if (!fid_is_sane(fid)) {
132 CERROR("%s: invalid fid "DFID": rc = %d\n",
133 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
138 if (lu_object_exists(&obj->do_lu))
141 rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
142 &tti->tti_tea, tti->tti_tea.ta_handle,
143 tti->tti_u.update.tti_update_reply,
144 tti->tti_u.update.tti_update_reply_index);
149 static int out_attr_set(struct tgt_session_info *tsi)
151 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
152 struct object_update *update = tti->tti_u.update.tti_update;
153 struct lu_attr *attr = &tti->tti_attr;
154 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
155 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
162 wobdo = object_update_param_get(update, 0, &size);
163 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
164 CERROR("%s: empty obdo in the update: rc = %ld\n",
165 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
166 RETURN(PTR_ERR(wobdo));
172 if (req_capsule_req_need_swab(tsi->tsi_pill))
173 lustre_swab_obdo(wobdo);
174 lustre_get_wire_obdo(NULL, lobdo, wobdo);
175 la_from_obdo(attr, lobdo, lobdo->o_valid);
177 rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
178 tti->tti_tea.ta_handle,
179 tti->tti_u.update.tti_update_reply,
180 tti->tti_u.update.tti_update_reply_index);
185 static int out_attr_get(struct tgt_session_info *tsi)
187 const struct lu_env *env = tsi->tsi_env;
188 struct tgt_thread_info *tti = tgt_th_info(env);
189 struct object_update *update = tti->tti_u.update.tti_update;
190 struct obdo *obdo = &tti->tti_u.update.tti_obdo;
191 struct lu_attr *la = &tti->tti_attr;
192 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
193 int idx = tti->tti_u.update.tti_update_reply_index;
198 if (unlikely(update->ou_result_size < sizeof(*obdo)))
201 if (!lu_object_exists(&obj->do_lu)) {
202 /* Usually, this will be called when the master MDT try
203 * to init a remote object(see osp_object_init), so if
204 * the object does not exist on slave, we need set BANSHEE flag,
205 * so the object can be removed from the cache immediately */
206 set_bit(LU_OBJECT_HEARD_BANSHEE,
207 &obj->do_lu.lo_header->loh_flags);
211 dt_read_lock(env, obj, DT_TGT_CHILD);
212 rc = dt_attr_get(env, obj, la);
214 GOTO(out_unlock, rc);
217 obdo_from_la(obdo, la, la->la_valid);
220 dt_read_unlock(env, obj);
222 CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
223 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
226 object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
227 sizeof(*obdo), idx, rc);
232 static int out_xattr_get(struct tgt_session_info *tsi)
234 const struct lu_env *env = tsi->tsi_env;
235 struct tgt_thread_info *tti = tgt_th_info(env);
236 struct object_update *update = tti->tti_u.update.tti_update;
237 struct lu_buf *lbuf = &tti->tti_buf;
238 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
239 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
241 struct object_update_result *update_result;
242 int idx = tti->tti_u.update.tti_update_reply_index;
247 if (!lu_object_exists(&obj->do_lu)) {
248 set_bit(LU_OBJECT_HEARD_BANSHEE,
249 &obj->do_lu.lo_header->loh_flags);
253 name = object_update_param_get(update, 0, NULL);
255 CERROR("%s: empty name for xattr get: rc = %ld\n",
256 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
257 RETURN(PTR_ERR(name));
260 update_result = object_update_result_get(reply, idx, NULL);
261 if (update_result == NULL) {
262 CERROR("%s: empty name for xattr get: rc = %d\n",
263 tgt_name(tsi->tsi_tgt), -EPROTO);
267 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
268 if (lbuf->lb_len == 0)
271 lbuf->lb_buf = update_result->our_data;
273 dt_read_lock(env, obj, DT_TGT_CHILD);
274 rc = dt_xattr_get(env, obj, lbuf, name);
275 dt_read_unlock(env, obj);
280 } else if (lbuf->lb_buf) {
283 CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
284 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
290 object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
294 static int out_xattr_list(struct tgt_session_info *tsi)
296 const struct lu_env *env = tsi->tsi_env;
297 struct tgt_thread_info *tti = tgt_th_info(env);
298 struct lu_buf *lbuf = &tti->tti_buf;
299 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
300 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
301 struct object_update_result *update_result;
302 int idx = tti->tti_u.update.tti_update_reply_index;
307 if (!lu_object_exists(&obj->do_lu)) {
308 set_bit(LU_OBJECT_HEARD_BANSHEE,
309 &obj->do_lu.lo_header->loh_flags);
313 update_result = object_update_result_get(reply, 0, NULL);
314 if (!update_result) {
316 CERROR("%s: empty buf for xattr list: rc = %d\n",
317 tgt_name(tsi->tsi_tgt), rc);
321 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
322 lbuf->lb_buf = update_result->our_data;
323 if (lbuf->lb_len == 0)
326 dt_read_lock(env, obj, DT_TGT_CHILD);
327 rc = dt_xattr_list(env, obj, lbuf);
328 dt_read_unlock(env, obj);
333 } else if (lbuf->lb_buf) {
337 CDEBUG(D_INFO, "%s: "DFID" list xattr len %d\n",
338 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)), rc);
340 /* Since we directly use update_result->our_data as the lbuf->lb_buf,
341 * then use NULL for result_insert to avoid unnecessary memory copy. */
342 object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
347 static int out_index_lookup(struct tgt_session_info *tsi)
349 const struct lu_env *env = tsi->tsi_env;
350 struct tgt_thread_info *tti = tgt_th_info(env);
351 struct object_update *update = tti->tti_u.update.tti_update;
352 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
358 if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
361 if (!lu_object_exists(&obj->do_lu))
364 name = object_update_param_get(update, 0, NULL);
366 CERROR("%s: empty name for lookup: rc = %ld\n",
367 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
368 RETURN(PTR_ERR(name));
371 dt_read_lock(env, obj, DT_TGT_CHILD);
372 if (!dt_try_as_dir(env, obj))
373 GOTO(out_unlock, rc = -ENOTDIR);
375 rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
376 (struct dt_key *)name);
379 GOTO(out_unlock, rc);
385 dt_read_unlock(env, obj);
387 CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
388 PFID(lu_object_fid(&obj->do_lu)), name,
389 PFID(&tti->tti_fid1), rc);
391 CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
392 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
395 object_update_result_insert(tti->tti_u.update.tti_update_reply,
396 &tti->tti_fid1, sizeof(tti->tti_fid1),
397 tti->tti_u.update.tti_update_reply_index, rc);
401 static int out_xattr_set(struct tgt_session_info *tsi)
403 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
404 struct object_update *update = tti->tti_u.update.tti_update;
405 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
406 struct lu_buf *lbuf = &tti->tti_buf;
416 name = object_update_param_get(update, 0, NULL);
418 CERROR("%s: empty name for xattr set: rc = %ld\n",
419 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
420 RETURN(PTR_ERR(name));
423 /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
424 buf = object_update_param_get(update, 1, &buf_len);
425 if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
426 RETURN(PTR_ERR(buf));
429 lbuf->lb_len = buf_len;
431 tmp = object_update_param_get(update, 2, &size);
432 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
433 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
434 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
435 RETURN(PTR_ERR(tmp));
438 if (req_capsule_req_need_swab(tsi->tsi_pill))
442 rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
443 &tti->tti_tea, tti->tti_tea.ta_handle,
444 tti->tti_u.update.tti_update_reply,
445 tti->tti_u.update.tti_update_reply_index);
449 static int out_xattr_del(struct tgt_session_info *tsi)
451 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
452 struct object_update *update = tti->tti_u.update.tti_update;
453 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
458 name = object_update_param_get(update, 0, NULL);
460 CERROR("%s: empty name for xattr set: rc = %ld\n",
461 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
462 RETURN(PTR_ERR(name));
465 rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
466 tti->tti_tea.ta_handle,
467 tti->tti_u.update.tti_update_reply,
468 tti->tti_u.update.tti_update_reply_index);
473 * increase ref of the object
475 static int out_ref_add(struct tgt_session_info *tsi)
477 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
478 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
483 rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
484 tti->tti_tea.ta_handle,
485 tti->tti_u.update.tti_update_reply,
486 tti->tti_u.update.tti_update_reply_index);
490 static int out_ref_del(struct tgt_session_info *tsi)
492 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
493 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
498 if (!lu_object_exists(&obj->do_lu))
501 rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
502 tti->tti_tea.ta_handle,
503 tti->tti_u.update.tti_update_reply,
504 tti->tti_u.update.tti_update_reply_index);
508 static int out_index_insert(struct tgt_session_info *tsi)
510 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
511 struct object_update *update = tti->tti_u.update.tti_update;
512 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
513 struct dt_insert_rec *rec = &tti->tti_rec;
521 name = object_update_param_get(update, 0, NULL);
523 CERROR("%s: empty name for index insert: rc = %ld\n",
524 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
525 RETURN(PTR_ERR(name));
528 fid = object_update_param_get(update, 1, &size);
529 if (IS_ERR(fid) || size != sizeof(*fid)) {
530 CERROR("%s: invalid fid: rc = %ld\n",
531 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
532 RETURN(PTR_ERR(fid));
535 if (req_capsule_req_need_swab(tsi->tsi_pill))
536 lustre_swab_lu_fid(fid);
538 if (!fid_is_sane(fid)) {
539 CERROR("%s: invalid FID "DFID": rc = %d\n",
540 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
544 ptype = object_update_param_get(update, 2, &size);
545 if (IS_ERR(ptype) || size != sizeof(*ptype)) {
546 CERROR("%s: invalid type for index insert: rc = %ld\n",
547 tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
548 RETURN(PTR_ERR(ptype));
551 if (req_capsule_req_need_swab(tsi->tsi_pill))
555 rec->rec_type = *ptype;
557 rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
558 (const struct dt_key *)name, &tti->tti_tea,
559 tti->tti_tea.ta_handle,
560 tti->tti_u.update.tti_update_reply,
561 tti->tti_u.update.tti_update_reply_index);
563 CDEBUG(D_INFO, "%s: "DFID" index insert %s: rc = %d\n",
564 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
570 static int out_index_delete(struct tgt_session_info *tsi)
572 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
573 struct object_update *update = tti->tti_u.update.tti_update;
574 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
578 if (!lu_object_exists(&obj->do_lu))
581 name = object_update_param_get(update, 0, NULL);
583 CERROR("%s: empty name for index delete: rc = %ld\n",
584 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
585 RETURN(PTR_ERR(name));
588 rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
589 &tti->tti_tea, tti->tti_tea.ta_handle,
590 tti->tti_u.update.tti_update_reply,
591 tti->tti_u.update.tti_update_reply_index);
595 static int out_destroy(struct tgt_session_info *tsi)
597 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
598 struct object_update *update = tti->tti_u.update.tti_update;
599 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
604 fid = &update->ou_fid;
605 if (!fid_is_sane(fid)) {
606 CERROR("%s: invalid FID "DFID": rc = %d\n",
607 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
611 if (!lu_object_exists(&obj->do_lu))
614 rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
615 tti->tti_tea.ta_handle,
616 tti->tti_u.update.tti_update_reply,
617 tti->tti_u.update.tti_update_reply_index);
622 static int out_write(struct tgt_session_info *tsi)
624 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
625 struct object_update *update = tti->tti_u.update.tti_update;
626 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
627 struct lu_buf *lbuf = &tti->tti_buf;
636 buf = object_update_param_get(update, 0, &buf_len);
637 if (IS_ERR(buf) || buf_len == 0) {
638 CERROR("%s: empty buf for xattr set: rc = %ld\n",
639 tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
640 RETURN(PTR_ERR(buf));
643 lbuf->lb_len = buf_len;
645 tmp = object_update_param_get(update, 1, &size);
646 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
647 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
648 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
649 RETURN(PTR_ERR(tmp));
652 if (req_capsule_req_need_swab(tsi->tsi_pill))
656 rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
657 &tti->tti_tea, tti->tti_tea.ta_handle,
658 tti->tti_u.update.tti_update_reply,
659 tti->tti_u.update.tti_update_reply_index);
663 static int out_read(struct tgt_session_info *tsi)
665 const struct lu_env *env = tsi->tsi_env;
666 struct tgt_thread_info *tti = tgt_th_info(env);
667 struct object_update *update = tti->tti_u.update.tti_update;
668 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
669 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
670 int index = tti->tti_u.update.tti_update_reply_index;
671 struct lu_rdbuf *rdbuf;
672 struct object_update_result *update_result;
673 struct out_read_reply *orr;
676 size_t total_size = 0;
683 update_result = object_update_result_get(reply, index, NULL);
684 LASSERT(update_result != NULL);
685 update_result->our_datalen = sizeof(*orr);
687 if (!lu_object_exists(&obj->do_lu))
688 GOTO(out, rc = -ENOENT);
690 tmp = object_update_param_get(update, 0, NULL);
692 CERROR("%s: empty size for read: rc = %ld\n",
693 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
694 GOTO(out, rc = PTR_ERR(tmp));
696 size = le64_to_cpu(*(size_t *)(tmp));
698 tmp = object_update_param_get(update, 1, NULL);
700 CERROR("%s: empty pos for read: rc = %ld\n",
701 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
702 GOTO(out, rc = PTR_ERR(tmp));
704 pos = le64_to_cpu(*(__u64 *)(tmp));
706 /* Put the offset into the begining of the buffer in reply */
707 orr = (struct out_read_reply *)update_result->our_data;
709 nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
710 OBD_ALLOC(rdbuf, sizeof(*rdbuf) + nbufs * sizeof(rdbuf->rb_bufs[0]));
712 GOTO(out, rc = -ENOMEM);
716 for (i = 0; i < nbufs; i++) {
719 read_size = size > OUT_BULK_BUFFER_SIZE ?
720 OUT_BULK_BUFFER_SIZE : size;
721 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
722 if (rdbuf->rb_bufs[i].lb_buf == NULL)
723 GOTO(out_free, rc = -ENOMEM);
725 rdbuf->rb_bufs[i].lb_len = read_size;
726 dt_read_lock(env, obj, DT_TGT_CHILD);
727 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
728 dt_read_unlock(env, obj);
730 total_size += rc < 0 ? 0 : rc;
738 /* send pages to client */
739 rc = tgt_send_buffer(tsi, rdbuf);
743 orr->orr_size = total_size;
744 orr->orr_offset = pos;
746 orr_cpu_to_le(orr, orr);
747 update_result->our_datalen += orr->orr_size;
749 for (i = 0; i < nbufs; i++) {
750 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
751 OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
752 rdbuf->rb_bufs[i].lb_len);
755 OBD_FREE(rdbuf, sizeof(*rdbuf) +
756 nbufs * sizeof(rdbuf->rb_bufs[0]));
758 /* Insert read buffer */
759 update_result->our_rc = ptlrpc_status_hton(rc);
760 reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
761 sizeof(*update_result));
765 static int out_noop(struct tgt_session_info *tsi)
770 #define DEF_OUT_HNDL(opc, name, flags, fn) \
771 [opc - OUT_CREATE] = { \
781 static struct tgt_handler out_update_ops[] = {
782 DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
784 DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
786 DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
788 DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
790 DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set", IS_MUTABLE | HAS_REPLY,
792 DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get", HAS_REPLY,
794 DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
796 DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
798 DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
800 DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
802 DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
803 IS_MUTABLE | HAS_REPLY, out_index_insert),
804 DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
805 IS_MUTABLE | HAS_REPLY, out_index_delete),
806 DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
807 DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
808 DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
809 DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
813 static struct tgt_handler *out_handler_find(__u32 opc)
815 struct tgt_handler *h;
818 if (OUT_CREATE <= opc && opc < OUT_LAST) {
819 h = &out_update_ops[opc - OUT_CREATE];
820 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
823 h = NULL; /* unsupported opc */
828 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
829 struct thandle_exec_args *ta, struct obd_export *exp)
832 ta->ta_handle = dt_trans_create(env, dt);
833 if (IS_ERR(ta->ta_handle)) {
836 rc = PTR_ERR(ta->ta_handle);
837 ta->ta_handle = NULL;
838 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
842 if (exp->exp_need_sync)
843 ta->ta_handle->th_sync = 1;
848 static int out_trans_start(const struct lu_env *env,
849 struct thandle_exec_args *ta)
851 return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
854 static int out_trans_stop(const struct lu_env *env,
855 struct thandle_exec_args *ta, int err)
860 ta->ta_handle->th_result = err;
861 rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
862 for (i = 0; i < ta->ta_argno; i++) {
863 if (ta->ta_args[i]->object != NULL) {
864 dt_object_put(env, ta->ta_args[i]->object);
865 ta->ta_args[i]->object = NULL;
868 ta->ta_handle = NULL;
874 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
877 struct tgt_session_info *tsi = tgt_ses_info(env);
883 if (ta->ta_handle == NULL)
886 if (declare_ret != 0 || ta->ta_argno == 0)
887 GOTO(stop, rc = declare_ret);
889 LASSERT(ta->ta_handle->th_dev != NULL);
890 rc = out_trans_start(env, ta);
891 if (unlikely(rc != 0))
894 for (i = 0; i < ta->ta_argno; i++) {
895 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
897 if (unlikely(rc != 0)) {
898 CDEBUG(D_INFO, "error during execution of #%u from"
899 " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
900 ta->ta_args[i]->line, rc);
902 if (ta->ta_args[i]->undo_fn != NULL)
903 ta->ta_args[i]->undo_fn(env,
907 CERROR("%s: undo for %s:%d: rc = %d\n",
908 dt_obd_name(ta->ta_handle->th_dev),
909 ta->ta_args[i]->file,
910 ta->ta_args[i]->line, -ENOTSUPP);
914 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
915 dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
918 /* Only fail for real updates, XXX right now llog updates will be
919 * ignore, whose updates count is usually 1, so failover test
920 * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
921 * be removed after async update patch is landed. */
922 if (ta->ta_argno > 1)
923 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
926 rc1 = out_trans_stop(env, ta, rc);
930 ta->ta_handle = NULL;
937 * Object updates between Targets. Because all the updates has been
938 * dis-assemblied into object updates at sender side, so OUT will
939 * call OSD API directly to execute these updates.
941 * In DNE phase I all of the updates in the request need to be executed
942 * in one transaction, and the transaction has to be synchronously.
944 * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
947 int out_handle(struct tgt_session_info *tsi)
949 const struct lu_env *env = tsi->tsi_env;
950 struct tgt_thread_info *tti = tgt_th_info(env);
951 struct thandle_exec_args *ta = &tti->tti_tea;
952 struct req_capsule *pill = tsi->tsi_pill;
953 struct dt_device *dt = tsi->tsi_tgt->lut_bottom;
954 struct out_update_header *ouh;
955 struct out_update_buffer *oub = NULL;
956 struct object_update *update;
957 struct object_update_reply *reply;
958 struct ptlrpc_bulk_desc *desc = NULL;
960 int current_batchid = -1;
961 __u32 update_buf_count;
963 unsigned int reply_index = 0;
966 int ouh_size, reply_size;
968 bool need_reconstruct;
972 req_capsule_set(pill, &RQF_OUT_UPDATE);
973 ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
976 RETURN(err_serious(-EPROTO));
978 ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
980 RETURN(err_serious(-EPROTO));
982 if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
983 CERROR("%s: invalid update buffer magic %x expect %x: "
984 "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
985 UPDATE_REQUEST_MAGIC, -EPROTO);
986 RETURN(err_serious(-EPROTO));
989 update_buf_count = ouh->ouh_count;
990 if (update_buf_count == 0)
991 RETURN(err_serious(-EPROTO));
993 OBD_ALLOC_PTR_ARRAY(update_bufs, update_buf_count);
994 if (update_bufs == NULL)
995 RETURN(err_serious(-ENOMEM));
997 if (ouh->ouh_inline_length > 0) {
998 update_bufs[0] = ouh->ouh_inline_data;
1000 struct out_update_buffer *tmp;
1003 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
1005 GOTO(out_free, rc = err_serious(-EPROTO));
1007 for (i = 0; i < update_buf_count; i++)
1008 /* First *and* last might be partial pages, hence +1 */
1009 page_count += DIV_ROUND_UP(oub[i].oub_size,
1012 desc = ptlrpc_prep_bulk_exp(pill->rc_req, page_count,
1013 PTLRPC_BULK_OPS_COUNT,
1014 PTLRPC_BULK_GET_SINK,
1016 &ptlrpc_bulk_kiov_nopin_ops);
1018 GOTO(out_free, rc = err_serious(-ENOMEM));
1021 for (i = 0; i < update_buf_count; i++, tmp++) {
1022 if (tmp->oub_size >= OUT_MAXREQSIZE)
1023 GOTO(out_free, rc = err_serious(-EPROTO));
1025 OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1026 if (update_bufs[i] == NULL)
1027 GOTO(out_free, rc = err_serious(-ENOMEM));
1029 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1033 pill->rc_req->rq_bulk_write = 1;
1034 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1036 GOTO(out_free, rc = err_serious(rc));
1038 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1040 GOTO(out_free, rc = err_serious(rc));
1042 /* validate the request and calculate the total update count and
1043 * set it to reply */
1046 for (i = 0; i < update_buf_count; i++) {
1047 struct object_update_request *our;
1050 our = update_bufs[i];
1051 if (req_capsule_req_need_swab(pill))
1052 lustre_swab_object_update_request(our, 0);
1054 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1055 CERROR("%s: invalid update buffer magic %x"
1056 " expect %x: rc = %d\n",
1057 tgt_name(tsi->tsi_tgt), our->ourq_magic,
1058 UPDATE_REQUEST_MAGIC, -EPROTO);
1059 GOTO(out_free, rc = err_serious(-EPROTO));
1061 updates += our->ourq_count;
1063 /* need to calculate reply size */
1064 for (j = 0; j < our->ourq_count; j++) {
1065 update = object_update_request_get(our, j, NULL);
1067 GOTO(out, rc = err_serious(-EPROTO));
1068 if (req_capsule_req_need_swab(pill))
1069 lustre_swab_object_update(update);
1071 if (!fid_is_sane(&update->ou_fid)) {
1072 CERROR("%s: invalid FID "DFID": rc = %d\n",
1073 tgt_name(tsi->tsi_tgt),
1074 PFID(&update->ou_fid), -EPROTO);
1075 GOTO(out, rc = err_serious(-EPROTO));
1078 /* XXX: what ou_result_size can be considered safe? */
1080 reply_size += sizeof(reply->ourp_lens[0]);
1081 reply_size += sizeof(struct object_update_result);
1082 reply_size += update->ou_result_size;
1085 reply_size += sizeof(*reply);
1087 if (unlikely(reply_size > ouh->ouh_reply_size)) {
1088 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1089 tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1090 updates, reply_size);
1091 GOTO(out_free, rc = err_serious(-EPROTO));
1094 req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1095 ouh->ouh_reply_size);
1096 rc = req_capsule_server_pack(pill);
1098 CERROR("%s: Can't pack response: rc = %d\n",
1099 tgt_name(tsi->tsi_tgt), rc);
1100 GOTO(out_free, rc = err_serious(-EPROTO));
1103 /* Prepare the update reply buffer */
1104 reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1106 GOTO(out_free, rc = -EPROTO);
1107 reply->ourp_magic = UPDATE_REPLY_MAGIC;
1108 reply->ourp_count = updates;
1109 tti->tti_u.update.tti_update_reply = reply;
1110 tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1112 need_reconstruct = out_check_resent(pill->rc_req);
1114 /* Walk through updates in the request to execute them */
1115 for (i = 0; i < update_buf_count; i++) {
1116 struct tgt_handler *h;
1117 struct dt_object *dt_obj;
1119 struct object_update_request *our;
1122 our = update_bufs[i];
1123 update_count = our->ourq_count;
1124 for (j = 0; j < update_count; j++) {
1125 struct lu_object_conf conf;
1127 update = object_update_request_get(our, j, NULL);
1128 if (update->ou_type == OUT_CREATE)
1129 conf.loc_flags = LOC_F_NEW;
1133 dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1134 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1136 GOTO(out, rc = PTR_ERR(dt_obj));
1138 if (dt->dd_record_fid_accessed) {
1139 struct lfsck_req_local *lrl = &tti->tti_lrl;
1142 lu_object_fid(&dt_obj->do_lu),
1145 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1148 tti->tti_u.update.tti_dt_object = dt_obj;
1149 tti->tti_u.update.tti_update = update;
1150 tti->tti_u.update.tti_update_reply_index = reply_index;
1152 h = out_handler_find(update->ou_type);
1153 if (unlikely(h == NULL)) {
1154 CERROR("%s: unsupported opc: 0x%x\n",
1155 tgt_name(tsi->tsi_tgt), update->ou_type);
1156 GOTO(next, rc = -ENOTSUPP);
1159 /* Check resend case only for modifying RPC */
1160 if (h->th_flags & IS_MUTABLE) {
1161 /* sanity check for last XID changing */
1162 if (unlikely(!need_reconstruct &&
1163 req_xid_is_last(pill->rc_req))) {
1164 DEBUG_REQ(D_ERROR, pill->rc_req,
1165 "unexpected last XID change");
1166 GOTO(next, rc = -EINVAL);
1169 if (need_reconstruct) {
1170 out_reconstruct(env, dt, dt_obj, reply,
1176 GOTO(next, rc = -EROFS);
1179 /* start transaction for modification RPC only */
1180 if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1181 current_batchid = update->ou_batchid;
1183 if (reply_index == 0)
1184 CFS_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
1186 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1190 if (update->ou_flags & UPDATE_FL_SYNC)
1191 ta->ta_handle->th_sync = 1;
1194 /* Stop the current update transaction, if the update
1195 * has different batchid, or read-only update */
1196 if (((current_batchid != update->ou_batchid) ||
1197 !(h->th_flags & IS_MUTABLE)) &&
1198 ta->ta_handle != NULL) {
1199 rc = out_tx_end(env, ta, rc);
1200 current_batchid = -1;
1204 /* start a new transaction if needed */
1205 if (h->th_flags & IS_MUTABLE) {
1206 rc = out_tx_start(env, dt, ta,
1210 if (update->ou_flags & UPDATE_FL_SYNC)
1211 ta->ta_handle->th_sync = 1;
1212 current_batchid = update->ou_batchid;
1216 rc = h->th_act(tsi);
1219 dt_object_put(env, dt_obj);
1225 if (current_batchid != -1) {
1226 rc1 = out_tx_end(env, ta, rc);
1232 if (update_bufs != NULL) {
1234 for (i = 0; i < update_buf_count; i++, oub++) {
1235 if (update_bufs[i] != NULL)
1236 OBD_FREE_LARGE(update_bufs[i],
1241 OBD_FREE_PTR_ARRAY(update_bufs, update_buf_count);
1245 ptlrpc_free_bulk(desc);
1250 struct tgt_handler tgt_out_handlers[] = {
1251 TGT_UPDATE_HDL(IS_MUTABLE, OUT_UPDATE, out_handle),
1253 EXPORT_SYMBOL(tgt_out_handlers);