4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.htm
23 * Copyright (c) 2013, 2017, Intel Corporation.
26 * lustre/include/lustre_update.h
28 * Author: Di Wang <di.wang@intel.com>
31 #ifndef _LUSTRE_UPDATE_H
32 #define _LUSTRE_UPDATE_H
33 #include <dt_object.h>
34 #include <lustre_net.h>
35 #include <obj_update.h>
37 #define OUT_UPDATE_REPLY_SIZE 4096
38 #define OUT_BULK_BUFFER_SIZE 4096
42 struct object_update_param;
43 struct llog_update_record;
45 static inline size_t update_params_size(const struct update_params *params,
46 unsigned int param_count)
48 struct object_update_param *param;
49 size_t total_size = sizeof(*params);
52 param = (struct object_update_param *)¶ms->up_params[0];
53 for (i = 0; i < param_count; i++) {
54 size_t size = object_update_param_size(param);
56 param = (struct object_update_param *)((char *)param + size);
63 static inline struct object_update_param *
64 update_params_get_param(const struct update_params *params,
65 unsigned int index, unsigned int param_count)
67 struct object_update_param *param;
70 if (index > param_count)
73 param = (struct object_update_param *)¶ms->up_params[0];
74 for (i = 0; i < index; i++)
75 param = (struct object_update_param *)((char *)param +
76 object_update_param_size(param));
82 update_params_get_param_buf(const struct update_params *params, __u16 index,
83 unsigned int param_count, __u16 *size)
85 struct object_update_param *param;
87 param = update_params_get_param(params, (unsigned int)index,
93 *size = param->oup_len;
95 return param->oup_buf;
99 update_op_size(unsigned int param_count)
101 return offsetof(struct update_op, uop_params_off[param_count]);
104 static inline struct update_op *
105 update_op_next_op(const struct update_op *uop)
107 return (struct update_op *)((char *)uop +
108 update_op_size(uop->uop_param_count));
111 static inline size_t update_ops_size(const struct update_ops *ops,
112 unsigned int update_count)
114 struct update_op *op;
115 size_t total_size = sizeof(*ops);
118 op = (struct update_op *)&ops->uops_op[0];
119 for (i = 0; i < update_count; i++, op = update_op_next_op(op))
120 total_size += update_op_size(op->uop_param_count);
125 static inline struct update_params *
126 update_records_get_params(const struct update_records *record)
128 return (struct update_params *)((char *)record +
129 offsetof(struct update_records, ur_ops) +
130 update_ops_size(&record->ur_ops, record->ur_update_count));
133 static inline struct update_param *
134 update_param_next_param(const struct update_param *param)
136 return (struct update_param *)((char *)param +
137 object_update_param_size(
138 (struct object_update_param *)param));
142 __update_records_size(size_t raw_size)
144 return cfs_size_round(offsetof(struct update_records, ur_ops) +
149 update_records_size(const struct update_records *record)
152 size_t param_size = 0;
154 if (record->ur_update_count > 0)
155 op_size = update_ops_size(&record->ur_ops,
156 record->ur_update_count);
157 if (record->ur_param_count > 0) {
158 struct update_params *params;
160 params = update_records_get_params(record);
161 param_size = update_params_size(params, record->ur_param_count);
164 return __update_records_size(op_size + param_size);
168 __llog_update_record_size(size_t records_size)
170 return cfs_size_round(sizeof(struct llog_rec_hdr) + records_size +
171 sizeof(struct llog_rec_tail));
175 llog_update_record_size(const struct llog_update_record *lur)
177 return __llog_update_record_size(
178 update_records_size(&lur->lur_update_rec));
181 static inline struct update_op *
182 update_ops_get_op(const struct update_ops *ops, unsigned int index,
183 unsigned int update_count)
185 struct update_op *op;
188 if (index > update_count)
191 op = (struct update_op *)&ops->uops_op[0];
192 for (i = 0; i < index; i++)
193 op = update_op_next_op(op);
199 *object_update_param_get(const struct object_update *update, size_t index,
202 const struct object_update_param *param;
205 if (index >= update->ou_params_count)
206 return ERR_PTR(-EINVAL);
208 param = &update->ou_params[0];
209 for (i = 0; i < index; i++)
210 param = (struct object_update_param *)((char *)param +
211 object_update_param_size(param));
214 *size = param->oup_len;
216 if (param->oup_len == 0)
217 return ERR_PTR(-ENODATA);
219 return (void *)¶m->oup_buf[0];
222 static inline unsigned long
223 object_update_request_size(const struct object_update_request *our)
228 size = offsetof(struct object_update_request, ourq_updates[0]);
229 for (i = 0; i < our->ourq_count; i++) {
230 struct object_update *update;
232 update = (struct object_update *)((char *)our + size);
233 size += object_update_size(update);
239 object_update_result_insert(struct object_update_reply *reply,
240 void *data, size_t data_len, size_t index,
243 struct object_update_result *update_result;
245 update_result = object_update_result_get(reply, index, NULL);
246 LASSERT(update_result);
248 update_result->our_rc = ptlrpc_status_hton(rc);
250 if (data_len > 0 && data)
251 memcpy(update_result->our_data, data, data_len);
252 update_result->our_datalen = data_len;
255 reply->ourp_lens[index] = cfs_size_round(data_len +
256 sizeof(struct object_update_result));
260 object_update_result_data_get(const struct object_update_reply *reply,
261 struct lu_buf *lbuf, size_t index)
263 struct object_update_result *update_result;
267 LASSERT(lbuf != NULL);
268 update_result = object_update_result_get(reply, index, &size);
269 if (update_result == NULL ||
270 size < cfs_size_round(sizeof(struct object_update_reply)) ||
271 update_result->our_datalen > size)
274 result = ptlrpc_status_ntoh(update_result->our_rc);
278 lbuf->lb_buf = update_result->our_data;
279 lbuf->lb_len = update_result->our_datalen;
285 * Attached in the thandle to record the updates for distribute
288 struct thandle_update_records {
289 /* All of updates for the cross-MDT operation, vmalloc'd. */
290 struct llog_update_record *tur_update_records;
291 size_t tur_update_records_buf_size;
293 /* All of parameters for the cross-MDT operation, vmalloc'd */
294 struct update_params *tur_update_params;
295 unsigned int tur_update_param_count;
296 size_t tur_update_params_buf_size;
299 #define TOP_THANDLE_MAGIC 0x20140917
300 struct top_multiple_thandle {
301 struct dt_device *tmt_master_sub_dt;
302 atomic_t tmt_refcount;
303 /* Other sub transactions will be listed here. */
304 struct list_head tmt_sub_thandle_list;
305 spinlock_t tmt_sub_lock;
307 struct list_head tmt_commit_list;
308 /* All of update records will packed here */
309 struct thandle_update_records *tmt_update_records;
311 wait_queue_head_t tmt_stop_waitq;
315 size_t tmt_record_size;
316 __u32 tmt_committed:1;
319 /* {top,sub}_thandle are used to manage distributed transactions which
320 * include updates on several nodes. A top_handle represents the
321 * whole operation, and sub_thandle represents updates on each node. */
323 struct thandle tt_super;
324 /* The master sub transaction. */
325 struct thandle *tt_master_sub_thandle;
327 struct top_multiple_thandle *tt_multiple_thandle;
330 struct sub_thandle_cookie {
331 struct llog_cookie stc_cookie;
332 struct list_head stc_list;
335 /* Sub thandle is used to track multiple sub thandles under one parent
338 struct thandle *st_sub_th;
339 struct dt_device *st_dt;
340 struct list_head st_cookie_list;
341 struct dt_txn_commit_cb st_commit_dcb;
342 struct dt_txn_commit_cb st_stop_dcb;
345 /* linked to top_thandle */
346 struct list_head st_sub_list;
348 /* If this sub thandle is committed */
355 typedef int (*tx_exec_func_t)(const struct lu_env *env, struct thandle *th,
358 /* Structure for holding one update execution */
360 tx_exec_func_t exec_fn;
361 tx_exec_func_t undo_fn;
362 struct dt_object *object;
364 struct object_update_reply *reply;
369 struct dt_insert_rec rec;
370 const struct dt_key *key;
385 struct dt_allocation_hint hint;
386 struct dt_object_format dof;
394 struct ost_body *body;
399 /* Structure for holding all update executations of one transaction */
400 struct thandle_exec_args {
401 struct thandle *ta_handle;
402 int ta_argno; /* used args */
403 int ta_alloc_args; /* allocated args count */
404 struct tx_arg **ta_args;
407 /* target/out_lib.c */
408 int out_update_pack(const struct lu_env *env, struct object_update *update,
409 size_t *max_update_size, enum update_type op,
410 const struct lu_fid *fid, unsigned int params_count,
411 __u16 *param_sizes, const void **param_bufs,
413 int out_create_pack(const struct lu_env *env, struct object_update *update,
414 size_t *max_update_size, const struct lu_fid *fid,
415 const struct lu_attr *attr, struct dt_allocation_hint *hint,
416 struct dt_object_format *dof);
417 int out_destroy_pack(const struct lu_env *env, struct object_update *update,
418 size_t *max_update_size, const struct lu_fid *fid);
419 int out_index_delete_pack(const struct lu_env *env,
420 struct object_update *update, size_t *max_update_size,
421 const struct lu_fid *fid, const struct dt_key *key);
422 int out_index_insert_pack(const struct lu_env *env,
423 struct object_update *update, size_t *max_update_size,
424 const struct lu_fid *fid, const struct dt_rec *rec,
425 const struct dt_key *key);
426 int out_xattr_set_pack(const struct lu_env *env,
427 struct object_update *update, size_t *max_update_size,
428 const struct lu_fid *fid, const struct lu_buf *buf,
429 const char *name, __u32 flag);
430 int out_xattr_del_pack(const struct lu_env *env,
431 struct object_update *update, size_t *max_update_size,
432 const struct lu_fid *fid, const char *name);
433 int out_attr_set_pack(const struct lu_env *env,
434 struct object_update *update, size_t *max_update_size,
435 const struct lu_fid *fid, const struct lu_attr *attr);
436 int out_ref_add_pack(const struct lu_env *env,
437 struct object_update *update, size_t *max_update_size,
438 const struct lu_fid *fid);
439 int out_ref_del_pack(const struct lu_env *env,
440 struct object_update *update, size_t *max_update_size,
441 const struct lu_fid *fid);
442 int out_write_pack(const struct lu_env *env,
443 struct object_update *update, size_t *max_update_size,
444 const struct lu_fid *fid, const struct lu_buf *buf,
446 int out_attr_get_pack(const struct lu_env *env,
447 struct object_update *update, size_t *max_update_size,
448 const struct lu_fid *fid);
449 int out_index_lookup_pack(const struct lu_env *env,
450 struct object_update *update, size_t *max_update_size,
451 const struct lu_fid *fid, struct dt_rec *rec,
452 const struct dt_key *key);
453 int out_xattr_get_pack(const struct lu_env *env,
454 struct object_update *update, size_t *max_update_size,
455 const struct lu_fid *fid, const char *name,
457 int out_read_pack(const struct lu_env *env, struct object_update *update,
458 size_t *max_update_length, const struct lu_fid *fid,
459 size_t size, loff_t pos);
461 const char *update_op_str(__u16 opcode);
463 /* target/update_trans.c */
464 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
466 struct dt_device *sub_dt);
468 static inline struct thandle *
469 thandle_get_sub(const struct lu_env *env, struct thandle *th,
470 const struct dt_object *sub_obj)
472 return thandle_get_sub_by_dt(env, th, lu2dt_dev(sub_obj->do_lu.lo_dev));
476 top_trans_create(const struct lu_env *env, struct dt_device *master_dev);
477 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
479 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
481 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt);
483 static inline void top_multiple_thandle_get(struct top_multiple_thandle *tmt)
485 atomic_inc(&tmt->tmt_refcount);
488 static inline void top_multiple_thandle_put(struct top_multiple_thandle *tmt)
490 if (atomic_dec_and_test(&tmt->tmt_refcount))
491 top_multiple_thandle_destroy(tmt);
494 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
495 struct dt_device *dt_dev);
496 int sub_thandle_trans_create(const struct lu_env *env,
497 struct top_thandle *top_th,
498 struct sub_thandle *st);
500 /* update_records.c */
501 size_t update_records_create_size(const struct lu_env *env,
502 const struct lu_fid *fid,
503 const struct lu_attr *attr,
504 const struct dt_allocation_hint *hint,
505 struct dt_object_format *dof);
506 size_t update_records_attr_set_size(const struct lu_env *env,
507 const struct lu_fid *fid,
508 const struct lu_attr *attr);
509 size_t update_records_ref_add_size(const struct lu_env *env,
510 const struct lu_fid *fid);
511 size_t update_records_ref_del_size(const struct lu_env *env,
512 const struct lu_fid *fid);
513 size_t update_records_destroy_size(const struct lu_env *env,
514 const struct lu_fid *fid);
515 size_t update_records_index_insert_size(const struct lu_env *env,
516 const struct lu_fid *fid,
517 const struct dt_rec *rec,
518 const struct dt_key *key);
519 size_t update_records_index_delete_size(const struct lu_env *env,
520 const struct lu_fid *fid,
521 const struct dt_key *key);
522 size_t update_records_xattr_set_size(const struct lu_env *env,
523 const struct lu_fid *fid,
524 const struct lu_buf *buf,
527 size_t update_records_xattr_del_size(const struct lu_env *env,
528 const struct lu_fid *fid,
530 size_t update_records_write_size(const struct lu_env *env,
531 const struct lu_fid *fid,
532 const struct lu_buf *buf,
534 size_t update_records_punch_size(const struct lu_env *env,
535 const struct lu_fid *fid,
536 __u64 start, __u64 end);
538 int update_records_create_pack(const struct lu_env *env,
539 struct update_ops *ops,
540 unsigned int *op_count,
541 size_t *max_ops_size,
542 struct update_params *params,
543 unsigned int *param_count,
544 size_t *max_param_size,
545 const struct lu_fid *fid,
546 const struct lu_attr *attr,
547 const struct dt_allocation_hint *hint,
548 struct dt_object_format *dof);
549 int update_records_attr_set_pack(const struct lu_env *env,
550 struct update_ops *ops,
551 unsigned int *op_count,
552 size_t *max_ops_size,
553 struct update_params *params,
554 unsigned int *param_count,
555 size_t *max_param_size,
556 const struct lu_fid *fid,
557 const struct lu_attr *attr);
558 int update_records_ref_add_pack(const struct lu_env *env,
559 struct update_ops *ops,
560 unsigned int *op_count,
561 size_t *max_ops_size,
562 struct update_params *params,
563 unsigned int *param_count,
564 size_t *max_param_size,
565 const struct lu_fid *fid);
566 int update_records_ref_del_pack(const struct lu_env *env,
567 struct update_ops *ops,
568 unsigned int *op_count,
569 size_t *max_ops_size,
570 struct update_params *params,
571 unsigned int *param_count,
572 size_t *max_param_size,
573 const struct lu_fid *fid);
574 int update_records_destroy_pack(const struct lu_env *env,
575 struct update_ops *ops, unsigned int *op_count,
576 size_t *max_ops_size,
577 struct update_params *params,
578 unsigned int *param_count,
579 size_t *max_param_size,
580 const struct lu_fid *fid);
581 int update_records_index_insert_pack(const struct lu_env *env,
582 struct update_ops *ops,
583 unsigned int *op_count,
584 size_t *max_ops_size,
585 struct update_params *params,
586 unsigned int *param_count,
587 size_t *max_param_size,
588 const struct lu_fid *fid,
589 const struct dt_rec *rec,
590 const struct dt_key *key);
591 int update_records_index_delete_pack(const struct lu_env *env,
592 struct update_ops *ops,
593 unsigned int *op_count,
594 size_t *max_ops_size,
595 struct update_params *params,
596 unsigned int *param_count,
597 size_t *max_param_size,
598 const struct lu_fid *fid,
599 const struct dt_key *key);
600 int update_records_xattr_set_pack(const struct lu_env *env,
601 struct update_ops *ops,
602 unsigned int *op_count,
603 size_t *max_ops_size,
604 struct update_params *params,
605 unsigned int *param_count,
606 size_t *max_param_size,
607 const struct lu_fid *fid,
608 const struct lu_buf *buf, const char *name,
610 int update_records_xattr_del_pack(const struct lu_env *env,
611 struct update_ops *ops,
612 unsigned int *op_count,
613 size_t *max_ops_size,
614 struct update_params *params,
615 unsigned int *param_count,
616 size_t *max_param_size,
617 const struct lu_fid *fid,
619 int update_records_write_pack(const struct lu_env *env,
620 struct update_ops *ops,
621 unsigned int *op_count,
622 size_t *max_ops_size,
623 struct update_params *params,
624 unsigned int *param_count,
625 size_t *max_param_size,
626 const struct lu_fid *fid,
627 const struct lu_buf *buf,
629 int update_records_punch_pack(const struct lu_env *env,
630 struct update_ops *ops,
631 unsigned int *op_count,
632 size_t *max_ops_size,
633 struct update_params *params,
634 unsigned int *param_count,
635 size_t *max_param_size,
636 const struct lu_fid *fid,
637 __u64 start, __u64 end);
638 int update_records_noop_pack(const struct lu_env *env,
639 struct update_ops *ops,
640 unsigned int *op_count,
641 size_t *max_ops_size,
642 struct update_params *params,
643 unsigned int *param_count,
644 size_t *max_param_size,
645 const struct lu_fid *fid);
647 int tur_update_records_extend(struct thandle_update_records *tur,
649 int tur_update_params_extend(struct thandle_update_records *tur,
651 int tur_update_extend(struct thandle_update_records *tur,
652 size_t new_op_size, size_t new_param_size);
654 #define update_record_pack(name, th, ...) \
656 struct top_thandle *top_th; \
657 struct top_multiple_thandle *tmt; \
658 struct thandle_update_records *tur; \
659 struct llog_update_record *lur; \
660 size_t avail_param_size; \
661 size_t avail_op_size; \
665 top_th = container_of(th, struct top_thandle, tt_super);\
666 tmt = top_th->tt_multiple_thandle; \
667 tur = tmt->tmt_update_records; \
668 lur = tur->tur_update_records; \
669 avail_param_size = tur->tur_update_params_buf_size - \
670 update_params_size(tur->tur_update_params, \
671 tur->tur_update_param_count); \
672 avail_op_size = tur->tur_update_records_buf_size - \
673 llog_update_record_size(lur); \
674 ret = update_records_##name##_pack(env, \
675 &lur->lur_update_rec.ur_ops, \
676 &lur->lur_update_rec.ur_update_count, \
678 tur->tur_update_params, \
679 &tur->tur_update_param_count, \
680 &avail_param_size, __VA_ARGS__); \
681 if (ret == -E2BIG) { \
682 ret = tur_update_extend(tur, avail_op_size, \
694 #define update_record_size(env, name, th, ...) \
696 struct top_thandle *top_th; \
697 struct top_multiple_thandle *tmt; \
699 top_th = container_of(th, struct top_thandle, tt_super); \
701 LASSERT(top_th->tt_multiple_thandle != NULL); \
702 tmt = top_th->tt_multiple_thandle; \
703 tmt->tmt_record_size += \
704 update_records_##name##_size(env, __VA_ARGS__); \