Whamcloud - gitweb
LU-6299 osp: zero-length EA xattr_set for striped_dir
[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, 2014, 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 int tx_extend_args(struct thandle_exec_args *ta, int new_alloc_ta)
40 {
41         struct tx_arg   **new_ta;
42         int             i;
43         int             rc = 0;
44
45         if (ta->ta_alloc_args >= new_alloc_ta)
46                 return 0;
47
48         OBD_ALLOC(new_ta, sizeof(*new_ta) * new_alloc_ta);
49         if (new_ta == NULL)
50                 return -ENOMEM;
51
52         for (i = 0; i < new_alloc_ta; i++) {
53                 if (i < ta->ta_alloc_args) {
54                         /* copy the old args to new one */
55                         new_ta[i] = ta->ta_args[i];
56                 } else {
57                         OBD_ALLOC_PTR(new_ta[i]);
58                         if (new_ta[i] == NULL)
59                                 GOTO(out, rc = -ENOMEM);
60                 }
61         }
62
63         /* free the old args */
64         if (ta->ta_args != NULL)
65                 OBD_FREE(ta->ta_args, sizeof(ta->ta_args[0]) *
66                                       ta->ta_alloc_args);
67
68         ta->ta_args = new_ta;
69         ta->ta_alloc_args = new_alloc_ta;
70 out:
71         if (rc != 0) {
72                 for (i = 0; i < new_alloc_ta; i++) {
73                         if (new_ta[i] != NULL)
74                                 OBD_FREE_PTR(new_ta[i]);
75                 }
76                 OBD_FREE(new_ta, sizeof(*new_ta) * new_alloc_ta);
77         }
78         return rc;
79 }
80
81 #define TX_ALLOC_STEP   8
82 static struct tx_arg *tx_add_exec(struct thandle_exec_args *ta,
83                                   tx_exec_func_t func, tx_exec_func_t undo,
84                                   const char *file, int line)
85 {
86         int rc;
87         int i;
88
89         LASSERT(ta != NULL);
90         LASSERT(func != NULL);
91
92         if (ta->ta_argno + 1 >= ta->ta_alloc_args) {
93                 rc = tx_extend_args(ta, ta->ta_alloc_args + TX_ALLOC_STEP);
94                 if (rc != 0)
95                         return ERR_PTR(rc);
96         }
97
98         i = ta->ta_argno;
99
100         ta->ta_argno++;
101
102         ta->ta_args[i]->exec_fn = func;
103         ta->ta_args[i]->undo_fn = undo;
104         ta->ta_args[i]->file    = file;
105         ta->ta_args[i]->line    = line;
106
107         return ta->ta_args[i];
108 }
109
110 static void out_reconstruct(const struct lu_env *env, struct dt_device *dt,
111                             struct dt_object *obj,
112                             struct object_update_reply *reply,
113                             int index)
114 {
115         CDEBUG(D_INFO, "%s: fork reply reply %p index %d: rc = %d\n",
116                dt_obd_name(dt), reply, index, 0);
117
118         object_update_result_insert(reply, NULL, 0, index, 0);
119         return;
120 }
121
122 typedef void (*out_reconstruct_t)(const struct lu_env *env,
123                                   struct dt_device *dt,
124                                   struct dt_object *obj,
125                                   struct object_update_reply *reply,
126                                   int index);
127
128 static inline int out_check_resent(const struct lu_env *env,
129                                    struct dt_device *dt,
130                                    struct dt_object *obj,
131                                    struct ptlrpc_request *req,
132                                    out_reconstruct_t reconstruct,
133                                    struct object_update_reply *reply,
134                                    int index)
135 {
136         if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
137                 return 0;
138
139         if (req_xid_is_last(req)) {
140                 reconstruct(env, dt, obj, reply, index);
141                 return 1;
142         }
143         DEBUG_REQ(D_HA, req, "no reply for RESENT req (have "LPD64")",
144                  req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
145         return 0;
146 }
147
148 static int out_obj_destroy(const struct lu_env *env, struct dt_object *dt_obj,
149                            struct thandle *th)
150 {
151         int rc;
152
153         CDEBUG(D_INFO, "%s: destroy "DFID"\n", dt_obd_name(th->th_dev),
154                PFID(lu_object_fid(&dt_obj->do_lu)));
155
156         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
157         rc = dt_destroy(env, dt_obj, th);
158         dt_write_unlock(env, dt_obj);
159
160         return rc;
161 }
162
163 /**
164  * All of the xxx_undo will be used once execution failed,
165  * But because all of the required resource has been reserved in
166  * declare phase, i.e. if declare succeed, it should make sure
167  * the following executing phase succeed in anyway, so these undo
168  * should be useless for most of the time in Phase I
169  */
170 static int out_tx_create_undo(const struct lu_env *env, struct thandle *th,
171                               struct tx_arg *arg)
172 {
173         int rc;
174
175         rc = out_obj_destroy(env, arg->object, th);
176         if (rc != 0)
177                 CERROR("%s: undo failure, we are doomed!: rc = %d\n",
178                        dt_obd_name(th->th_dev), rc);
179         return rc;
180 }
181
182 static int out_tx_create_exec(const struct lu_env *env, struct thandle *th,
183                               struct tx_arg *arg)
184 {
185         struct dt_object        *dt_obj = arg->object;
186         int                      rc;
187
188         CDEBUG(D_OTHER, "%s: create "DFID": dof %u, mode %o\n",
189                dt_obd_name(th->th_dev),
190                PFID(lu_object_fid(&arg->object->do_lu)),
191                arg->u.create.dof.dof_type,
192                arg->u.create.attr.la_mode & S_IFMT);
193
194         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
195         rc = dt_create(env, dt_obj, &arg->u.create.attr,
196                        &arg->u.create.hint, &arg->u.create.dof, th);
197
198         dt_write_unlock(env, dt_obj);
199
200         CDEBUG(D_INFO, "%s: insert create reply %p index %d: rc = %d\n",
201                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
202
203         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
204
205         return rc;
206 }
207
208 static int __out_tx_create(const struct lu_env *env, struct dt_object *obj,
209                            struct lu_attr *attr, struct lu_fid *parent_fid,
210                            struct dt_object_format *dof,
211                            struct thandle_exec_args *ta,
212                            struct object_update_reply *reply,
213                            int index, const char *file, int line)
214 {
215         struct tx_arg *arg;
216         int rc;
217
218         LASSERT(ta->ta_handle != NULL);
219         rc = dt_declare_create(env, obj, attr, NULL, dof,
220                                        ta->ta_handle);
221         if (rc != 0)
222                 return rc;
223
224         arg = tx_add_exec(ta, out_tx_create_exec, out_tx_create_undo, file,
225                           line);
226         if (IS_ERR(arg))
227                 return PTR_ERR(arg);
228
229         /* release the object in out_trans_stop */
230         lu_object_get(&obj->do_lu);
231         arg->object = obj;
232         arg->u.create.attr = *attr;
233         if (parent_fid != NULL)
234                 arg->u.create.fid = *parent_fid;
235         memset(&arg->u.create.hint, 0, sizeof(arg->u.create.hint));
236         arg->u.create.dof  = *dof;
237         arg->reply = reply;
238         arg->index = index;
239
240         return 0;
241 }
242
243 static int out_create(struct tgt_session_info *tsi)
244 {
245         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
246         struct object_update    *update = tti->tti_u.update.tti_update;
247         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
248         struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
249         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
250         struct lu_attr          *attr = &tti->tti_attr;
251         struct lu_fid           *fid = NULL;
252         struct obdo             *wobdo;
253         size_t                  size;
254         int                     rc;
255
256         ENTRY;
257
258         wobdo = object_update_param_get(update, 0, &size);
259         if (wobdo == NULL || IS_ERR(wobdo) || size != sizeof(*wobdo)) {
260                 CERROR("%s: obdo is NULL, invalid RPC: rc = %d\n",
261                        tgt_name(tsi->tsi_tgt), -EPROTO);
262                 RETURN(err_serious(-EPROTO));
263         }
264
265         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
266                 lustre_swab_obdo(wobdo);
267         lustre_get_wire_obdo(NULL, lobdo, wobdo);
268         la_from_obdo(attr, lobdo, lobdo->o_valid);
269
270         dof->dof_type = dt_mode_to_dft(attr->la_mode);
271         if (update->ou_params_count > 1) {
272                 fid = object_update_param_get(update, 1, &size);
273                 if (fid == NULL || IS_ERR(fid) || size != sizeof(*fid)) {
274                         CERROR("%s: invalid fid: rc = %d\n",
275                                tgt_name(tsi->tsi_tgt), -EPROTO);
276                         RETURN(err_serious(-EPROTO));
277                 }
278                 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
279                         lustre_swab_lu_fid(fid);
280                 if (!fid_is_sane(fid)) {
281                         CERROR("%s: invalid fid "DFID": rc = %d\n",
282                                tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
283                         RETURN(err_serious(-EPROTO));
284                 }
285         }
286
287         if (lu_object_exists(&obj->do_lu))
288                 RETURN(-EEXIST);
289
290         rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
291                            &tti->tti_tea,
292                            tti->tti_u.update.tti_update_reply,
293                            tti->tti_u.update.tti_update_reply_index);
294
295         RETURN(rc);
296 }
297
298 static int out_tx_attr_set_undo(const struct lu_env *env,
299                                 struct thandle *th, struct tx_arg *arg)
300 {
301         CERROR("%s: attr set undo "DFID" unimplemented yet!: rc = %d\n",
302                dt_obd_name(th->th_dev),
303                PFID(lu_object_fid(&arg->object->do_lu)), -ENOTSUPP);
304
305         return -ENOTSUPP;
306 }
307
308 static int out_tx_attr_set_exec(const struct lu_env *env, struct thandle *th,
309                                 struct tx_arg *arg)
310 {
311         struct dt_object        *dt_obj = arg->object;
312         int                     rc;
313
314         CDEBUG(D_OTHER, "%s: attr set "DFID"\n", dt_obd_name(th->th_dev),
315                PFID(lu_object_fid(&dt_obj->do_lu)));
316
317         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
318         rc = dt_attr_set(env, dt_obj, &arg->u.attr_set.attr, th);
319         dt_write_unlock(env, dt_obj);
320
321         CDEBUG(D_INFO, "%s: insert attr_set reply %p index %d: rc = %d\n",
322                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
323
324         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
325
326         return rc;
327 }
328
329 static int __out_tx_attr_set(const struct lu_env *env,
330                              struct dt_object *dt_obj,
331                              const struct lu_attr *attr,
332                              struct thandle_exec_args *th,
333                              struct object_update_reply *reply,
334                              int index, const char *file, int line)
335 {
336         struct tx_arg   *arg;
337         int             rc;
338
339         LASSERT(th->ta_handle != NULL);
340         rc = dt_declare_attr_set(env, dt_obj, attr, th->ta_handle);
341         if (rc != 0)
342                 return rc;
343
344         arg = tx_add_exec(th, out_tx_attr_set_exec, out_tx_attr_set_undo,
345                           file, line);
346         if (IS_ERR(arg))
347                 return PTR_ERR(arg);
348
349         lu_object_get(&dt_obj->do_lu);
350         arg->object = dt_obj;
351         arg->u.attr_set.attr = *attr;
352         arg->reply = reply;
353         arg->index = index;
354         return 0;
355 }
356
357 static int out_attr_set(struct tgt_session_info *tsi)
358 {
359         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
360         struct object_update    *update = tti->tti_u.update.tti_update;
361         struct lu_attr          *attr = &tti->tti_attr;
362         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
363         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
364         struct obdo             *wobdo;
365         size_t                   size;
366         int                      rc;
367
368         ENTRY;
369
370         wobdo = object_update_param_get(update, 0, &size);
371         if (wobdo == NULL || IS_ERR(wobdo) || size != sizeof(*wobdo)) {
372                 CERROR("%s: empty obdo in the update: rc = %d\n",
373                        tgt_name(tsi->tsi_tgt), -EPROTO);
374                 RETURN(err_serious(-EPROTO));
375         }
376
377         attr->la_valid = 0;
378         attr->la_valid = 0;
379
380         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
381                 lustre_swab_obdo(wobdo);
382         lustre_get_wire_obdo(NULL, lobdo, wobdo);
383         la_from_obdo(attr, lobdo, lobdo->o_valid);
384
385         rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
386                              tti->tti_u.update.tti_update_reply,
387                              tti->tti_u.update.tti_update_reply_index);
388
389         RETURN(rc);
390 }
391
392 static int out_attr_get(struct tgt_session_info *tsi)
393 {
394         const struct lu_env     *env = tsi->tsi_env;
395         struct tgt_thread_info  *tti = tgt_th_info(env);
396         struct obdo             *obdo = &tti->tti_u.update.tti_obdo;
397         struct lu_attr          *la = &tti->tti_attr;
398         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
399         int                     idx = tti->tti_u.update.tti_update_reply_index;
400         int                     rc;
401
402         ENTRY;
403
404         if (!lu_object_exists(&obj->do_lu)) {
405                 /* Usually, this will be called when the master MDT try
406                  * to init a remote object(see osp_object_init), so if
407                  * the object does not exist on slave, we need set BANSHEE flag,
408                  * so the object can be removed from the cache immediately */
409                 set_bit(LU_OBJECT_HEARD_BANSHEE,
410                         &obj->do_lu.lo_header->loh_flags);
411                 RETURN(-ENOENT);
412         }
413
414         dt_read_lock(env, obj, MOR_TGT_CHILD);
415         rc = dt_attr_get(env, obj, la);
416         if (rc)
417                 GOTO(out_unlock, rc);
418
419         obdo->o_valid = 0;
420         obdo_from_la(obdo, la, la->la_valid);
421         lustre_set_wire_obdo(NULL, obdo, obdo);
422
423 out_unlock:
424         dt_read_unlock(env, obj);
425
426         CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
427                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
428                0, rc);
429
430         object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
431                                     sizeof(*obdo), idx, rc);
432
433         RETURN(rc);
434 }
435
436 static int out_xattr_get(struct tgt_session_info *tsi)
437 {
438         const struct lu_env        *env = tsi->tsi_env;
439         struct tgt_thread_info     *tti = tgt_th_info(env);
440         struct object_update       *update = tti->tti_u.update.tti_update;
441         struct lu_buf              *lbuf = &tti->tti_buf;
442         struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
443         struct dt_object           *obj = tti->tti_u.update.tti_dt_object;
444         char                       *name;
445         struct object_update_result *update_result;
446         int                     idx = tti->tti_u.update.tti_update_reply_index;
447         int                        rc;
448
449         ENTRY;
450
451         if (!lu_object_exists(&obj->do_lu)) {
452                 set_bit(LU_OBJECT_HEARD_BANSHEE,
453                         &obj->do_lu.lo_header->loh_flags);
454                 RETURN(-ENOENT);
455         }
456
457         name = object_update_param_get(update, 0, NULL);
458         if (name == NULL || IS_ERR(name)) {
459                 CERROR("%s: empty name for xattr get: rc = %d\n",
460                        tgt_name(tsi->tsi_tgt), -EPROTO);
461                 RETURN(err_serious(-EPROTO));
462         }
463
464         update_result = object_update_result_get(reply, 0, NULL);
465         if (update_result == NULL) {
466                 CERROR("%s: empty name for xattr get: rc = %d\n",
467                        tgt_name(tsi->tsi_tgt), -EPROTO);
468                 RETURN(err_serious(-EPROTO));
469         }
470
471         lbuf->lb_buf = update_result->our_data;
472         lbuf->lb_len = OUT_UPDATE_REPLY_SIZE -
473                        cfs_size_round((unsigned long)update_result->our_data -
474                                       (unsigned long)update_result);
475         dt_read_lock(env, obj, MOR_TGT_CHILD);
476         rc = dt_xattr_get(env, obj, lbuf, name);
477         dt_read_unlock(env, obj);
478         if (rc < 0) {
479                 lbuf->lb_len = 0;
480                 GOTO(out, rc);
481         }
482         if (rc == 0) {
483                 lbuf->lb_len = 0;
484                 GOTO(out, rc = -ENOENT);
485         }
486         lbuf->lb_len = rc;
487         rc = 0;
488         CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
489                tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
490                name, (int)lbuf->lb_len);
491
492         GOTO(out, rc);
493
494 out:
495         object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
496         RETURN(rc);
497 }
498
499 static int out_index_lookup(struct tgt_session_info *tsi)
500 {
501         const struct lu_env     *env = tsi->tsi_env;
502         struct tgt_thread_info  *tti = tgt_th_info(env);
503         struct object_update    *update = tti->tti_u.update.tti_update;
504         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
505         char                    *name;
506         int                      rc;
507
508         ENTRY;
509
510         if (!lu_object_exists(&obj->do_lu))
511                 RETURN(-ENOENT);
512
513         name = object_update_param_get(update, 0, NULL);
514         if (name == NULL || IS_ERR(name)) {
515                 CERROR("%s: empty name for lookup: rc = %d\n",
516                        tgt_name(tsi->tsi_tgt), -EPROTO);
517                 RETURN(err_serious(-EPROTO));
518         }
519
520         dt_read_lock(env, obj, MOR_TGT_CHILD);
521         if (!dt_try_as_dir(env, obj))
522                 GOTO(out_unlock, rc = -ENOTDIR);
523
524         rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
525                        (struct dt_key *)name);
526
527         if (rc < 0)
528                 GOTO(out_unlock, rc);
529
530         if (rc == 0)
531                 rc += 1;
532
533 out_unlock:
534         dt_read_unlock(env, obj);
535
536         CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
537                PFID(lu_object_fid(&obj->do_lu)), name,
538                PFID(&tti->tti_fid1), rc);
539
540         CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
541                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
542                0, rc);
543
544         object_update_result_insert(tti->tti_u.update.tti_update_reply,
545                             &tti->tti_fid1, sizeof(tti->tti_fid1),
546                             tti->tti_u.update.tti_update_reply_index, rc);
547         RETURN(rc);
548 }
549
550 static int out_tx_xattr_set_exec(const struct lu_env *env,
551                                  struct thandle *th,
552                                  struct tx_arg *arg)
553 {
554         struct dt_object *dt_obj = arg->object;
555         int rc;
556
557         CDEBUG(D_INFO, "%s: set xattr buf %p name %s flag %d\n",
558                dt_obd_name(th->th_dev), arg->u.xattr_set.buf.lb_buf,
559                arg->u.xattr_set.name, arg->u.xattr_set.flags);
560
561         if (!lu_object_exists(&dt_obj->do_lu))
562                 GOTO(out, rc = -ENOENT);
563
564         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
565         rc = dt_xattr_set(env, dt_obj, &arg->u.xattr_set.buf,
566                           arg->u.xattr_set.name, arg->u.xattr_set.flags, th);
567         /**
568          * Ignore errors if this is LINK EA
569          **/
570         if (unlikely(rc != 0 &&
571                      strcmp(arg->u.xattr_set.name, XATTR_NAME_LINK) == 0)) {
572                 /* XXX: If the linkEA is overflow, then we need to notify the
573                  *      namespace LFSCK to skip "nlink" attribute verification
574                  *      on this object to avoid the "nlink" to be shrinked by
575                  *      wrong. It may be not good an interaction with LFSCK
576                  *      like this. We will consider to replace it with other
577                  *      mechanism in future. LU-5802. */
578                 if (rc == -ENOSPC) {
579                         struct lfsck_request *lr = &tgt_th_info(env)->tti_lr;
580
581                         lfsck_pack_rfa(lr, lu_object_fid(&dt_obj->do_lu),
582                                        LE_SKIP_NLINK, LFSCK_TYPE_NAMESPACE);
583                         tgt_lfsck_in_notify(env,
584                                 tgt_ses_info(env)->tsi_tgt->lut_bottom, lr, th);
585                 }
586
587                 rc = 0;
588         }
589         dt_write_unlock(env, dt_obj);
590
591 out:
592         CDEBUG(D_INFO, "%s: insert xattr set reply %p index %d: rc = %d\n",
593                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
594
595         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
596
597         return rc;
598 }
599
600 static int __out_tx_xattr_set(const struct lu_env *env,
601                               struct dt_object *dt_obj,
602                               const struct lu_buf *buf,
603                               const char *name, int flags,
604                               struct thandle_exec_args *ta,
605                               struct object_update_reply *reply,
606                               int index, const char *file, int line)
607 {
608         struct tx_arg   *arg;
609         int             rc;
610
611         LASSERT(ta->ta_handle != NULL);
612         rc = dt_declare_xattr_set(env, dt_obj, buf, name, flags, ta->ta_handle);
613         if (rc != 0)
614                 return rc;
615
616         if (strcmp(name, XATTR_NAME_LINK) == 0) {
617                 struct lfsck_request *lr = &tgt_th_info(env)->tti_lr;
618
619                 /* XXX: If the linkEA is overflow, then we need to notify the
620                  *      namespace LFSCK to skip "nlink" attribute verification
621                  *      on this object to avoid the "nlink" to be shrinked by
622                  *      wrong. It may be not good an interaction with LFSCK
623                  *      like this. We will consider to replace it with other
624                  *      mechanism in future. LU-5802. */
625                 lfsck_pack_rfa(lr, lu_object_fid(&dt_obj->do_lu),
626                                LE_SKIP_NLINK_DECLARE, LFSCK_TYPE_NAMESPACE);
627                 rc = tgt_lfsck_in_notify(env,
628                                          tgt_ses_info(env)->tsi_tgt->lut_bottom,
629                                          lr, ta->ta_handle);
630                 if (rc != 0)
631                         return rc;
632         }
633
634         arg = tx_add_exec(ta, out_tx_xattr_set_exec, NULL, file, line);
635         if (IS_ERR(arg))
636                 return PTR_ERR(arg);
637
638         lu_object_get(&dt_obj->do_lu);
639         arg->object = dt_obj;
640         arg->u.xattr_set.name = name;
641         arg->u.xattr_set.flags = flags;
642         arg->u.xattr_set.buf = *buf;
643         arg->reply = reply;
644         arg->index = index;
645         arg->u.xattr_set.csum = 0;
646         return 0;
647 }
648
649 static int out_xattr_set(struct tgt_session_info *tsi)
650 {
651         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
652         struct object_update    *update = tti->tti_u.update.tti_update;
653         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
654         struct lu_buf           *lbuf = &tti->tti_buf;
655         char                    *name;
656         char                    *buf;
657         __u32                   *tmp;
658         size_t                   buf_len = 0;
659         int                      flag;
660         size_t                   size = 0;
661         int                      rc;
662         ENTRY;
663
664         name = object_update_param_get(update, 0, NULL);
665         if (name == NULL || IS_ERR(name)) {
666                 CERROR("%s: empty name for xattr set: rc = %d\n",
667                        tgt_name(tsi->tsi_tgt), -EPROTO);
668                 RETURN(err_serious(-EPROTO));
669         }
670
671         buf = object_update_param_get(update, 1, &buf_len);
672         if (IS_ERR(buf))
673                 RETURN(err_serious(-EPROTO));
674
675         lbuf->lb_buf = buf;
676         lbuf->lb_len = buf_len;
677
678         tmp = object_update_param_get(update, 2, &size);
679         if (tmp == NULL || size != sizeof(*tmp)) {
680                 CERROR("%s: emptry or wrong size %zu flag: rc = %d\n",
681                        tgt_name(tsi->tsi_tgt), size, -EPROTO);
682                 RETURN(err_serious(-EPROTO));
683         }
684
685         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
686                 __swab32s(tmp);
687         flag = *tmp;
688
689         rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
690                               &tti->tti_tea,
691                               tti->tti_u.update.tti_update_reply,
692                               tti->tti_u.update.tti_update_reply_index);
693         RETURN(rc);
694 }
695
696 static int out_tx_xattr_del_exec(const struct lu_env *env, struct thandle *th,
697                                  struct tx_arg *arg)
698 {
699         struct dt_object *dt_obj = arg->object;
700         int rc;
701
702         CDEBUG(D_INFO, "%s: del xattr name '%s' on "DFID"\n",
703                dt_obd_name(th->th_dev), arg->u.xattr_set.name,
704                PFID(lu_object_fid(&dt_obj->do_lu)));
705
706         if (!lu_object_exists(&dt_obj->do_lu))
707                 GOTO(out, rc = -ENOENT);
708
709         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
710         rc = dt_xattr_del(env, dt_obj, arg->u.xattr_set.name, th);
711         dt_write_unlock(env, dt_obj);
712 out:
713         CDEBUG(D_INFO, "%s: insert xattr del reply %p index %d: rc = %d\n",
714                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
715
716         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
717
718         return rc;
719 }
720
721 static int __out_tx_xattr_del(const struct lu_env *env,
722                               struct dt_object *dt_obj, const char *name,
723                               struct thandle_exec_args *ta,
724                               struct object_update_reply *reply,
725                               int index, const char *file, int line)
726 {
727         struct tx_arg   *arg;
728         int             rc;
729
730         rc = dt_declare_xattr_del(env, dt_obj, name, ta->ta_handle);
731         if (rc != 0)
732                 return rc;
733
734         arg = tx_add_exec(ta, out_tx_xattr_del_exec, NULL, file, line);
735         if (IS_ERR(arg))
736                 return PTR_ERR(arg);
737
738         lu_object_get(&dt_obj->do_lu);
739         arg->object = dt_obj;
740         arg->u.xattr_set.name = name;
741         arg->reply = reply;
742         arg->index = index;
743         return 0;
744 }
745
746 static int out_xattr_del(struct tgt_session_info *tsi)
747 {
748         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
749         struct object_update    *update = tti->tti_u.update.tti_update;
750         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
751         char                    *name;
752         int                      rc;
753         ENTRY;
754
755         name = object_update_param_get(update, 0, NULL);
756         if (name == NULL || IS_ERR(name)) {
757                 CERROR("%s: empty name for xattr set: rc = %d\n",
758                        tgt_name(tsi->tsi_tgt), -EPROTO);
759                 RETURN(err_serious(-EPROTO));
760         }
761
762         rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
763                               tti->tti_u.update.tti_update_reply,
764                               tti->tti_u.update.tti_update_reply_index);
765         RETURN(rc);
766 }
767
768 static int out_obj_ref_add(const struct lu_env *env,
769                            struct dt_object *dt_obj,
770                            struct thandle *th)
771 {
772         int rc;
773
774         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
775         rc = dt_ref_add(env, dt_obj, th);
776         dt_write_unlock(env, dt_obj);
777
778         return rc;
779 }
780
781 static int out_obj_ref_del(const struct lu_env *env,
782                            struct dt_object *dt_obj,
783                            struct thandle *th)
784 {
785         int rc;
786
787         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
788         rc = dt_ref_del(env, dt_obj, th);
789         dt_write_unlock(env, dt_obj);
790
791         return rc;
792 }
793
794 static int out_tx_ref_add_exec(const struct lu_env *env, struct thandle *th,
795                                struct tx_arg *arg)
796 {
797         struct dt_object *dt_obj = arg->object;
798         int rc;
799
800         rc = out_obj_ref_add(env, dt_obj, th);
801
802         CDEBUG(D_INFO, "%s: insert ref_add reply %p index %d: rc = %d\n",
803                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
804
805         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
806         return rc;
807 }
808
809 static int out_tx_ref_add_undo(const struct lu_env *env, struct thandle *th,
810                                struct tx_arg *arg)
811 {
812         return out_obj_ref_del(env, arg->object, th);
813 }
814
815 static int __out_tx_ref_add(const struct lu_env *env,
816                             struct dt_object *dt_obj,
817                             struct thandle_exec_args *ta,
818                             struct object_update_reply *reply,
819                             int index, const char *file, int line)
820 {
821         struct tx_arg   *arg;
822         int             rc;
823
824         LASSERT(ta->ta_handle != NULL);
825         rc = dt_declare_ref_add(env, dt_obj, ta->ta_handle);
826         if (rc != 0)
827                 return rc;
828
829         arg = tx_add_exec(ta, out_tx_ref_add_exec, out_tx_ref_add_undo, file,
830                           line);
831         if (IS_ERR(arg))
832                 return PTR_ERR(arg);
833
834         lu_object_get(&dt_obj->do_lu);
835         arg->object = dt_obj;
836         arg->reply = reply;
837         arg->index = index;
838         return 0;
839 }
840
841 /**
842  * increase ref of the object
843  **/
844 static int out_ref_add(struct tgt_session_info *tsi)
845 {
846         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
847         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
848         int                      rc;
849
850         ENTRY;
851
852         rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
853                             tti->tti_u.update.tti_update_reply,
854                             tti->tti_u.update.tti_update_reply_index);
855         RETURN(rc);
856 }
857
858 static int out_tx_ref_del_exec(const struct lu_env *env, struct thandle *th,
859                                struct tx_arg *arg)
860 {
861         struct dt_object        *dt_obj = arg->object;
862         int                      rc;
863
864         rc = out_obj_ref_del(env, dt_obj, th);
865
866         CDEBUG(D_INFO, "%s: insert ref_del reply %p index %d: rc = %d\n",
867                dt_obd_name(th->th_dev), arg->reply, arg->index, 0);
868
869         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
870
871         return rc;
872 }
873
874 static int out_tx_ref_del_undo(const struct lu_env *env, struct thandle *th,
875                                struct tx_arg *arg)
876 {
877         return out_obj_ref_add(env, arg->object, th);
878 }
879
880 static int __out_tx_ref_del(const struct lu_env *env,
881                             struct dt_object *dt_obj,
882                             struct thandle_exec_args *ta,
883                             struct object_update_reply *reply,
884                             int index, const char *file, int line)
885 {
886         struct tx_arg   *arg;
887         int             rc;
888
889         LASSERT(ta->ta_handle != NULL);
890         rc = dt_declare_ref_del(env, dt_obj, ta->ta_handle);
891         if (rc != 0)
892                 return rc;
893
894         arg = tx_add_exec(ta, out_tx_ref_del_exec, out_tx_ref_del_undo, file,
895                           line);
896         if (IS_ERR(arg))
897                 return PTR_ERR(arg);
898
899         lu_object_get(&dt_obj->do_lu);
900         arg->object = dt_obj;
901         arg->reply = reply;
902         arg->index = index;
903         return 0;
904 }
905
906 static int out_ref_del(struct tgt_session_info *tsi)
907 {
908         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
909         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
910         int                      rc;
911
912         ENTRY;
913
914         if (!lu_object_exists(&obj->do_lu))
915                 RETURN(-ENOENT);
916
917         rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
918                             tti->tti_u.update.tti_update_reply,
919                             tti->tti_u.update.tti_update_reply_index);
920         RETURN(rc);
921 }
922
923 static int out_obj_index_insert(const struct lu_env *env,
924                                 struct dt_object *dt_obj,
925                                 const struct dt_rec *rec,
926                                 const struct dt_key *key,
927                                 struct thandle *th)
928 {
929         int rc;
930
931         CDEBUG(D_INFO, "%s: index insert "DFID" name: %s fid "DFID", type %u\n",
932                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
933                (char *)key, PFID(((struct dt_insert_rec *)rec)->rec_fid),
934                ((struct dt_insert_rec *)rec)->rec_type);
935
936         if (dt_try_as_dir(env, dt_obj) == 0)
937                 return -ENOTDIR;
938
939         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
940         rc = dt_insert(env, dt_obj, rec, key, th, 0);
941         dt_write_unlock(env, dt_obj);
942
943         return rc;
944 }
945
946 static int out_obj_index_delete(const struct lu_env *env,
947                                 struct dt_object *dt_obj,
948                                 const struct dt_key *key,
949                                 struct thandle *th)
950 {
951         int rc;
952
953         CDEBUG(D_INFO, "%s: index delete "DFID" name: %s\n",
954                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
955                (char *)key);
956
957         if (dt_try_as_dir(env, dt_obj) == 0)
958                 return -ENOTDIR;
959
960         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
961         rc = dt_delete(env, dt_obj, key, th);
962         dt_write_unlock(env, dt_obj);
963
964         return rc;
965 }
966
967 static int out_tx_index_insert_exec(const struct lu_env *env,
968                                     struct thandle *th, struct tx_arg *arg)
969 {
970         struct dt_object *dt_obj = arg->object;
971         int rc;
972
973         if (unlikely(!dt_object_exists(dt_obj)))
974                 RETURN(-ESTALE);
975
976         rc = out_obj_index_insert(env, dt_obj,
977                                   (const struct dt_rec *)&arg->u.insert.rec,
978                                   arg->u.insert.key, th);
979
980         CDEBUG(D_INFO, "%s: insert idx insert reply %p index %d: rc = %d\n",
981                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
982
983         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
984
985         return rc;
986 }
987
988 static int out_tx_index_insert_undo(const struct lu_env *env,
989                                     struct thandle *th, struct tx_arg *arg)
990 {
991         return out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
992 }
993
994 static int __out_tx_index_insert(const struct lu_env *env,
995                                  struct dt_object *dt_obj,
996                                  const struct dt_rec *rec,
997                                  const struct dt_key *key,
998                                  struct thandle_exec_args *ta,
999                                  struct object_update_reply *reply,
1000                                  int index, const char *file, int line)
1001 {
1002         struct tx_arg   *arg;
1003         int             rc;
1004
1005         LASSERT(ta->ta_handle != NULL);
1006         if (dt_try_as_dir(env, dt_obj) == 0) {
1007                 rc = -ENOTDIR;
1008                 return rc;
1009         }
1010
1011         rc = dt_declare_insert(env, dt_obj, rec, key, ta->ta_handle);
1012         if (rc != 0)
1013                 return rc;
1014
1015         arg = tx_add_exec(ta, out_tx_index_insert_exec,
1016                           out_tx_index_insert_undo, file, line);
1017         if (IS_ERR(arg))
1018                 return PTR_ERR(arg);
1019
1020         lu_object_get(&dt_obj->do_lu);
1021         arg->object = dt_obj;
1022         arg->reply = reply;
1023         arg->index = index;
1024         arg->u.insert.rec = *(const struct dt_insert_rec *)rec;
1025         arg->u.insert.key = key;
1026
1027         return 0;
1028 }
1029
1030 static int out_index_insert(struct tgt_session_info *tsi)
1031 {
1032         struct tgt_thread_info  *tti    = tgt_th_info(tsi->tsi_env);
1033         struct object_update    *update = tti->tti_u.update.tti_update;
1034         struct dt_object        *obj    = tti->tti_u.update.tti_dt_object;
1035         struct dt_insert_rec    *rec    = &tti->tti_rec;
1036         struct lu_fid           *fid;
1037         char                    *name;
1038         __u32                   *ptype;
1039         int                      rc     = 0;
1040         size_t                   size;
1041         ENTRY;
1042
1043         name = object_update_param_get(update, 0, NULL);
1044         if (name == NULL || IS_ERR(name)) {
1045                 CERROR("%s: empty name for index insert: rc = %d\n",
1046                        tgt_name(tsi->tsi_tgt), -EPROTO);
1047                 RETURN(err_serious(-EPROTO));
1048         }
1049
1050         fid = object_update_param_get(update, 1, &size);
1051         if (fid == NULL || IS_ERR(fid) || size != sizeof(*fid)) {
1052                 CERROR("%s: invalid fid: rc = %d\n",
1053                        tgt_name(tsi->tsi_tgt), -EPROTO);
1054                 RETURN(err_serious(-EPROTO));
1055         }
1056
1057         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
1058                 lustre_swab_lu_fid(fid);
1059
1060         if (!fid_is_sane(fid)) {
1061                 CERROR("%s: invalid FID "DFID": rc = %d\n",
1062                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
1063                 RETURN(err_serious(-EPROTO));
1064         }
1065
1066         ptype = object_update_param_get(update, 2, &size);
1067         if (ptype == NULL || IS_ERR(ptype) || size != sizeof(*ptype)) {
1068                 CERROR("%s: invalid type for index insert: rc = %d\n",
1069                        tgt_name(tsi->tsi_tgt), -EPROTO);
1070                 RETURN(err_serious(-EPROTO));
1071         }
1072
1073         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
1074                 __swab32s(ptype);
1075
1076         rec->rec_fid = fid;
1077         rec->rec_type = *ptype;
1078
1079         rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
1080                                  (const struct dt_key *)name, &tti->tti_tea,
1081                                  tti->tti_u.update.tti_update_reply,
1082                                  tti->tti_u.update.tti_update_reply_index);
1083         RETURN(rc);
1084 }
1085
1086 static int out_tx_index_delete_exec(const struct lu_env *env,
1087                                     struct thandle *th,
1088                                     struct tx_arg *arg)
1089 {
1090         int rc;
1091
1092         rc = out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
1093
1094         CDEBUG(D_INFO, "%s: delete idx insert reply %p index %d: rc = %d\n",
1095                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1096
1097         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
1098
1099         return rc;
1100 }
1101
1102 static int out_tx_index_delete_undo(const struct lu_env *env,
1103                                     struct thandle *th,
1104                                     struct tx_arg *arg)
1105 {
1106         CERROR("%s: Oops, can not rollback index_delete yet: rc = %d\n",
1107                dt_obd_name(th->th_dev), -ENOTSUPP);
1108         return -ENOTSUPP;
1109 }
1110
1111 static int __out_tx_index_delete(const struct lu_env *env,
1112                                  struct dt_object *dt_obj,
1113                                  const struct dt_key *key,
1114                                  struct thandle_exec_args *ta,
1115                                  struct object_update_reply *reply,
1116                                  int index, const char *file, int line)
1117 {
1118         struct tx_arg   *arg;
1119         int             rc;
1120
1121         if (dt_try_as_dir(env, dt_obj) == 0) {
1122                 rc = -ENOTDIR;
1123                 return rc;
1124         }
1125
1126         LASSERT(ta->ta_handle != NULL);
1127         rc = dt_declare_delete(env, dt_obj, key, ta->ta_handle);
1128         if (rc != 0)
1129                 return rc;
1130
1131         arg = tx_add_exec(ta, out_tx_index_delete_exec,
1132                           out_tx_index_delete_undo, file, line);
1133         if (IS_ERR(arg))
1134                 return PTR_ERR(arg);
1135
1136         lu_object_get(&dt_obj->do_lu);
1137         arg->object = dt_obj;
1138         arg->reply = reply;
1139         arg->index = index;
1140         arg->u.insert.key = key;
1141         return 0;
1142 }
1143
1144 static int out_index_delete(struct tgt_session_info *tsi)
1145 {
1146         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1147         struct object_update    *update = tti->tti_u.update.tti_update;
1148         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
1149         char                    *name;
1150         int                      rc = 0;
1151
1152         if (!lu_object_exists(&obj->do_lu))
1153                 RETURN(-ENOENT);
1154
1155         name = object_update_param_get(update, 0, NULL);
1156         if (name == NULL || IS_ERR(name)) {
1157                 CERROR("%s: empty name for index delete: rc = %d\n",
1158                        tgt_name(tsi->tsi_tgt), -EPROTO);
1159                 RETURN(err_serious(-EPROTO));
1160         }
1161
1162         rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
1163                                  &tti->tti_tea,
1164                                  tti->tti_u.update.tti_update_reply,
1165                                  tti->tti_u.update.tti_update_reply_index);
1166         RETURN(rc);
1167 }
1168
1169 static int out_tx_destroy_exec(const struct lu_env *env, struct thandle *th,
1170                                struct tx_arg *arg)
1171 {
1172         struct dt_object *dt_obj = arg->object;
1173         int rc;
1174
1175         rc = out_obj_destroy(env, dt_obj, th);
1176
1177         CDEBUG(D_INFO, "%s: insert destroy reply %p index %d: rc = %d\n",
1178                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1179
1180         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
1181
1182         RETURN(rc);
1183 }
1184
1185 static int out_tx_destroy_undo(const struct lu_env *env, struct thandle *th,
1186                                struct tx_arg *arg)
1187 {
1188         CERROR("%s: not support destroy undo yet!: rc = %d\n",
1189                dt_obd_name(th->th_dev), -ENOTSUPP);
1190         return -ENOTSUPP;
1191 }
1192
1193 static int __out_tx_destroy(const struct lu_env *env, struct dt_object *dt_obj,
1194                              struct thandle_exec_args *ta,
1195                              struct object_update_reply *reply,
1196                              int index, const char *file, int line)
1197 {
1198         struct tx_arg   *arg;
1199         int             rc;
1200
1201         LASSERT(ta->ta_handle != NULL);
1202         rc = dt_declare_destroy(env, dt_obj, ta->ta_handle);
1203         if (rc != 0)
1204                 return rc;
1205
1206         arg = tx_add_exec(ta, out_tx_destroy_exec, out_tx_destroy_undo,
1207                           file, line);
1208         if (IS_ERR(arg))
1209                 return PTR_ERR(arg);
1210
1211         lu_object_get(&dt_obj->do_lu);
1212         arg->object = dt_obj;
1213         arg->reply = reply;
1214         arg->index = index;
1215         return 0;
1216 }
1217
1218 static int out_destroy(struct tgt_session_info *tsi)
1219 {
1220         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1221         struct object_update    *update = tti->tti_u.update.tti_update;
1222         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
1223         struct lu_fid           *fid;
1224         int                      rc;
1225         ENTRY;
1226
1227         fid = &update->ou_fid;
1228         if (!fid_is_sane(fid)) {
1229                 CERROR("%s: invalid FID "DFID": rc = %d\n",
1230                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
1231                 RETURN(err_serious(-EPROTO));
1232         }
1233
1234         if (!lu_object_exists(&obj->do_lu))
1235                 RETURN(-ENOENT);
1236
1237         rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
1238                             tti->tti_u.update.tti_update_reply,
1239                             tti->tti_u.update.tti_update_reply_index);
1240
1241         RETURN(rc);
1242 }
1243
1244 static int out_tx_write_exec(const struct lu_env *env, struct thandle *th,
1245                              struct tx_arg *arg)
1246 {
1247         struct dt_object *dt_obj = arg->object;
1248         int rc;
1249
1250         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
1251         rc = dt_record_write(env, dt_obj, &arg->u.write.buf,
1252                              &arg->u.write.pos, th);
1253         dt_write_unlock(env, dt_obj);
1254
1255         if (rc == 0)
1256                 rc = arg->u.write.buf.lb_len;
1257
1258         object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
1259
1260         return rc > 0 ? 0 : rc;
1261 }
1262
1263 static int __out_tx_write(const struct lu_env *env,
1264                           struct dt_object *dt_obj,
1265                           const struct lu_buf *buf,
1266                           loff_t pos, struct thandle_exec_args *ta,
1267                           struct object_update_reply *reply,
1268                           int index, const char *file, int line)
1269 {
1270         struct tx_arg   *arg;
1271         int             rc;
1272
1273         LASSERT(ta->ta_handle != NULL);
1274         rc = dt_declare_record_write(env, dt_obj, buf, pos, ta->ta_handle);
1275         if (rc != 0)
1276                 return rc;
1277
1278         arg = tx_add_exec(ta, out_tx_write_exec, NULL, file, line);
1279         if (IS_ERR(arg))
1280                 return PTR_ERR(arg);
1281
1282         lu_object_get(&dt_obj->do_lu);
1283         arg->object = dt_obj;
1284         arg->u.write.buf = *buf;
1285         arg->u.write.pos = pos;
1286         arg->reply = reply;
1287         arg->index = index;
1288         return 0;
1289 }
1290
1291 static int out_write(struct tgt_session_info *tsi)
1292 {
1293         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1294         struct object_update    *update = tti->tti_u.update.tti_update;
1295         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
1296         struct lu_buf           *lbuf = &tti->tti_buf;
1297         char                    *buf;
1298         __u64                   *tmp;
1299         size_t                  size = 0;
1300         size_t                  buf_len = 0;
1301         loff_t                  pos;
1302         int                      rc;
1303         ENTRY;
1304
1305         buf = object_update_param_get(update, 0, &buf_len);
1306         if (buf == NULL || IS_ERR(buf) || buf_len == 0) {
1307                 CERROR("%s: empty buf for xattr set: rc = %d\n",
1308                        tgt_name(tsi->tsi_tgt), -EPROTO);
1309                 RETURN(err_serious(-EPROTO));
1310         }
1311         lbuf->lb_buf = buf;
1312         lbuf->lb_len = buf_len;
1313
1314         tmp = object_update_param_get(update, 1, &size);
1315         if (tmp == NULL || IS_ERR(tmp) || size != sizeof(*tmp)) {
1316                 CERROR("%s: empty or wrong size %zu pos: rc = %d\n",
1317                        tgt_name(tsi->tsi_tgt), size, -EPROTO);
1318                 RETURN(err_serious(-EPROTO));
1319         }
1320
1321         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
1322                 __swab64s(tmp);
1323         pos = *tmp;
1324
1325         rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
1326                           &tti->tti_tea,
1327                           tti->tti_u.update.tti_update_reply,
1328                           tti->tti_u.update.tti_update_reply_index);
1329         RETURN(rc);
1330 }
1331
1332 #define DEF_OUT_HNDL(opc, name, flags, fn)     \
1333 [opc - OUT_CREATE] = {                                  \
1334         .th_name    = name,                             \
1335         .th_fail_id = 0,                                \
1336         .th_opc     = opc,                              \
1337         .th_flags   = flags,                            \
1338         .th_act     = fn,                               \
1339         .th_fmt     = NULL,                             \
1340         .th_version = 0,                                \
1341 }
1342
1343 static struct tgt_handler out_update_ops[] = {
1344         DEF_OUT_HNDL(OUT_CREATE, "out_create", MUTABOR | HABEO_REFERO,
1345                      out_create),
1346         DEF_OUT_HNDL(OUT_DESTROY, "out_create", MUTABOR | HABEO_REFERO,
1347                      out_destroy),
1348         DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", MUTABOR | HABEO_REFERO,
1349                      out_ref_add),
1350         DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", MUTABOR | HABEO_REFERO,
1351                      out_ref_del),
1352         DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set",  MUTABOR | HABEO_REFERO,
1353                      out_attr_set),
1354         DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get",  HABEO_REFERO,
1355                      out_attr_get),
1356         DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", MUTABOR | HABEO_REFERO,
1357                      out_xattr_set),
1358         DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", MUTABOR | HABEO_REFERO,
1359                      out_xattr_del),
1360         DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HABEO_REFERO,
1361                      out_xattr_get),
1362         DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HABEO_REFERO,
1363                      out_index_lookup),
1364         DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
1365                      MUTABOR | HABEO_REFERO, out_index_insert),
1366         DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
1367                      MUTABOR | HABEO_REFERO, out_index_delete),
1368         DEF_OUT_HNDL(OUT_WRITE, "out_write", MUTABOR | HABEO_REFERO, out_write),
1369 };
1370
1371 static struct tgt_handler *out_handler_find(__u32 opc)
1372 {
1373         struct tgt_handler *h;
1374
1375         h = NULL;
1376         if (OUT_CREATE <= opc && opc < OUT_LAST) {
1377                 h = &out_update_ops[opc - OUT_CREATE];
1378                 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
1379                          h->th_opc, opc);
1380         } else {
1381                 h = NULL; /* unsupported opc */
1382         }
1383         return h;
1384 }
1385
1386 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
1387                         struct thandle_exec_args *ta, struct obd_export *exp)
1388 {
1389         ta->ta_argno = 0;
1390         ta->ta_handle = dt_trans_create(env, dt);
1391         if (IS_ERR(ta->ta_handle)) {
1392                 int rc;
1393
1394                 rc = PTR_ERR(ta->ta_handle);
1395                 ta->ta_handle = NULL;
1396                 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
1397                        rc);
1398                 return rc;
1399         }
1400         if (exp->exp_need_sync)
1401                 ta->ta_handle->th_sync = 1;
1402
1403         return 0;
1404 }
1405
1406 static int out_trans_start(const struct lu_env *env,
1407                            struct thandle_exec_args *ta)
1408 {
1409         return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
1410 }
1411
1412 static int out_trans_stop(const struct lu_env *env,
1413                           struct thandle_exec_args *ta, int err)
1414 {
1415         int i;
1416         int rc;
1417
1418         ta->ta_handle->th_result = err;
1419         rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
1420         for (i = 0; i < ta->ta_argno; i++) {
1421                 if (ta->ta_args[i]->object != NULL) {
1422                         struct dt_object *obj = ta->ta_args[i]->object;
1423
1424                         /* If the object is being created during this
1425                          * transaction, we need to remove them from the
1426                          * cache immediately, because a few layers are
1427                          * missing in OUT handler, i.e. the object might
1428                          * not be initialized in all layers */
1429                         if (ta->ta_args[i]->exec_fn == out_tx_create_exec)
1430                                 set_bit(LU_OBJECT_HEARD_BANSHEE,
1431                                         &obj->do_lu.lo_header->loh_flags);
1432                         lu_object_put(env, &ta->ta_args[i]->object->do_lu);
1433                         ta->ta_args[i]->object = NULL;
1434                 }
1435         }
1436         ta->ta_handle = NULL;
1437         ta->ta_argno = 0;
1438
1439         return rc;
1440 }
1441
1442 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
1443                       int declare_ret)
1444 {
1445         struct tgt_session_info *tsi = tgt_ses_info(env);
1446         int                     i;
1447         int                     rc;
1448         int                     rc1;
1449         ENTRY;
1450
1451         if (ta->ta_handle == NULL)
1452                 RETURN(0);
1453
1454         if (declare_ret != 0 || ta->ta_argno == 0)
1455                 GOTO(stop, rc = declare_ret);
1456
1457         LASSERT(ta->ta_handle->th_dev != NULL);
1458         rc = out_trans_start(env, ta);
1459         if (unlikely(rc != 0))
1460                 GOTO(stop, rc);
1461
1462         for (i = 0; i < ta->ta_argno; i++) {
1463                 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
1464                                              ta->ta_args[i]);
1465                 if (unlikely(rc != 0)) {
1466                         CDEBUG(D_INFO, "error during execution of #%u from"
1467                                " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
1468                                ta->ta_args[i]->line, rc);
1469                         while (--i >= 0) {
1470                                 if (ta->ta_args[i]->undo_fn != NULL)
1471                                         ta->ta_args[i]->undo_fn(env,
1472                                                                ta->ta_handle,
1473                                                                ta->ta_args[i]);
1474                                 else
1475                                         CERROR("%s: undo for %s:%d: rc = %d\n",
1476                                              dt_obd_name(ta->ta_handle->th_dev),
1477                                                ta->ta_args[i]->file,
1478                                                ta->ta_args[i]->line, -ENOTSUPP);
1479                         }
1480                         break;
1481                 }
1482                 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
1483                        dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
1484         }
1485
1486         /* Only fail for real update */
1487         tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
1488 stop:
1489         rc1 = out_trans_stop(env, ta, rc);
1490         if (rc == 0)
1491                 rc = rc1;
1492
1493         ta->ta_handle = NULL;
1494         ta->ta_argno = 0;
1495
1496         RETURN(rc);
1497 }
1498
1499 /**
1500  * Object updates between Targets. Because all the updates has been
1501  * dis-assemblied into object updates at sender side, so OUT will
1502  * call OSD API directly to execute these updates.
1503  *
1504  * In DNE phase I all of the updates in the request need to be executed
1505  * in one transaction, and the transaction has to be synchronously.
1506  *
1507  * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
1508  * format.
1509  */
1510 int out_handle(struct tgt_session_info *tsi)
1511 {
1512         const struct lu_env             *env = tsi->tsi_env;
1513         struct tgt_thread_info          *tti = tgt_th_info(env);
1514         struct thandle_exec_args        *ta = &tti->tti_tea;
1515         struct req_capsule              *pill = tsi->tsi_pill;
1516         struct dt_device                *dt = tsi->tsi_tgt->lut_bottom;
1517         struct object_update_request    *ureq;
1518         struct object_update            *update;
1519         struct object_update_reply      *reply;
1520         int                              bufsize;
1521         int                              count;
1522         int                              current_batchid = -1;
1523         int                              i;
1524         int                              rc = 0;
1525         int                              rc1 = 0;
1526
1527         ENTRY;
1528
1529         req_capsule_set(pill, &RQF_OUT_UPDATE);
1530         ureq = req_capsule_client_get(pill, &RMF_OUT_UPDATE);
1531         if (ureq == NULL) {
1532                 CERROR("%s: No buf!: rc = %d\n", tgt_name(tsi->tsi_tgt),
1533                        -EPROTO);
1534                 RETURN(err_serious(-EPROTO));
1535         }
1536
1537         bufsize = req_capsule_get_size(pill, &RMF_OUT_UPDATE, RCL_CLIENT);
1538         if (bufsize != object_update_request_size(ureq)) {
1539                 CERROR("%s: invalid bufsize %d: rc = %d\n",
1540                        tgt_name(tsi->tsi_tgt), bufsize, -EPROTO);
1541                 RETURN(err_serious(-EPROTO));
1542         }
1543
1544         if (ureq->ourq_magic != UPDATE_REQUEST_MAGIC) {
1545                 CERROR("%s: invalid update buffer magic %x expect %x: "
1546                        "rc = %d\n", tgt_name(tsi->tsi_tgt), ureq->ourq_magic,
1547                        UPDATE_REQUEST_MAGIC, -EPROTO);
1548                 RETURN(err_serious(-EPROTO));
1549         }
1550
1551         count = ureq->ourq_count;
1552         if (count <= 0) {
1553                 CERROR("%s: empty update: rc = %d\n", tgt_name(tsi->tsi_tgt),
1554                        -EPROTO);
1555                 RETURN(err_serious(-EPROTO));
1556         }
1557
1558         req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1559                              OUT_UPDATE_REPLY_SIZE);
1560         rc = req_capsule_server_pack(pill);
1561         if (rc != 0) {
1562                 CERROR("%s: Can't pack response: rc = %d\n",
1563                        tgt_name(tsi->tsi_tgt), rc);
1564                 RETURN(rc);
1565         }
1566
1567         /* Prepare the update reply buffer */
1568         reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1569         if (reply == NULL)
1570                 RETURN(err_serious(-EPROTO));
1571         object_update_reply_init(reply, count);
1572         tti->tti_u.update.tti_update_reply = reply;
1573         tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1574
1575         /* Walk through updates in the request to execute them synchronously */
1576         for (i = 0; i < count; i++) {
1577                 struct tgt_handler      *h;
1578                 struct dt_object        *dt_obj;
1579
1580                 update = object_update_request_get(ureq, i, NULL);
1581                 if (update == NULL)
1582                         GOTO(out, rc = -EPROTO);
1583
1584                 if (ptlrpc_req_need_swab(pill->rc_req))
1585                         lustre_swab_object_update(update);
1586
1587                 if (!fid_is_sane(&update->ou_fid)) {
1588                         CERROR("%s: invalid FID "DFID": rc = %d\n",
1589                                tgt_name(tsi->tsi_tgt), PFID(&update->ou_fid),
1590                                -EPROTO);
1591                         GOTO(out, rc = err_serious(-EPROTO));
1592                 }
1593
1594                 dt_obj = dt_locate(env, dt, &update->ou_fid);
1595                 if (IS_ERR(dt_obj))
1596                         GOTO(out, rc = PTR_ERR(dt_obj));
1597
1598                 if (dt->dd_record_fid_accessed) {
1599                         lfsck_pack_rfa(&tti->tti_lr,
1600                                        lu_object_fid(&dt_obj->do_lu),
1601                                        LE_FID_ACCESSED,
1602                                        LFSCK_TYPE_LAYOUT);
1603                         tgt_lfsck_in_notify(env, dt, &tti->tti_lr, NULL);
1604                 }
1605
1606                 tti->tti_u.update.tti_dt_object = dt_obj;
1607                 tti->tti_u.update.tti_update = update;
1608                 tti->tti_u.update.tti_update_reply_index = i;
1609
1610                 h = out_handler_find(update->ou_type);
1611                 if (unlikely(h == NULL)) {
1612                         CERROR("%s: unsupported opc: 0x%x\n",
1613                                tgt_name(tsi->tsi_tgt), update->ou_type);
1614                         GOTO(next, rc = -ENOTSUPP);
1615                 }
1616
1617                 /* Check resend case only for modifying RPC */
1618                 if (h->th_flags & MUTABOR) {
1619                         struct ptlrpc_request *req = tgt_ses_req(tsi);
1620
1621                         if (out_check_resent(env, dt, dt_obj, req,
1622                                              out_reconstruct, reply, i))
1623                                 GOTO(next, rc = 0);
1624                 }
1625
1626                 /* start transaction for modification RPC only */
1627                 if (h->th_flags & MUTABOR && current_batchid == -1) {
1628                         current_batchid = update->ou_batchid;
1629                         rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1630                         if (rc != 0)
1631                                 GOTO(next, rc);
1632                 }
1633
1634                 /* Stop the current update transaction, if the update has
1635                  * different batchid, or read-only update */
1636                 if (((current_batchid != update->ou_batchid) ||
1637                      !(h->th_flags & MUTABOR)) && ta->ta_handle != NULL) {
1638                         rc = out_tx_end(env, ta, rc);
1639                         current_batchid = -1;
1640                         if (rc != 0)
1641                                 GOTO(next, rc);
1642
1643                         /* start a new transaction if needed */
1644                         if (h->th_flags & MUTABOR) {
1645                                 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1646                                 if (rc != 0)
1647                                         GOTO(next, rc);
1648
1649                                 current_batchid = update->ou_batchid;
1650                         }
1651                 }
1652
1653                 rc = h->th_act(tsi);
1654 next:
1655                 lu_object_put(env, &dt_obj->do_lu);
1656                 if (rc < 0)
1657                         GOTO(out, rc);
1658         }
1659 out:
1660         if (current_batchid != -1) {
1661                 rc1 = out_tx_end(env, ta, rc);
1662                 if (rc == 0)
1663                         rc = rc1;
1664         }
1665
1666         RETURN(rc);
1667 }
1668
1669 struct tgt_handler tgt_out_handlers[] = {
1670 TGT_UPDATE_HDL(MUTABOR, OUT_UPDATE,     out_handle),
1671 };
1672 EXPORT_SYMBOL(tgt_out_handlers);
1673