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 int out_create(struct tgt_session_info *tsi)
69 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
70 struct object_update *update = tti->tti_u.update.tti_update;
71 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
72 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
73 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
74 struct lu_attr *attr = &tti->tti_attr;
75 struct lu_fid *fid = NULL;
82 wobdo = object_update_param_get(update, 0, &size);
83 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
84 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
85 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
86 RETURN(PTR_ERR(wobdo));
89 if (req_capsule_req_need_swab(tsi->tsi_pill))
90 lustre_swab_obdo(wobdo);
91 lustre_get_wire_obdo(NULL, lobdo, wobdo);
92 la_from_obdo(attr, lobdo, lobdo->o_valid);
94 dof->dof_type = dt_mode_to_dft(attr->la_mode);
95 if (update->ou_params_count > 1) {
96 fid = object_update_param_get(update, 1, &size);
97 if (IS_ERR(fid) || size != sizeof(*fid)) {
98 CERROR("%s: invalid fid: rc = %ld\n",
99 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
100 RETURN(PTR_ERR(fid));
102 if (req_capsule_req_need_swab(tsi->tsi_pill))
103 lustre_swab_lu_fid(fid);
104 if (!fid_is_sane(fid)) {
105 CERROR("%s: invalid fid "DFID": rc = %d\n",
106 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
111 if (lu_object_exists(&obj->do_lu))
114 rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
115 &tti->tti_tea, tti->tti_tea.ta_handle,
116 tti->tti_u.update.tti_update_reply,
117 tti->tti_u.update.tti_update_reply_index);
122 static int out_attr_set(struct tgt_session_info *tsi)
124 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
125 struct object_update *update = tti->tti_u.update.tti_update;
126 struct lu_attr *attr = &tti->tti_attr;
127 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
128 struct obdo *lobdo = &tti->tti_u.update.tti_obdo;
135 wobdo = object_update_param_get(update, 0, &size);
136 if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
137 CERROR("%s: empty obdo in the update: rc = %ld\n",
138 tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
139 RETURN(PTR_ERR(wobdo));
145 if (req_capsule_req_need_swab(tsi->tsi_pill))
146 lustre_swab_obdo(wobdo);
147 lustre_get_wire_obdo(NULL, lobdo, wobdo);
148 la_from_obdo(attr, lobdo, lobdo->o_valid);
150 rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
151 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_get(struct tgt_session_info *tsi)
160 const struct lu_env *env = tsi->tsi_env;
161 struct tgt_thread_info *tti = tgt_th_info(env);
162 struct object_update *update = tti->tti_u.update.tti_update;
163 struct obdo *obdo = &tti->tti_u.update.tti_obdo;
164 struct lu_attr *la = &tti->tti_attr;
165 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
166 int idx = tti->tti_u.update.tti_update_reply_index;
171 if (unlikely(update->ou_result_size < sizeof(*obdo)))
174 if (!lu_object_exists(&obj->do_lu)) {
175 /* Usually, this will be called when the master MDT try
176 * to init a remote object(see osp_object_init), so if
177 * the object does not exist on slave, we need set BANSHEE flag,
178 * so the object can be removed from the cache immediately */
179 set_bit(LU_OBJECT_HEARD_BANSHEE,
180 &obj->do_lu.lo_header->loh_flags);
184 dt_read_lock(env, obj, DT_TGT_CHILD);
185 rc = dt_attr_get(env, obj, la);
187 GOTO(out_unlock, rc);
190 obdo_from_la(obdo, la, la->la_valid);
193 dt_read_unlock(env, obj);
195 CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
196 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
199 object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
200 sizeof(*obdo), idx, rc);
205 static int out_xattr_get(struct tgt_session_info *tsi)
207 const struct lu_env *env = tsi->tsi_env;
208 struct tgt_thread_info *tti = tgt_th_info(env);
209 struct object_update *update = tti->tti_u.update.tti_update;
210 struct lu_buf *lbuf = &tti->tti_buf;
211 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
212 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
214 struct object_update_result *update_result;
215 int idx = tti->tti_u.update.tti_update_reply_index;
220 if (!lu_object_exists(&obj->do_lu)) {
221 set_bit(LU_OBJECT_HEARD_BANSHEE,
222 &obj->do_lu.lo_header->loh_flags);
226 name = object_update_param_get(update, 0, NULL);
228 CERROR("%s: empty name for xattr get: rc = %ld\n",
229 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
230 RETURN(PTR_ERR(name));
233 update_result = object_update_result_get(reply, idx, NULL);
234 if (update_result == NULL) {
235 CERROR("%s: empty name for xattr get: rc = %d\n",
236 tgt_name(tsi->tsi_tgt), -EPROTO);
240 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
241 if (lbuf->lb_len == 0)
244 lbuf->lb_buf = update_result->our_data;
246 dt_read_lock(env, obj, DT_TGT_CHILD);
247 rc = dt_xattr_get(env, obj, lbuf, name);
248 dt_read_unlock(env, obj);
253 } else if (lbuf->lb_buf) {
256 CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
257 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
263 object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
267 static int out_xattr_list(struct tgt_session_info *tsi)
269 const struct lu_env *env = tsi->tsi_env;
270 struct tgt_thread_info *tti = tgt_th_info(env);
271 struct lu_buf *lbuf = &tti->tti_buf;
272 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
273 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
274 struct object_update_result *update_result;
275 int idx = tti->tti_u.update.tti_update_reply_index;
280 if (!lu_object_exists(&obj->do_lu)) {
281 set_bit(LU_OBJECT_HEARD_BANSHEE,
282 &obj->do_lu.lo_header->loh_flags);
286 update_result = object_update_result_get(reply, 0, NULL);
287 if (!update_result) {
289 CERROR("%s: empty buf for xattr list: rc = %d\n",
290 tgt_name(tsi->tsi_tgt), rc);
294 lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
295 lbuf->lb_buf = update_result->our_data;
296 if (lbuf->lb_len == 0)
299 dt_read_lock(env, obj, DT_TGT_CHILD);
300 rc = dt_xattr_list(env, obj, lbuf);
301 dt_read_unlock(env, obj);
306 } else if (lbuf->lb_buf) {
310 CDEBUG(D_INFO, "%s: "DFID" list xattr len %d\n",
311 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)), rc);
313 /* Since we directly use update_result->our_data as the lbuf->lb_buf,
314 * then use NULL for result_insert to avoid unnecessary memory copy. */
315 object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
320 static int out_index_lookup(struct tgt_session_info *tsi)
322 const struct lu_env *env = tsi->tsi_env;
323 struct tgt_thread_info *tti = tgt_th_info(env);
324 struct object_update *update = tti->tti_u.update.tti_update;
325 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
331 if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
334 if (!lu_object_exists(&obj->do_lu))
337 name = object_update_param_get(update, 0, NULL);
339 CERROR("%s: empty name for lookup: rc = %ld\n",
340 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
341 RETURN(PTR_ERR(name));
344 dt_read_lock(env, obj, DT_TGT_CHILD);
345 if (!dt_try_as_dir(env, obj, true))
346 GOTO(out_unlock, rc = -ENOTDIR);
348 rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
349 (struct dt_key *)name);
352 GOTO(out_unlock, rc);
358 dt_read_unlock(env, obj);
360 CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
361 PFID(lu_object_fid(&obj->do_lu)), name,
362 PFID(&tti->tti_fid1), rc);
364 CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
365 tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
368 object_update_result_insert(tti->tti_u.update.tti_update_reply,
369 &tti->tti_fid1, sizeof(tti->tti_fid1),
370 tti->tti_u.update.tti_update_reply_index, rc);
374 static int out_xattr_set(struct tgt_session_info *tsi)
376 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
377 struct object_update *update = tti->tti_u.update.tti_update;
378 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
379 struct lu_buf *lbuf = &tti->tti_buf;
389 name = object_update_param_get(update, 0, NULL);
391 CERROR("%s: empty name for xattr set: rc = %ld\n",
392 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
393 RETURN(PTR_ERR(name));
396 /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
397 buf = object_update_param_get(update, 1, &buf_len);
398 if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
399 RETURN(PTR_ERR(buf));
402 lbuf->lb_len = buf_len;
404 tmp = object_update_param_get(update, 2, &size);
405 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
406 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
407 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
408 RETURN(PTR_ERR(tmp));
411 if (req_capsule_req_need_swab(tsi->tsi_pill))
415 rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
416 &tti->tti_tea, tti->tti_tea.ta_handle,
417 tti->tti_u.update.tti_update_reply,
418 tti->tti_u.update.tti_update_reply_index);
422 static int out_xattr_del(struct tgt_session_info *tsi)
424 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
425 struct object_update *update = tti->tti_u.update.tti_update;
426 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
431 name = object_update_param_get(update, 0, NULL);
433 CERROR("%s: empty name for xattr set: rc = %ld\n",
434 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
435 RETURN(PTR_ERR(name));
438 rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
439 tti->tti_tea.ta_handle,
440 tti->tti_u.update.tti_update_reply,
441 tti->tti_u.update.tti_update_reply_index);
446 * increase ref of the object
448 static int out_ref_add(struct tgt_session_info *tsi)
450 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
451 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
456 rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
457 tti->tti_tea.ta_handle,
458 tti->tti_u.update.tti_update_reply,
459 tti->tti_u.update.tti_update_reply_index);
463 static int out_ref_del(struct tgt_session_info *tsi)
465 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
466 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
471 if (!lu_object_exists(&obj->do_lu))
474 rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
475 tti->tti_tea.ta_handle,
476 tti->tti_u.update.tti_update_reply,
477 tti->tti_u.update.tti_update_reply_index);
481 static int out_index_insert(struct tgt_session_info *tsi)
483 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
484 struct object_update *update = tti->tti_u.update.tti_update;
485 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
486 struct dt_insert_rec *rec = &tti->tti_rec;
494 name = object_update_param_get(update, 0, NULL);
496 CERROR("%s: empty name for index insert: rc = %ld\n",
497 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
498 RETURN(PTR_ERR(name));
501 fid = object_update_param_get(update, 1, &size);
502 if (IS_ERR(fid) || size != sizeof(*fid)) {
503 CERROR("%s: invalid fid: rc = %ld\n",
504 tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
505 RETURN(PTR_ERR(fid));
508 if (req_capsule_req_need_swab(tsi->tsi_pill))
509 lustre_swab_lu_fid(fid);
511 if (!fid_is_sane(fid)) {
512 CERROR("%s: invalid FID "DFID": rc = %d\n",
513 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
517 ptype = object_update_param_get(update, 2, &size);
518 if (IS_ERR(ptype) || size != sizeof(*ptype)) {
519 CERROR("%s: invalid type for index insert: rc = %ld\n",
520 tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
521 RETURN(PTR_ERR(ptype));
524 if (req_capsule_req_need_swab(tsi->tsi_pill))
528 rec->rec_type = *ptype;
530 rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
531 (const struct dt_key *)name, &tti->tti_tea,
532 tti->tti_tea.ta_handle,
533 tti->tti_u.update.tti_update_reply,
534 tti->tti_u.update.tti_update_reply_index);
536 CDEBUG(D_INFO, "%s: "DFID" index insert %s: rc = %d\n",
537 tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
543 static int out_index_delete(struct tgt_session_info *tsi)
545 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
546 struct object_update *update = tti->tti_u.update.tti_update;
547 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
551 if (!lu_object_exists(&obj->do_lu))
554 name = object_update_param_get(update, 0, NULL);
556 CERROR("%s: empty name for index delete: rc = %ld\n",
557 tgt_name(tsi->tsi_tgt), PTR_ERR(name));
558 RETURN(PTR_ERR(name));
561 rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
562 &tti->tti_tea, tti->tti_tea.ta_handle,
563 tti->tti_u.update.tti_update_reply,
564 tti->tti_u.update.tti_update_reply_index);
568 static int out_destroy(struct tgt_session_info *tsi)
570 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
571 struct object_update *update = tti->tti_u.update.tti_update;
572 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
577 fid = &update->ou_fid;
578 if (!fid_is_sane(fid)) {
579 CERROR("%s: invalid FID "DFID": rc = %d\n",
580 tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
584 if (!lu_object_exists(&obj->do_lu))
587 rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
588 tti->tti_tea.ta_handle,
589 tti->tti_u.update.tti_update_reply,
590 tti->tti_u.update.tti_update_reply_index);
595 static int out_write(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;
600 struct lu_buf *lbuf = &tti->tti_buf;
609 buf = object_update_param_get(update, 0, &buf_len);
610 if (IS_ERR(buf) || buf_len == 0) {
611 CERROR("%s: empty buf for xattr set: rc = %ld\n",
612 tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
613 RETURN(PTR_ERR(buf));
616 lbuf->lb_len = buf_len;
618 tmp = object_update_param_get(update, 1, &size);
619 if (IS_ERR(tmp) || size != sizeof(*tmp)) {
620 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
621 tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
622 RETURN(PTR_ERR(tmp));
625 if (req_capsule_req_need_swab(tsi->tsi_pill))
629 rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
630 &tti->tti_tea, tti->tti_tea.ta_handle,
631 tti->tti_u.update.tti_update_reply,
632 tti->tti_u.update.tti_update_reply_index);
636 static int out_read(struct tgt_session_info *tsi)
638 const struct lu_env *env = tsi->tsi_env;
639 struct tgt_thread_info *tti = tgt_th_info(env);
640 struct object_update *update = tti->tti_u.update.tti_update;
641 struct dt_object *obj = tti->tti_u.update.tti_dt_object;
642 struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
643 int index = tti->tti_u.update.tti_update_reply_index;
644 struct lu_rdbuf *rdbuf;
645 struct object_update_result *update_result;
646 struct out_read_reply *orr;
649 size_t total_size = 0;
656 update_result = object_update_result_get(reply, index, NULL);
657 LASSERT(update_result != NULL);
658 update_result->our_datalen = sizeof(*orr);
660 if (!lu_object_exists(&obj->do_lu))
661 GOTO(out, rc = -ENOENT);
663 tmp = object_update_param_get(update, 0, NULL);
665 CERROR("%s: empty size for read: rc = %ld\n",
666 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
667 GOTO(out, rc = PTR_ERR(tmp));
669 size = le64_to_cpu(*(size_t *)(tmp));
671 tmp = object_update_param_get(update, 1, NULL);
673 CERROR("%s: empty pos for read: rc = %ld\n",
674 tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
675 GOTO(out, rc = PTR_ERR(tmp));
677 pos = le64_to_cpu(*(__u64 *)(tmp));
679 /* Put the offset into the begining of the buffer in reply */
680 orr = (struct out_read_reply *)update_result->our_data;
682 nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
683 OBD_ALLOC(rdbuf, sizeof(*rdbuf) + nbufs * sizeof(rdbuf->rb_bufs[0]));
685 GOTO(out, rc = -ENOMEM);
689 for (i = 0; i < nbufs; i++) {
692 read_size = size > OUT_BULK_BUFFER_SIZE ?
693 OUT_BULK_BUFFER_SIZE : size;
694 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
695 if (rdbuf->rb_bufs[i].lb_buf == NULL)
696 GOTO(out_free, rc = -ENOMEM);
698 rdbuf->rb_bufs[i].lb_len = read_size;
699 dt_read_lock(env, obj, DT_TGT_CHILD);
700 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
701 dt_read_unlock(env, obj);
703 total_size += rc < 0 ? 0 : rc;
711 /* send pages to client */
712 rc = tgt_send_buffer(tsi, rdbuf);
716 orr->orr_size = total_size;
717 orr->orr_offset = pos;
719 orr_cpu_to_le(orr, orr);
720 update_result->our_datalen += orr->orr_size;
722 for (i = 0; i < nbufs; i++) {
723 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
724 OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
725 rdbuf->rb_bufs[i].lb_len);
728 OBD_FREE(rdbuf, sizeof(*rdbuf) +
729 nbufs * sizeof(rdbuf->rb_bufs[0]));
731 /* Insert read buffer */
732 update_result->our_rc = ptlrpc_status_hton(rc);
733 reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
734 sizeof(*update_result));
738 static int out_noop(struct tgt_session_info *tsi)
743 #define DEF_OUT_HNDL(opc, name, flags, fn) \
744 [opc - OUT_CREATE] = { \
754 static struct tgt_handler out_update_ops[] = {
755 DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
757 DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
759 DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
761 DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
763 DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set", IS_MUTABLE | HAS_REPLY,
765 DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get", HAS_REPLY,
767 DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
769 DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
771 DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
773 DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
775 DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
776 IS_MUTABLE | HAS_REPLY, out_index_insert),
777 DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
778 IS_MUTABLE | HAS_REPLY, out_index_delete),
779 DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
780 DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
781 DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
782 DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
786 static struct tgt_handler *out_handler_find(__u32 opc)
788 struct tgt_handler *h;
791 if (OUT_CREATE <= opc && opc < OUT_LAST) {
792 h = &out_update_ops[opc - OUT_CREATE];
793 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
796 h = NULL; /* unsupported opc */
801 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
802 struct thandle_exec_args *ta, struct obd_export *exp)
805 ta->ta_handle = dt_trans_create(env, dt);
806 if (IS_ERR(ta->ta_handle)) {
809 rc = PTR_ERR(ta->ta_handle);
810 ta->ta_handle = NULL;
811 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
815 if (exp->exp_need_sync)
816 ta->ta_handle->th_sync = 1;
821 static int out_trans_start(const struct lu_env *env,
822 struct thandle_exec_args *ta)
824 return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
827 static int out_trans_stop(const struct lu_env *env,
828 struct thandle_exec_args *ta, int err)
833 ta->ta_handle->th_result = err;
834 rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
835 for (i = 0; i < ta->ta_argno; i++) {
836 if (ta->ta_args[i]->object != NULL) {
837 dt_object_put(env, ta->ta_args[i]->object);
838 ta->ta_args[i]->object = NULL;
841 ta->ta_handle = NULL;
847 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
850 struct tgt_session_info *tsi = tgt_ses_info(env);
856 if (ta->ta_handle == NULL)
859 if (declare_ret != 0 || ta->ta_argno == 0)
860 GOTO(stop, rc = declare_ret);
862 LASSERT(ta->ta_handle->th_dev != NULL);
863 rc = out_trans_start(env, ta);
864 if (unlikely(rc != 0))
867 for (i = 0; i < ta->ta_argno; i++) {
868 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
870 if (unlikely(rc != 0)) {
871 CDEBUG(D_INFO, "error during execution of #%u from"
872 " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
873 ta->ta_args[i]->line, rc);
875 if (ta->ta_args[i]->undo_fn != NULL)
876 ta->ta_args[i]->undo_fn(env,
880 CERROR("%s: undo for %s:%d: rc = %d\n",
881 dt_obd_name(ta->ta_handle->th_dev),
882 ta->ta_args[i]->file,
883 ta->ta_args[i]->line, -ENOTSUPP);
887 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
888 dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
891 /* Only fail for real updates, XXX right now llog updates will be
892 * ignore, whose updates count is usually 1, so failover test
893 * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
894 * be removed after async update patch is landed. */
895 if (ta->ta_argno > 1)
896 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
899 rc1 = out_trans_stop(env, ta, rc);
903 ta->ta_handle = NULL;
910 * Object updates between Targets. Because all the updates has been
911 * dis-assemblied into object updates at sender side, so OUT will
912 * call OSD API directly to execute these updates.
914 * In DNE phase I all of the updates in the request need to be executed
915 * in one transaction, and the transaction has to be synchronously.
917 * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
920 int out_handle(struct tgt_session_info *tsi)
922 const struct lu_env *env = tsi->tsi_env;
923 struct tgt_thread_info *tti = tgt_th_info(env);
924 struct thandle_exec_args *ta = &tti->tti_tea;
925 struct req_capsule *pill = tsi->tsi_pill;
926 struct dt_device *dt = tsi->tsi_tgt->lut_bottom;
927 struct out_update_header *ouh;
928 struct out_update_buffer *oub = NULL;
929 struct object_update *update;
930 struct object_update_reply *reply;
931 struct ptlrpc_bulk_desc *desc = NULL;
932 struct tg_reply_data *trd = NULL;
934 int current_batchid = -1;
935 __u32 update_buf_count;
937 unsigned int reply_index = 0;
940 int ouh_size, reply_size;
942 bool need_reconstruct;
946 req_capsule_set(pill, &RQF_OUT_UPDATE);
947 ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
950 RETURN(err_serious(-EPROTO));
952 ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
954 RETURN(err_serious(-EPROTO));
956 if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
957 CERROR("%s: invalid update buffer magic %x expect %x: "
958 "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
959 UPDATE_REQUEST_MAGIC, -EPROTO);
960 RETURN(err_serious(-EPROTO));
963 update_buf_count = ouh->ouh_count;
964 if (update_buf_count == 0)
965 RETURN(err_serious(-EPROTO));
967 OBD_ALLOC_PTR_ARRAY(update_bufs, update_buf_count);
968 if (update_bufs == NULL)
969 RETURN(err_serious(-ENOMEM));
971 if (ouh->ouh_inline_length > 0) {
972 update_bufs[0] = ouh->ouh_inline_data;
974 struct out_update_buffer *tmp;
977 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
979 GOTO(out_free, rc = err_serious(-EPROTO));
981 for (i = 0; i < update_buf_count; i++)
982 /* First *and* last might be partial pages, hence +1 */
983 page_count += DIV_ROUND_UP(oub[i].oub_size,
986 desc = ptlrpc_prep_bulk_exp(pill->rc_req, page_count,
987 PTLRPC_BULK_OPS_COUNT,
988 PTLRPC_BULK_GET_SINK,
990 &ptlrpc_bulk_kiov_nopin_ops);
992 GOTO(out_free, rc = err_serious(-ENOMEM));
995 for (i = 0; i < update_buf_count; i++, tmp++) {
996 if (tmp->oub_size >= OUT_MAXREQSIZE)
997 GOTO(out_free, rc = err_serious(-EPROTO));
999 OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1000 if (update_bufs[i] == NULL)
1001 GOTO(out_free, rc = err_serious(-ENOMEM));
1003 desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1007 pill->rc_req->rq_bulk_write = 1;
1008 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1010 GOTO(out_free, rc = err_serious(rc));
1012 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1014 GOTO(out_free, rc = err_serious(rc));
1016 /* validate the request and calculate the total update count and
1017 * set it to reply */
1020 for (i = 0; i < update_buf_count; i++) {
1021 struct object_update_request *our;
1024 our = update_bufs[i];
1025 if (req_capsule_req_need_swab(pill))
1026 lustre_swab_object_update_request(our, 0);
1028 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1029 CERROR("%s: invalid update buffer magic %x"
1030 " expect %x: rc = %d\n",
1031 tgt_name(tsi->tsi_tgt), our->ourq_magic,
1032 UPDATE_REQUEST_MAGIC, -EPROTO);
1033 GOTO(out_free, rc = err_serious(-EPROTO));
1035 updates += our->ourq_count;
1037 /* need to calculate reply size */
1038 for (j = 0; j < our->ourq_count; j++) {
1039 update = object_update_request_get(our, j, NULL);
1041 GOTO(out, rc = err_serious(-EPROTO));
1042 if (req_capsule_req_need_swab(pill))
1043 lustre_swab_object_update(update);
1045 if (!fid_is_sane(&update->ou_fid)) {
1046 CERROR("%s: invalid FID "DFID": rc = %d\n",
1047 tgt_name(tsi->tsi_tgt),
1048 PFID(&update->ou_fid), -EPROTO);
1049 GOTO(out, rc = err_serious(-EPROTO));
1052 /* XXX: what ou_result_size can be considered safe? */
1054 reply_size += sizeof(reply->ourp_lens[0]);
1055 reply_size += sizeof(struct object_update_result);
1056 reply_size += update->ou_result_size;
1059 reply_size += sizeof(*reply);
1061 if (unlikely(reply_size > ouh->ouh_reply_size)) {
1062 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1063 tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1064 updates, reply_size);
1065 GOTO(out_free, rc = err_serious(-EPROTO));
1068 req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1069 ouh->ouh_reply_size);
1070 rc = req_capsule_server_pack(pill);
1072 CERROR("%s: Can't pack response: rc = %d\n",
1073 tgt_name(tsi->tsi_tgt), rc);
1074 GOTO(out_free, rc = err_serious(-EPROTO));
1077 /* Prepare the update reply buffer */
1078 reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1080 GOTO(out_free, rc = -EPROTO);
1081 reply->ourp_magic = UPDATE_REPLY_MAGIC;
1082 reply->ourp_count = updates;
1083 tti->tti_u.update.tti_update_reply = reply;
1084 tsi->tsi_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1088 GOTO(out_free, rc = -ENOMEM);
1090 need_reconstruct = tgt_check_resent(pill->rc_req, trd);
1092 /* Walk through updates in the request to execute them */
1093 for (i = 0; i < update_buf_count; i++) {
1094 struct tgt_handler *h;
1095 struct dt_object *dt_obj;
1097 struct object_update_request *our;
1100 our = update_bufs[i];
1101 update_count = our->ourq_count;
1102 for (j = 0; j < update_count; j++) {
1103 struct lu_object_conf conf;
1105 update = object_update_request_get(our, j, NULL);
1106 if (update->ou_type == OUT_CREATE)
1107 conf.loc_flags = LOC_F_NEW;
1111 dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1112 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1114 GOTO(out, rc = PTR_ERR(dt_obj));
1116 if (dt->dd_record_fid_accessed) {
1117 struct lfsck_req_local *lrl = &tti->tti_lrl;
1120 lu_object_fid(&dt_obj->do_lu),
1123 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1126 tti->tti_u.update.tti_dt_object = dt_obj;
1127 tti->tti_u.update.tti_update = update;
1128 tti->tti_u.update.tti_update_reply_index = reply_index;
1130 h = out_handler_find(update->ou_type);
1131 if (unlikely(h == NULL)) {
1132 CERROR("%s: unsupported opc: 0x%x\n",
1133 tgt_name(tsi->tsi_tgt), update->ou_type);
1134 GOTO(next, rc = -ENOTSUPP);
1137 /* Check resend case only for modifying RPC */
1138 if (h->th_flags & IS_MUTABLE) {
1139 /* sanity check for last XID changing */
1140 if (unlikely(!need_reconstruct &&
1141 req_xid_is_last(pill->rc_req))) {
1142 DEBUG_REQ(D_ERROR, pill->rc_req,
1143 "unexpected last XID change");
1144 GOTO(next, rc = -EINVAL);
1147 if (need_reconstruct) {
1148 out_reconstruct(env, dt, dt_obj, reply,
1154 GOTO(next, rc = -EROFS);
1157 /* start transaction for modification RPC only */
1158 if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1159 current_batchid = update->ou_batchid;
1161 if (reply_index == 0)
1162 CFS_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
1164 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1168 if (update->ou_flags & UPDATE_FL_SYNC)
1169 ta->ta_handle->th_sync = 1;
1172 /* Stop the current update transaction, if the update
1173 * has different batchid, or read-only update */
1174 if (((current_batchid != update->ou_batchid) ||
1175 !(h->th_flags & IS_MUTABLE)) &&
1176 ta->ta_handle != NULL) {
1177 rc = out_tx_end(env, ta, rc);
1178 current_batchid = -1;
1182 /* start a new transaction if needed */
1183 if (h->th_flags & IS_MUTABLE) {
1184 rc = out_tx_start(env, dt, ta,
1188 if (update->ou_flags & UPDATE_FL_SYNC)
1189 ta->ta_handle->th_sync = 1;
1190 current_batchid = update->ou_batchid;
1194 if (OBD_FAIL_CHECK(OBD_FAIL_OUT_EIO))
1197 rc = h->th_act(tsi);
1200 dt_object_put(env, dt_obj);
1206 if (current_batchid != -1) {
1207 rc1 = out_tx_end(env, ta, rc);
1213 if (update_bufs != NULL) {
1215 for (i = 0; i < update_buf_count; i++, oub++) {
1216 if (update_bufs[i] != NULL)
1217 OBD_FREE_LARGE(update_bufs[i],
1222 OBD_FREE_PTR_ARRAY(update_bufs, update_buf_count);
1229 ptlrpc_free_bulk(desc);
1234 struct tgt_handler tgt_out_handlers[] = {
1235 TGT_UPDATE_HDL(IS_MUTABLE, OUT_UPDATE, out_handle),
1237 EXPORT_SYMBOL(tgt_out_handlers);