Whamcloud - gitweb
LU-7414 target: do not share update and rdbuf
[fs/lustre-release.git] / lustre / target / out_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2015, Intel Corporation.
24  *
25  * lustre/target/out_handler.c
26  *
27  * Object update handler between targets.
28  *
29  * Author: di.wang <di.wang@intel.com>
30  */
31
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <obd_class.h>
35 #include <md_object.h>
36 #include "tgt_internal.h"
37 #include <lustre_update.h>
38
39 static void out_reconstruct(const struct lu_env *env, struct dt_device *dt,
40                             struct dt_object *obj,
41                             struct object_update_reply *reply,
42                             int index)
43 {
44         CDEBUG(D_INFO, "%s: fork reply reply %p index %d: rc = %d\n",
45                dt_obd_name(dt), reply, index, 0);
46
47         object_update_result_insert(reply, NULL, 0, index, 0);
48         return;
49 }
50
51 typedef void (*out_reconstruct_t)(const struct lu_env *env,
52                                   struct dt_device *dt,
53                                   struct dt_object *obj,
54                                   struct object_update_reply *reply,
55                                   int index);
56
57 static inline int out_check_resent(const struct lu_env *env,
58                                    struct dt_device *dt,
59                                    struct dt_object *obj,
60                                    struct ptlrpc_request *req,
61                                    out_reconstruct_t reconstruct,
62                                    struct object_update_reply *reply,
63                                    int index)
64 {
65         if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
66                 return 0;
67
68         if (req_xid_is_last(req)) {
69                 struct lsd_client_data *lcd;
70
71                 /* XXX this does not support mulitple transactions yet, i.e.
72                  * only 1 update RPC each time betwee MDTs */
73                 lcd = req->rq_export->exp_target_data.ted_lcd;
74
75                 req->rq_transno = lcd->lcd_last_transno;
76                 req->rq_status = lcd->lcd_last_result;
77                 if (req->rq_status != 0)
78                         req->rq_transno = 0;
79                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
80                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
81
82                 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"status %d",
83                           req->rq_transno, req->rq_status);
84
85                 reconstruct(env, dt, obj, reply, index);
86                 return 1;
87         }
88         DEBUG_REQ(D_HA, req, "no reply for RESENT req (have "LPD64")",
89                  req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
90         return 0;
91 }
92
93 static int out_create(struct tgt_session_info *tsi)
94 {
95         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
96         struct object_update    *update = tti->tti_u.update.tti_update;
97         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
98         struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
99         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
100         struct lu_attr          *attr = &tti->tti_attr;
101         struct lu_fid           *fid = NULL;
102         struct obdo             *wobdo;
103         size_t                  size;
104         int                     rc;
105
106         ENTRY;
107
108         wobdo = object_update_param_get(update, 0, &size);
109         if (wobdo == NULL || IS_ERR(wobdo) || size != sizeof(*wobdo)) {
110                 CERROR("%s: obdo is NULL, invalid RPC: rc = %d\n",
111                        tgt_name(tsi->tsi_tgt), -EPROTO);
112                 RETURN(err_serious(-EPROTO));
113         }
114
115         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
116                 lustre_swab_obdo(wobdo);
117         lustre_get_wire_obdo(NULL, lobdo, wobdo);
118         la_from_obdo(attr, lobdo, lobdo->o_valid);
119
120         dof->dof_type = dt_mode_to_dft(attr->la_mode);
121         if (update->ou_params_count > 1) {
122                 fid = object_update_param_get(update, 1, &size);
123                 if (fid == NULL || IS_ERR(fid) || size != sizeof(*fid)) {
124                         CERROR("%s: invalid fid: rc = %d\n",
125                                tgt_name(tsi->tsi_tgt), -EPROTO);
126                         RETURN(err_serious(-EPROTO));
127                 }
128                 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
129                         lustre_swab_lu_fid(fid);
130                 if (!fid_is_sane(fid)) {
131                         CERROR("%s: invalid fid "DFID": rc = %d\n",
132                                tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
133                         RETURN(err_serious(-EPROTO));
134                 }
135         }
136
137         if (lu_object_exists(&obj->do_lu))
138                 RETURN(-EEXIST);
139
140         rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
141                            &tti->tti_tea, tti->tti_tea.ta_handle,
142                            tti->tti_u.update.tti_update_reply,
143                            tti->tti_u.update.tti_update_reply_index);
144
145         RETURN(rc);
146 }
147
148 static int out_attr_set(struct tgt_session_info *tsi)
149 {
150         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
151         struct object_update    *update = tti->tti_u.update.tti_update;
152         struct lu_attr          *attr = &tti->tti_attr;
153         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
154         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
155         struct obdo             *wobdo;
156         size_t                   size;
157         int                      rc;
158
159         ENTRY;
160
161         wobdo = object_update_param_get(update, 0, &size);
162         if (wobdo == NULL || IS_ERR(wobdo) || size != sizeof(*wobdo)) {
163                 CERROR("%s: empty obdo in the update: rc = %d\n",
164                        tgt_name(tsi->tsi_tgt), -EPROTO);
165                 RETURN(err_serious(-EPROTO));
166         }
167
168         attr->la_valid = 0;
169         attr->la_valid = 0;
170
171         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
172                 lustre_swab_obdo(wobdo);
173         lustre_get_wire_obdo(NULL, lobdo, wobdo);
174         la_from_obdo(attr, lobdo, lobdo->o_valid);
175
176         rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
177                              tti->tti_tea.ta_handle,
178                              tti->tti_u.update.tti_update_reply,
179                              tti->tti_u.update.tti_update_reply_index);
180
181         RETURN(rc);
182 }
183
184 static int out_attr_get(struct tgt_session_info *tsi)
185 {
186         const struct lu_env     *env = tsi->tsi_env;
187         struct tgt_thread_info  *tti = tgt_th_info(env);
188         struct obdo             *obdo = &tti->tti_u.update.tti_obdo;
189         struct lu_attr          *la = &tti->tti_attr;
190         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
191         int                     idx = tti->tti_u.update.tti_update_reply_index;
192         int                     rc;
193
194         ENTRY;
195
196         if (!lu_object_exists(&obj->do_lu)) {
197                 /* Usually, this will be called when the master MDT try
198                  * to init a remote object(see osp_object_init), so if
199                  * the object does not exist on slave, we need set BANSHEE flag,
200                  * so the object can be removed from the cache immediately */
201                 set_bit(LU_OBJECT_HEARD_BANSHEE,
202                         &obj->do_lu.lo_header->loh_flags);
203                 RETURN(-ENOENT);
204         }
205
206         dt_read_lock(env, obj, MOR_TGT_CHILD);
207         rc = dt_attr_get(env, obj, la);
208         if (rc)
209                 GOTO(out_unlock, rc);
210
211         obdo->o_valid = 0;
212         obdo_from_la(obdo, la, la->la_valid);
213         lustre_set_wire_obdo(NULL, obdo, obdo);
214
215 out_unlock:
216         dt_read_unlock(env, obj);
217
218         CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
219                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
220                0, rc);
221
222         object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
223                                     sizeof(*obdo), idx, rc);
224
225         RETURN(rc);
226 }
227
228 static int out_xattr_get(struct tgt_session_info *tsi)
229 {
230         const struct lu_env        *env = tsi->tsi_env;
231         struct tgt_thread_info     *tti = tgt_th_info(env);
232         struct object_update       *update = tti->tti_u.update.tti_update;
233         struct lu_buf              *lbuf = &tti->tti_buf;
234         struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
235         struct dt_object           *obj = tti->tti_u.update.tti_dt_object;
236         char                       *name;
237         struct object_update_result *update_result;
238         int                     idx = tti->tti_u.update.tti_update_reply_index;
239         int                        rc;
240
241         ENTRY;
242
243         if (!lu_object_exists(&obj->do_lu)) {
244                 set_bit(LU_OBJECT_HEARD_BANSHEE,
245                         &obj->do_lu.lo_header->loh_flags);
246                 RETURN(-ENOENT);
247         }
248
249         name = object_update_param_get(update, 0, NULL);
250         if (name == NULL || IS_ERR(name)) {
251                 CERROR("%s: empty name for xattr get: rc = %d\n",
252                        tgt_name(tsi->tsi_tgt), -EPROTO);
253                 RETURN(err_serious(-EPROTO));
254         }
255
256         update_result = object_update_result_get(reply, 0, NULL);
257         if (update_result == NULL) {
258                 CERROR("%s: empty name for xattr get: rc = %d\n",
259                        tgt_name(tsi->tsi_tgt), -EPROTO);
260                 RETURN(err_serious(-EPROTO));
261         }
262
263         lbuf->lb_buf = update_result->our_data;
264         lbuf->lb_len = OUT_UPDATE_REPLY_SIZE -
265                        cfs_size_round((unsigned long)update_result->our_data -
266                                       (unsigned long)update_result);
267         dt_read_lock(env, obj, MOR_TGT_CHILD);
268         rc = dt_xattr_get(env, obj, lbuf, name);
269         dt_read_unlock(env, obj);
270         if (rc < 0) {
271                 lbuf->lb_len = 0;
272                 GOTO(out, rc);
273         }
274         lbuf->lb_len = rc;
275         rc = 0;
276         CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
277                tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
278                name, (int)lbuf->lb_len);
279
280         GOTO(out, rc);
281
282 out:
283         object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
284         RETURN(rc);
285 }
286
287 static int out_index_lookup(struct tgt_session_info *tsi)
288 {
289         const struct lu_env     *env = tsi->tsi_env;
290         struct tgt_thread_info  *tti = tgt_th_info(env);
291         struct object_update    *update = tti->tti_u.update.tti_update;
292         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
293         char                    *name;
294         int                      rc;
295
296         ENTRY;
297
298         if (!lu_object_exists(&obj->do_lu))
299                 RETURN(-ENOENT);
300
301         name = object_update_param_get(update, 0, NULL);
302         if (name == NULL || IS_ERR(name)) {
303                 CERROR("%s: empty name for lookup: rc = %d\n",
304                        tgt_name(tsi->tsi_tgt), -EPROTO);
305                 RETURN(err_serious(-EPROTO));
306         }
307
308         dt_read_lock(env, obj, MOR_TGT_CHILD);
309         if (!dt_try_as_dir(env, obj))
310                 GOTO(out_unlock, rc = -ENOTDIR);
311
312         rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
313                        (struct dt_key *)name);
314
315         if (rc < 0)
316                 GOTO(out_unlock, rc);
317
318         if (rc == 0)
319                 rc += 1;
320
321 out_unlock:
322         dt_read_unlock(env, obj);
323
324         CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
325                PFID(lu_object_fid(&obj->do_lu)), name,
326                PFID(&tti->tti_fid1), rc);
327
328         CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
329                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
330                0, rc);
331
332         object_update_result_insert(tti->tti_u.update.tti_update_reply,
333                             &tti->tti_fid1, sizeof(tti->tti_fid1),
334                             tti->tti_u.update.tti_update_reply_index, rc);
335         RETURN(rc);
336 }
337
338 static int out_xattr_set(struct tgt_session_info *tsi)
339 {
340         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
341         struct object_update    *update = tti->tti_u.update.tti_update;
342         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
343         struct lu_buf           *lbuf = &tti->tti_buf;
344         char                    *name;
345         char                    *buf;
346         __u32                   *tmp;
347         size_t                   buf_len = 0;
348         int                      flag;
349         size_t                   size = 0;
350         int                      rc;
351         ENTRY;
352
353         name = object_update_param_get(update, 0, NULL);
354         if (name == NULL || IS_ERR(name)) {
355                 CERROR("%s: empty name for xattr set: rc = %d\n",
356                        tgt_name(tsi->tsi_tgt), -EPROTO);
357                 RETURN(err_serious(-EPROTO));
358         }
359
360         buf = object_update_param_get(update, 1, &buf_len);
361         if (IS_ERR(buf))
362                 RETURN(err_serious(-EPROTO));
363
364         lbuf->lb_buf = buf;
365         lbuf->lb_len = buf_len;
366
367         tmp = object_update_param_get(update, 2, &size);
368         if (tmp == NULL || IS_ERR(tmp) || size != sizeof(*tmp)) {
369                 CERROR("%s: emptry or wrong size %zu flag: rc = %d\n",
370                        tgt_name(tsi->tsi_tgt), size, -EPROTO);
371                 RETURN(err_serious(-EPROTO));
372         }
373
374         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
375                 __swab32s(tmp);
376         flag = *tmp;
377
378         rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
379                               &tti->tti_tea, tti->tti_tea.ta_handle,
380                               tti->tti_u.update.tti_update_reply,
381                               tti->tti_u.update.tti_update_reply_index);
382         RETURN(rc);
383 }
384
385 static int out_xattr_del(struct tgt_session_info *tsi)
386 {
387         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
388         struct object_update    *update = tti->tti_u.update.tti_update;
389         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
390         char                    *name;
391         int                      rc;
392         ENTRY;
393
394         name = object_update_param_get(update, 0, NULL);
395         if (name == NULL || IS_ERR(name)) {
396                 CERROR("%s: empty name for xattr set: rc = %d\n",
397                        tgt_name(tsi->tsi_tgt), -EPROTO);
398                 RETURN(err_serious(-EPROTO));
399         }
400
401         rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
402                               tti->tti_tea.ta_handle,
403                               tti->tti_u.update.tti_update_reply,
404                               tti->tti_u.update.tti_update_reply_index);
405         RETURN(rc);
406 }
407
408 /**
409  * increase ref of the object
410  **/
411 static int out_ref_add(struct tgt_session_info *tsi)
412 {
413         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
414         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
415         int                      rc;
416
417         ENTRY;
418
419         rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
420                             tti->tti_tea.ta_handle,
421                             tti->tti_u.update.tti_update_reply,
422                             tti->tti_u.update.tti_update_reply_index);
423         RETURN(rc);
424 }
425
426 static int out_ref_del(struct tgt_session_info *tsi)
427 {
428         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
429         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
430         int                      rc;
431
432         ENTRY;
433
434         if (!lu_object_exists(&obj->do_lu))
435                 RETURN(-ENOENT);
436
437         rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
438                             tti->tti_tea.ta_handle,
439                             tti->tti_u.update.tti_update_reply,
440                             tti->tti_u.update.tti_update_reply_index);
441         RETURN(rc);
442 }
443
444 static int out_index_insert(struct tgt_session_info *tsi)
445 {
446         struct tgt_thread_info  *tti    = tgt_th_info(tsi->tsi_env);
447         struct object_update    *update = tti->tti_u.update.tti_update;
448         struct dt_object        *obj    = tti->tti_u.update.tti_dt_object;
449         struct dt_insert_rec    *rec    = &tti->tti_rec;
450         struct lu_fid           *fid;
451         char                    *name;
452         __u32                   *ptype;
453         int                      rc     = 0;
454         size_t                   size;
455         ENTRY;
456
457         name = object_update_param_get(update, 0, NULL);
458         if (name == NULL || IS_ERR(name)) {
459                 CERROR("%s: empty name for index insert: rc = %d\n",
460                        tgt_name(tsi->tsi_tgt), -EPROTO);
461                 RETURN(err_serious(-EPROTO));
462         }
463
464         fid = object_update_param_get(update, 1, &size);
465         if (fid == NULL || IS_ERR(fid) || size != sizeof(*fid)) {
466                 CERROR("%s: invalid fid: rc = %d\n",
467                        tgt_name(tsi->tsi_tgt), -EPROTO);
468                 RETURN(err_serious(-EPROTO));
469         }
470
471         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
472                 lustre_swab_lu_fid(fid);
473
474         if (!fid_is_sane(fid)) {
475                 CERROR("%s: invalid FID "DFID": rc = %d\n",
476                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
477                 RETURN(err_serious(-EPROTO));
478         }
479
480         ptype = object_update_param_get(update, 2, &size);
481         if (ptype == NULL || IS_ERR(ptype) || size != sizeof(*ptype)) {
482                 CERROR("%s: invalid type for index insert: rc = %d\n",
483                        tgt_name(tsi->tsi_tgt), -EPROTO);
484                 RETURN(err_serious(-EPROTO));
485         }
486
487         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
488                 __swab32s(ptype);
489
490         rec->rec_fid = fid;
491         rec->rec_type = *ptype;
492
493         rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
494                                  (const struct dt_key *)name, &tti->tti_tea,
495                                  tti->tti_tea.ta_handle,
496                                  tti->tti_u.update.tti_update_reply,
497                                  tti->tti_u.update.tti_update_reply_index);
498         RETURN(rc);
499 }
500
501 static int out_index_delete(struct tgt_session_info *tsi)
502 {
503         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
504         struct object_update    *update = tti->tti_u.update.tti_update;
505         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
506         char                    *name;
507         int                      rc = 0;
508
509         if (!lu_object_exists(&obj->do_lu))
510                 RETURN(-ENOENT);
511
512         name = object_update_param_get(update, 0, NULL);
513         if (name == NULL || IS_ERR(name)) {
514                 CERROR("%s: empty name for index delete: rc = %d\n",
515                        tgt_name(tsi->tsi_tgt), -EPROTO);
516                 RETURN(err_serious(-EPROTO));
517         }
518
519         rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
520                                  &tti->tti_tea, tti->tti_tea.ta_handle,
521                                  tti->tti_u.update.tti_update_reply,
522                                  tti->tti_u.update.tti_update_reply_index);
523         RETURN(rc);
524 }
525
526 static int out_destroy(struct tgt_session_info *tsi)
527 {
528         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
529         struct object_update    *update = tti->tti_u.update.tti_update;
530         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
531         struct lu_fid           *fid;
532         int                      rc;
533         ENTRY;
534
535         fid = &update->ou_fid;
536         if (!fid_is_sane(fid)) {
537                 CERROR("%s: invalid FID "DFID": rc = %d\n",
538                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
539                 RETURN(err_serious(-EPROTO));
540         }
541
542         if (!lu_object_exists(&obj->do_lu))
543                 RETURN(-ENOENT);
544
545         rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
546                             tti->tti_tea.ta_handle,
547                             tti->tti_u.update.tti_update_reply,
548                             tti->tti_u.update.tti_update_reply_index);
549
550         RETURN(rc);
551 }
552
553 static int out_write(struct tgt_session_info *tsi)
554 {
555         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
556         struct object_update    *update = tti->tti_u.update.tti_update;
557         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
558         struct lu_buf           *lbuf = &tti->tti_buf;
559         char                    *buf;
560         __u64                   *tmp;
561         size_t                  size = 0;
562         size_t                  buf_len = 0;
563         loff_t                  pos;
564         int                      rc;
565         ENTRY;
566
567         buf = object_update_param_get(update, 0, &buf_len);
568         if (buf == NULL || IS_ERR(buf) || buf_len == 0) {
569                 CERROR("%s: empty buf for xattr set: rc = %d\n",
570                        tgt_name(tsi->tsi_tgt), -EPROTO);
571                 RETURN(err_serious(-EPROTO));
572         }
573         lbuf->lb_buf = buf;
574         lbuf->lb_len = buf_len;
575
576         tmp = object_update_param_get(update, 1, &size);
577         if (tmp == NULL || IS_ERR(tmp) || size != sizeof(*tmp)) {
578                 CERROR("%s: empty or wrong size %zu pos: rc = %d\n",
579                        tgt_name(tsi->tsi_tgt), size, -EPROTO);
580                 RETURN(err_serious(-EPROTO));
581         }
582
583         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
584                 __swab64s(tmp);
585         pos = *tmp;
586
587         rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
588                           &tti->tti_tea, tti->tti_tea.ta_handle,
589                           tti->tti_u.update.tti_update_reply,
590                           tti->tti_u.update.tti_update_reply_index);
591         RETURN(rc);
592 }
593
594 static int out_read(struct tgt_session_info *tsi)
595 {
596         const struct lu_env     *env = tsi->tsi_env;
597         struct tgt_thread_info  *tti = tgt_th_info(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 object_update_reply *reply = tti->tti_u.update.tti_update_reply;
601         int index = tti->tti_u.update.tti_update_reply_index;
602         struct lu_rdbuf *rdbuf;
603         struct object_update_result *update_result;
604         struct out_read_reply   *orr;
605         void *tmp;
606         size_t size;
607         size_t total_size = 0;
608         __u64 pos;
609         unsigned int i;
610         unsigned int nbufs;
611         int rc = 0;
612         ENTRY;
613
614         update_result = object_update_result_get(reply, index, NULL);
615         LASSERT(update_result != NULL);
616         update_result->our_datalen = sizeof(*orr);
617
618         if (!lu_object_exists(&obj->do_lu))
619                 GOTO(out, rc = -ENOENT);
620
621         tmp = object_update_param_get(update, 0, NULL);
622         if (tmp == NULL || IS_ERR(tmp)) {
623                 CERROR("%s: empty size for read: rc = %d\n",
624                        tgt_name(tsi->tsi_tgt), -EPROTO);
625                 GOTO(out, rc = err_serious(-EPROTO));
626         }
627         size = le64_to_cpu(*(size_t *)(tmp));
628
629         tmp = object_update_param_get(update, 1, NULL);
630         if (tmp == NULL || IS_ERR(tmp)) {
631                 CERROR("%s: empty pos for read: rc = %d\n",
632                        tgt_name(tsi->tsi_tgt), -EPROTO);
633                 GOTO(out, rc = err_serious(-EPROTO));
634         }
635         pos = le64_to_cpu(*(__u64 *)(tmp));
636
637         /* Put the offset into the begining of the buffer in reply */
638         orr = (struct out_read_reply *)update_result->our_data;
639
640         nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
641         OBD_ALLOC(rdbuf, sizeof(struct lu_rdbuf) +
642                          nbufs * sizeof(rdbuf->rb_bufs[0]));
643         if (rdbuf == NULL)
644                 GOTO(out, rc = -ENOMEM);
645
646         rdbuf->rb_nbufs = 0;
647         total_size = 0;
648         for (i = 0; i < nbufs; i++) {
649                 __u32 read_size;
650
651                 read_size = size > OUT_BULK_BUFFER_SIZE ?
652                             OUT_BULK_BUFFER_SIZE : size;
653                 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
654                 if (rdbuf->rb_bufs[i].lb_buf == NULL)
655                         GOTO(out_free, rc = -ENOMEM);
656
657                 rdbuf->rb_bufs[i].lb_len = read_size;
658                 dt_read_lock(env, obj, MOR_TGT_CHILD);
659                 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
660                 dt_read_unlock(env, obj);
661
662                 total_size += rc < 0 ? 0 : rc;
663                 if (rc <= 0)
664                         break;
665
666                 rdbuf->rb_nbufs++;
667                 size -= read_size;
668         }
669
670         /* send pages to client */
671         rc = tgt_send_buffer(tsi, rdbuf);
672         if (rc < 0)
673                 GOTO(out_free, rc);
674
675         orr->orr_size = total_size;
676         orr->orr_offset = pos;
677
678         orr_cpu_to_le(orr, orr);
679         update_result->our_datalen += orr->orr_size;
680 out_free:
681         for (i = 0; i < nbufs; i++) {
682                 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
683                         OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
684                                  rdbuf->rb_bufs[i].lb_len);
685                 }
686         }
687         OBD_FREE(rdbuf, sizeof(struct lu_rdbuf) +
688                         nbufs * sizeof(rdbuf->rb_bufs[0]));
689 out:
690         /* Insert read buffer */
691         update_result->our_rc = ptlrpc_status_hton(rc);
692         reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
693                                                  sizeof(*update_result));
694         RETURN(rc);
695 }
696
697 static int out_noop(struct tgt_session_info *tsi)
698 {
699         return 0;
700 }
701
702 #define DEF_OUT_HNDL(opc, name, flags, fn)     \
703 [opc - OUT_CREATE] = {                                  \
704         .th_name    = name,                             \
705         .th_fail_id = 0,                                \
706         .th_opc     = opc,                              \
707         .th_flags   = flags,                            \
708         .th_act     = fn,                               \
709         .th_fmt     = NULL,                             \
710         .th_version = 0,                                \
711 }
712
713 static struct tgt_handler out_update_ops[] = {
714         DEF_OUT_HNDL(OUT_CREATE, "out_create", MUTABOR | HABEO_REFERO,
715                      out_create),
716         DEF_OUT_HNDL(OUT_DESTROY, "out_create", MUTABOR | HABEO_REFERO,
717                      out_destroy),
718         DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", MUTABOR | HABEO_REFERO,
719                      out_ref_add),
720         DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", MUTABOR | HABEO_REFERO,
721                      out_ref_del),
722         DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set",  MUTABOR | HABEO_REFERO,
723                      out_attr_set),
724         DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get",  HABEO_REFERO,
725                      out_attr_get),
726         DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", MUTABOR | HABEO_REFERO,
727                      out_xattr_set),
728         DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", MUTABOR | HABEO_REFERO,
729                      out_xattr_del),
730         DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HABEO_REFERO,
731                      out_xattr_get),
732         DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HABEO_REFERO,
733                      out_index_lookup),
734         DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
735                      MUTABOR | HABEO_REFERO, out_index_insert),
736         DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
737                      MUTABOR | HABEO_REFERO, out_index_delete),
738         DEF_OUT_HNDL(OUT_WRITE, "out_write", MUTABOR | HABEO_REFERO, out_write),
739         DEF_OUT_HNDL(OUT_READ, "out_read", HABEO_REFERO, out_read),
740         DEF_OUT_HNDL(OUT_NOOP, "out_noop", HABEO_REFERO, out_noop),
741 };
742
743 static struct tgt_handler *out_handler_find(__u32 opc)
744 {
745         struct tgt_handler *h;
746
747         h = NULL;
748         if (OUT_CREATE <= opc && opc < OUT_LAST) {
749                 h = &out_update_ops[opc - OUT_CREATE];
750                 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
751                          h->th_opc, opc);
752         } else {
753                 h = NULL; /* unsupported opc */
754         }
755         return h;
756 }
757
758 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
759                         struct thandle_exec_args *ta, struct obd_export *exp)
760 {
761         ta->ta_argno = 0;
762         ta->ta_handle = dt_trans_create(env, dt);
763         if (IS_ERR(ta->ta_handle)) {
764                 int rc;
765
766                 rc = PTR_ERR(ta->ta_handle);
767                 ta->ta_handle = NULL;
768                 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
769                        rc);
770                 return rc;
771         }
772         if (exp->exp_need_sync)
773                 ta->ta_handle->th_sync = 1;
774
775         return 0;
776 }
777
778 static int out_trans_start(const struct lu_env *env,
779                            struct thandle_exec_args *ta)
780 {
781         return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
782 }
783
784 static int out_trans_stop(const struct lu_env *env,
785                           struct thandle_exec_args *ta, int err)
786 {
787         int i;
788         int rc;
789
790         ta->ta_handle->th_result = err;
791         rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
792         for (i = 0; i < ta->ta_argno; i++) {
793                 if (ta->ta_args[i]->object != NULL) {
794                         struct dt_object *obj = ta->ta_args[i]->object;
795
796                         /* If the object is being created during this
797                          * transaction, we need to remove them from the
798                          * cache immediately, because a few layers are
799                          * missing in OUT handler, i.e. the object might
800                          * not be initialized in all layers */
801                         if (ta->ta_args[i]->exec_fn == out_tx_create_exec)
802                                 set_bit(LU_OBJECT_HEARD_BANSHEE,
803                                         &obj->do_lu.lo_header->loh_flags);
804                         lu_object_put(env, &ta->ta_args[i]->object->do_lu);
805                         ta->ta_args[i]->object = NULL;
806                 }
807         }
808         ta->ta_handle = NULL;
809         ta->ta_argno = 0;
810
811         return rc;
812 }
813
814 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
815                       int declare_ret)
816 {
817         struct tgt_session_info *tsi = tgt_ses_info(env);
818         int                     i;
819         int                     rc;
820         int                     rc1;
821         ENTRY;
822
823         if (ta->ta_handle == NULL)
824                 RETURN(0);
825
826         if (declare_ret != 0 || ta->ta_argno == 0)
827                 GOTO(stop, rc = declare_ret);
828
829         LASSERT(ta->ta_handle->th_dev != NULL);
830         rc = out_trans_start(env, ta);
831         if (unlikely(rc != 0))
832                 GOTO(stop, rc);
833
834         for (i = 0; i < ta->ta_argno; i++) {
835                 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
836                                              ta->ta_args[i]);
837                 if (unlikely(rc != 0)) {
838                         CDEBUG(D_INFO, "error during execution of #%u from"
839                                " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
840                                ta->ta_args[i]->line, rc);
841                         while (--i >= 0) {
842                                 if (ta->ta_args[i]->undo_fn != NULL)
843                                         ta->ta_args[i]->undo_fn(env,
844                                                                ta->ta_handle,
845                                                                ta->ta_args[i]);
846                                 else
847                                         CERROR("%s: undo for %s:%d: rc = %d\n",
848                                              dt_obd_name(ta->ta_handle->th_dev),
849                                                ta->ta_args[i]->file,
850                                                ta->ta_args[i]->line, -ENOTSUPP);
851                         }
852                         break;
853                 }
854                 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
855                        dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
856         }
857
858         /* Only fail for real updates, XXX right now llog updates will be
859         * ignore, whose updates count is usually 1, so failover test
860         * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
861         * be removed after async update patch is landed. */
862         if (ta->ta_argno > 1)
863                 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
864
865 stop:
866         rc1 = out_trans_stop(env, ta, rc);
867         if (rc == 0)
868                 rc = rc1;
869
870         ta->ta_handle = NULL;
871         ta->ta_argno = 0;
872
873         RETURN(rc);
874 }
875
876 /**
877  * Object updates between Targets. Because all the updates has been
878  * dis-assemblied into object updates at sender side, so OUT will
879  * call OSD API directly to execute these updates.
880  *
881  * In DNE phase I all of the updates in the request need to be executed
882  * in one transaction, and the transaction has to be synchronously.
883  *
884  * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
885  * format.
886  */
887 int out_handle(struct tgt_session_info *tsi)
888 {
889         const struct lu_env             *env = tsi->tsi_env;
890         struct tgt_thread_info          *tti = tgt_th_info(env);
891         struct thandle_exec_args        *ta = &tti->tti_tea;
892         struct req_capsule              *pill = tsi->tsi_pill;
893         struct dt_device                *dt = tsi->tsi_tgt->lut_bottom;
894         struct out_update_header        *ouh;
895         struct out_update_buffer        *oub = NULL;
896         struct object_update            *update;
897         struct object_update_reply      *reply;
898         struct ptlrpc_bulk_desc         *desc = NULL;
899         struct l_wait_info              lwi;
900         void                            **update_bufs;
901         int                             current_batchid = -1;
902         __u32                           update_buf_count;
903         unsigned int                    i;
904         unsigned int                    reply_index = 0;
905         int                             rc = 0;
906         int                             rc1 = 0;
907         int                             ouh_size;
908         ENTRY;
909
910         req_capsule_set(pill, &RQF_OUT_UPDATE);
911         ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
912                                         RCL_CLIENT);
913         if (ouh_size <= 0)
914                 RETURN(err_serious(-EPROTO));
915
916         ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
917         if (ouh == NULL)
918                 RETURN(err_serious(-EPROTO));
919
920         if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
921                 CERROR("%s: invalid update buffer magic %x expect %x: "
922                        "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
923                        UPDATE_REQUEST_MAGIC, -EPROTO);
924                 RETURN(err_serious(-EPROTO));
925         }
926
927         update_buf_count = ouh->ouh_count;
928         if (update_buf_count == 0)
929                 RETURN(err_serious(-EPROTO));
930
931         req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
932                              OUT_UPDATE_REPLY_SIZE);
933         rc = req_capsule_server_pack(pill);
934         if (rc != 0) {
935                 CERROR("%s: Can't pack response: rc = %d\n",
936                        tgt_name(tsi->tsi_tgt), rc);
937                 RETURN(rc);
938         }
939
940         OBD_ALLOC(update_bufs, sizeof(*update_bufs) * update_buf_count);
941         if (update_bufs == NULL)
942                 RETURN(-ENOMEM);
943
944         if (ouh->ouh_inline_length > 0) {
945                 update_bufs[0] = ouh->ouh_inline_data;
946         } else {
947                 struct out_update_buffer *tmp;
948
949                 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
950                 if (oub == NULL)
951                         GOTO(out_free, rc = -EPROTO);
952
953                 desc = ptlrpc_prep_bulk_exp(pill->rc_req, update_buf_count,
954                                             PTLRPC_BULK_OPS_COUNT,
955                                             PTLRPC_BULK_GET_SINK |
956                                             PTLRPC_BULK_BUF_KVEC,
957                                             MDS_BULK_PORTAL,
958                                             &ptlrpc_bulk_kvec_ops);
959                 if (desc == NULL)
960                         GOTO(out_free, rc = -ENOMEM);
961
962                 tmp = oub;
963                 for (i = 0; i < update_buf_count; i++, tmp++) {
964                         if (tmp->oub_size >= OUT_MAXREQSIZE)
965                                 GOTO(out_free, rc = -EPROTO);
966
967                         OBD_ALLOC(update_bufs[i], tmp->oub_size);
968                         if (update_bufs[i] == NULL)
969                                 GOTO(out_free, rc = -ENOMEM);
970
971                         desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
972                                                         tmp->oub_size);
973                 }
974
975                 pill->rc_req->rq_bulk_write = 1;
976                 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
977                 if (rc != 0)
978                         GOTO(out_free, rc);
979
980                 rc = target_bulk_io(pill->rc_req->rq_export, desc, &lwi);
981                 if (rc < 0)
982                         GOTO(out_free, rc);
983         }
984         /* Prepare the update reply buffer */
985         reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
986         if (reply == NULL)
987                 GOTO(out_free, rc = err_serious(-EPROTO));
988         reply->ourp_magic = UPDATE_REPLY_MAGIC;
989         tti->tti_u.update.tti_update_reply = reply;
990         tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
991
992         /* validate the request and calculate the total update count and
993          * set it to reply */
994         for (i = 0; i < update_buf_count; i++) {
995                 struct object_update_request *our;
996                 int                     update_count;
997
998                 our = update_bufs[i];
999                 if (ptlrpc_req_need_swab(pill->rc_req))
1000                         lustre_swab_object_update_request(our);
1001
1002                 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1003                         CERROR("%s: invalid update buffer magic %x"
1004                                " expect %x: rc = %d\n",
1005                                tgt_name(tsi->tsi_tgt), our->ourq_magic,
1006                                UPDATE_REQUEST_MAGIC, -EPROTO);
1007                         GOTO(out_free, rc = -EPROTO);
1008                 }
1009                 update_count = our->ourq_count;
1010                 reply->ourp_count += update_count;
1011         }
1012  
1013         /* Walk through updates in the request to execute them */
1014         for (i = 0; i < update_buf_count; i++) {
1015                 struct tgt_handler      *h;
1016                 struct dt_object        *dt_obj;
1017                 int                     update_count;
1018                 struct object_update_request *our;
1019                 int                     j;
1020
1021                 our = update_bufs[i];
1022                 update_count = our->ourq_count;
1023                 for (j = 0; j < update_count; j++) {
1024                         update = object_update_request_get(our, j, NULL);
1025                         if (update == NULL)
1026                                 GOTO(out, rc = -EPROTO);
1027
1028                         if (ptlrpc_req_need_swab(pill->rc_req))
1029                                 lustre_swab_object_update(update);
1030
1031                         if (!fid_is_sane(&update->ou_fid)) {
1032                                 CERROR("%s: invalid FID "DFID": rc = %d\n",
1033                                        tgt_name(tsi->tsi_tgt),
1034                                        PFID(&update->ou_fid), -EPROTO);
1035                                 GOTO(out, rc = err_serious(-EPROTO));
1036                         }
1037
1038                         dt_obj = dt_locate(env, dt, &update->ou_fid);
1039                         if (IS_ERR(dt_obj))
1040                                 GOTO(out, rc = PTR_ERR(dt_obj));
1041
1042                         if (dt->dd_record_fid_accessed) {
1043                                 lfsck_pack_rfa(&tti->tti_lr,
1044                                                lu_object_fid(&dt_obj->do_lu),
1045                                                LE_FID_ACCESSED,
1046                                                LFSCK_TYPE_LAYOUT);
1047                                 tgt_lfsck_in_notify(env, dt, &tti->tti_lr,
1048                                                     NULL);
1049                         }
1050
1051                         tti->tti_u.update.tti_dt_object = dt_obj;
1052                         tti->tti_u.update.tti_update = update;
1053                         tti->tti_u.update.tti_update_reply_index = reply_index;
1054
1055                         h = out_handler_find(update->ou_type);
1056                         if (unlikely(h == NULL)) {
1057                                 CERROR("%s: unsupported opc: 0x%x\n",
1058                                        tgt_name(tsi->tsi_tgt), update->ou_type);
1059                                 GOTO(next, rc = -ENOTSUPP);
1060                         }
1061
1062                         /* Check resend case only for modifying RPC */
1063                         if (h->th_flags & MUTABOR) {
1064                                 struct ptlrpc_request *req = tgt_ses_req(tsi);
1065
1066                                 if (out_check_resent(env, dt, dt_obj, req,
1067                                                      out_reconstruct, reply,
1068                                                      reply_index))
1069                                         GOTO(next, rc = 0);
1070                         }
1071
1072                         /* start transaction for modification RPC only */
1073                         if (h->th_flags & MUTABOR && current_batchid == -1) {
1074                                 current_batchid = update->ou_batchid;
1075                                 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1076                                 if (rc != 0)
1077                                         GOTO(next, rc);
1078
1079                                 if (update->ou_flags & UPDATE_FL_SYNC)
1080                                         ta->ta_handle->th_sync = 1;
1081                         }
1082
1083                         /* Stop the current update transaction, if the update
1084                          * has different batchid, or read-only update */
1085                         if (((current_batchid != update->ou_batchid) ||
1086                              !(h->th_flags & MUTABOR)) &&
1087                              ta->ta_handle != NULL) {
1088                                 rc = out_tx_end(env, ta, rc);
1089                                 current_batchid = -1;
1090                                 if (rc != 0)
1091                                         GOTO(next, rc);
1092
1093                                 /* start a new transaction if needed */
1094                                 if (h->th_flags & MUTABOR) {
1095                                         rc = out_tx_start(env, dt, ta,
1096                                                           tsi->tsi_exp);
1097                                         if (rc != 0)
1098                                                 GOTO(next, rc);
1099                                         if (update->ou_flags & UPDATE_FL_SYNC)
1100                                                 ta->ta_handle->th_sync = 1;
1101                                         current_batchid = update->ou_batchid;
1102                                 }
1103                         }
1104
1105                         rc = h->th_act(tsi);
1106 next:
1107                         reply_index++;
1108                         lu_object_put(env, &dt_obj->do_lu);
1109                         if (rc < 0)
1110                                 GOTO(out, rc);
1111                 }
1112         }
1113 out:
1114         if (current_batchid != -1) {
1115                 rc1 = out_tx_end(env, ta, rc);
1116                 if (rc == 0)
1117                         rc = rc1;
1118         }
1119
1120 out_free:
1121         if (update_bufs != NULL) {
1122                 if (oub != NULL) {
1123                         for (i = 0; i < update_buf_count; i++, oub++) {
1124                                 if (update_bufs[i] != NULL)
1125                                         OBD_FREE(update_bufs[i], oub->oub_size);
1126                         }
1127                 }
1128
1129                 OBD_FREE(update_bufs, sizeof(*update_bufs) * update_buf_count);
1130         }
1131
1132         if (desc != NULL)
1133                 ptlrpc_free_bulk(desc);
1134
1135         RETURN(rc);
1136 }
1137
1138 struct tgt_handler tgt_out_handlers[] = {
1139 TGT_UPDATE_HDL(MUTABOR, OUT_UPDATE,     out_handle),
1140 };
1141 EXPORT_SYMBOL(tgt_out_handlers);
1142