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, 2014, 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 <lustre_net.h>
34 #include <dt_object.h>
36 #define OUT_UPDATE_INIT_BUFFER_SIZE 4096
37 /* 16KB, the current biggest size is llog header(8KB) */
38 #define OUT_UPDATE_REPLY_SIZE 16384
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));
134 update_records_size(const struct update_records *record)
137 size_t param_size = 0;
139 if (record->ur_update_count > 0)
140 op_size = update_ops_size(&record->ur_ops,
141 record->ur_update_count);
142 if (record->ur_param_count > 0) {
143 struct update_params *params;
145 params = update_records_get_params(record);
146 param_size = update_params_size(params, record->ur_param_count);
149 return cfs_size_round(offsetof(struct update_records, ur_ops) +
150 op_size + param_size);
154 llog_update_record_size(const struct llog_update_record *lur)
156 return cfs_size_round(sizeof(lur->lur_hdr) +
157 update_records_size(&lur->lur_update_rec) +
158 sizeof(struct llog_rec_tail));
161 static inline struct update_op *
162 update_ops_get_op(const struct update_ops *ops, unsigned int index,
163 unsigned int update_count)
165 struct update_op *op;
168 if (index > update_count)
171 op = (struct update_op *)&ops->uops_op[0];
172 for (i = 0; i < index; i++)
173 op = update_op_next_op(op);
179 *object_update_param_get(const struct object_update *update, size_t index,
182 const struct object_update_param *param;
185 if (index >= update->ou_params_count)
186 return ERR_PTR(-EINVAL);
188 param = &update->ou_params[0];
189 for (i = 0; i < index; i++)
190 param = (struct object_update_param *)((char *)param +
191 object_update_param_size(param));
194 *size = param->oup_len;
196 if (param->oup_len == 0)
199 return (void *)¶m->oup_buf[0];
202 static inline unsigned long
203 object_update_request_size(const struct object_update_request *our)
208 size = offsetof(struct object_update_request, ourq_updates[0]);
209 for (i = 0; i < our->ourq_count; i++) {
210 struct object_update *update;
212 update = (struct object_update *)((char *)our + size);
213 size += object_update_size(update);
219 object_update_result_insert(struct object_update_reply *reply,
220 void *data, size_t data_len, size_t index,
223 struct object_update_result *update_result;
226 update_result = object_update_result_get(reply, index, NULL);
227 LASSERT(update_result != NULL);
229 update_result->our_rc = ptlrpc_status_hton(rc);
231 LASSERT(data != NULL);
232 ptr = (char *)update_result +
233 cfs_size_round(sizeof(struct object_update_reply));
234 update_result->our_datalen = data_len;
235 memcpy(ptr, data, data_len);
238 reply->ourp_lens[index] = cfs_size_round(data_len +
239 sizeof(struct object_update_result));
243 object_update_result_data_get(const struct object_update_reply *reply,
244 struct lu_buf *lbuf, size_t index)
246 struct object_update_result *update_result;
250 LASSERT(lbuf != NULL);
251 update_result = object_update_result_get(reply, index, &size);
252 if (update_result == NULL ||
253 size < cfs_size_round(sizeof(struct object_update_reply)) ||
254 update_result->our_datalen > size)
257 result = ptlrpc_status_ntoh(update_result->our_rc);
261 lbuf->lb_buf = update_result->our_data;
262 lbuf->lb_len = update_result->our_datalen;
268 * Attached in the thandle to record the updates for distribute
271 struct thandle_update_records {
272 /* All of updates for the cross-MDT operation. */
273 struct llog_update_record *tur_update_records;
274 size_t tur_update_records_buf_size;
276 /* All of parameters for the cross-MDT operation */
277 struct update_params *tur_update_params;
278 unsigned int tur_update_param_count;
279 size_t tur_update_params_buf_size;
282 #define TOP_THANDLE_MAGIC 0x20140917
283 struct top_multiple_thandle {
284 struct dt_device *tmt_master_sub_dt;
285 atomic_t tmt_refcount;
286 /* Other sub transactions will be listed here. */
287 struct list_head tmt_sub_thandle_list;
289 struct list_head tmt_commit_list;
290 /* All of update records will packed here */
291 struct thandle_update_records *tmt_update_records;
293 wait_queue_head_t tmt_stop_waitq;
297 size_t tmt_record_size;
298 __u32 tmt_committed:1;
301 /* {top,sub}_thandle are used to manage distributed transactions which
302 * include updates on several nodes. A top_handle represents the
303 * whole operation, and sub_thandle represents updates on each node. */
305 struct thandle tt_super;
306 /* The master sub transaction. */
307 struct thandle *tt_master_sub_thandle;
309 struct top_multiple_thandle *tt_multiple_thandle;
312 struct sub_thandle_cookie {
313 struct llog_cookie stc_cookie;
314 struct list_head stc_list;
317 /* Sub thandle is used to track multiple sub thandles under one parent
320 struct thandle *st_sub_th;
321 struct dt_device *st_dt;
322 struct list_head st_cookie_list;
323 struct dt_txn_commit_cb st_commit_dcb;
324 struct dt_txn_commit_cb st_stop_dcb;
327 /* linked to top_thandle */
328 struct list_head st_sub_list;
330 /* If this sub thandle is committed */
336 typedef int (*tx_exec_func_t)(const struct lu_env *env, struct thandle *th,
339 /* Structure for holding one update execution */
341 tx_exec_func_t exec_fn;
342 tx_exec_func_t undo_fn;
343 struct dt_object *object;
345 struct object_update_reply *reply;
350 struct dt_insert_rec rec;
351 const struct dt_key *key;
366 struct dt_allocation_hint hint;
367 struct dt_object_format dof;
375 struct ost_body *body;
380 /* Structure for holding all update executations of one transaction */
381 struct thandle_exec_args {
382 struct thandle *ta_handle;
383 int ta_argno; /* used args */
384 int ta_alloc_args; /* allocated args count */
385 struct tx_arg **ta_args;
388 /* target/out_lib.c */
389 int out_update_pack(const struct lu_env *env, struct object_update *update,
390 size_t *max_update_size, enum update_type op,
391 const struct lu_fid *fid, unsigned int params_count,
392 __u16 *param_sizes, const void **param_bufs);
393 int out_create_pack(const struct lu_env *env, struct object_update *update,
394 size_t *max_update_size, const struct lu_fid *fid,
395 const struct lu_attr *attr, struct dt_allocation_hint *hint,
396 struct dt_object_format *dof);
397 int out_object_destroy_pack(const struct lu_env *env,
398 struct object_update *update,
399 size_t *max_update_size,
400 const struct lu_fid *fid);
401 int out_index_delete_pack(const struct lu_env *env,
402 struct object_update *update, size_t *max_update_size,
403 const struct lu_fid *fid, const struct dt_key *key);
404 int out_index_insert_pack(const struct lu_env *env,
405 struct object_update *update, size_t *max_update_size,
406 const struct lu_fid *fid, const struct dt_rec *rec,
407 const struct dt_key *key);
408 int out_xattr_set_pack(const struct lu_env *env,
409 struct object_update *update, size_t *max_update_size,
410 const struct lu_fid *fid, const struct lu_buf *buf,
411 const char *name, __u32 flag);
412 int out_xattr_del_pack(const struct lu_env *env,
413 struct object_update *update, size_t *max_update_size,
414 const struct lu_fid *fid, const char *name);
415 int out_attr_set_pack(const struct lu_env *env,
416 struct object_update *update, size_t *max_update_size,
417 const struct lu_fid *fid, const struct lu_attr *attr);
418 int out_ref_add_pack(const struct lu_env *env,
419 struct object_update *update, size_t *max_update_size,
420 const struct lu_fid *fid);
421 int out_ref_del_pack(const struct lu_env *env,
422 struct object_update *update, size_t *max_update_size,
423 const struct lu_fid *fid);
424 int out_write_pack(const struct lu_env *env,
425 struct object_update *update, size_t *max_update_size,
426 const struct lu_fid *fid, const struct lu_buf *buf,
428 int out_attr_get_pack(const struct lu_env *env,
429 struct object_update *update, size_t *max_update_size,
430 const struct lu_fid *fid);
431 int out_index_lookup_pack(const struct lu_env *env,
432 struct object_update *update, size_t *max_update_size,
433 const struct lu_fid *fid, struct dt_rec *rec,
434 const struct dt_key *key);
435 int out_xattr_get_pack(const struct lu_env *env,
436 struct object_update *update, size_t *max_update_size,
437 const struct lu_fid *fid, const char *name);
438 int out_read_pack(const struct lu_env *env, struct object_update *update,
439 size_t *max_update_length, const struct lu_fid *fid,
440 size_t size, loff_t pos);
442 const char *update_op_str(__u16 opcode);
444 /* target/update_trans.c */
445 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
447 struct dt_device *sub_dt);
449 static inline struct thandle *
450 thandle_get_sub(const struct lu_env *env, struct thandle *th,
451 const struct dt_object *sub_obj)
453 return thandle_get_sub_by_dt(env, th, lu2dt_dev(sub_obj->do_lu.lo_dev));
457 top_trans_create(const struct lu_env *env, struct dt_device *master_dev);
458 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
460 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
462 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt);
464 static inline void top_multiple_thandle_get(struct top_multiple_thandle *tmt)
466 atomic_inc(&tmt->tmt_refcount);
469 static inline void top_multiple_thandle_put(struct top_multiple_thandle *tmt)
471 if (atomic_dec_and_test(&tmt->tmt_refcount))
472 top_multiple_thandle_destroy(tmt);
475 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
476 struct dt_device *dt_dev);
477 int sub_thandle_trans_create(const struct lu_env *env,
478 struct top_thandle *top_th,
479 struct sub_thandle *st);
481 /* update_records.c */
482 size_t update_records_create_size(const struct lu_env *env,
483 const struct lu_fid *fid,
484 const struct lu_attr *attr,
485 const struct dt_allocation_hint *hint,
486 struct dt_object_format *dof);
487 size_t update_records_attr_set_size(const struct lu_env *env,
488 const struct lu_fid *fid,
489 const struct lu_attr *attr);
490 size_t update_records_ref_add_size(const struct lu_env *env,
491 const struct lu_fid *fid);
492 size_t update_records_ref_del_size(const struct lu_env *env,
493 const struct lu_fid *fid);
494 size_t update_records_object_destroy_size(const struct lu_env *env,
495 const struct lu_fid *fid);
496 size_t update_records_index_insert_size(const struct lu_env *env,
497 const struct lu_fid *fid,
498 const struct dt_rec *rec,
499 const struct dt_key *key);
500 size_t update_records_index_delete_size(const struct lu_env *env,
501 const struct lu_fid *fid,
502 const struct dt_key *key);
503 size_t update_records_xattr_set_size(const struct lu_env *env,
504 const struct lu_fid *fid,
505 const struct lu_buf *buf,
508 size_t update_records_xattr_del_size(const struct lu_env *env,
509 const struct lu_fid *fid,
511 size_t update_records_write_size(const struct lu_env *env,
512 const struct lu_fid *fid,
513 const struct lu_buf *buf,
515 size_t update_records_punch_size(const struct lu_env *env,
516 const struct lu_fid *fid,
517 __u64 start, __u64 end);
519 int update_records_create_pack(const struct lu_env *env,
520 struct update_ops *ops,
521 unsigned int *op_count,
522 size_t *max_ops_size,
523 struct update_params *params,
524 unsigned int *param_count,
525 size_t *max_param_size,
526 const struct lu_fid *fid,
527 const struct lu_attr *attr,
528 const struct dt_allocation_hint *hint,
529 struct dt_object_format *dof);
530 int update_records_attr_set_pack(const struct lu_env *env,
531 struct update_ops *ops,
532 unsigned int *op_count,
533 size_t *max_ops_size,
534 struct update_params *params,
535 unsigned int *param_count,
536 size_t *max_param_size,
537 const struct lu_fid *fid,
538 const struct lu_attr *attr);
539 int update_records_ref_add_pack(const struct lu_env *env,
540 struct update_ops *ops,
541 unsigned int *op_count,
542 size_t *max_ops_size,
543 struct update_params *params,
544 unsigned int *param_count,
545 size_t *max_param_size,
546 const struct lu_fid *fid);
547 int update_records_ref_del_pack(const struct lu_env *env,
548 struct update_ops *ops,
549 unsigned int *op_count,
550 size_t *max_ops_size,
551 struct update_params *params,
552 unsigned int *param_count,
553 size_t *max_param_size,
554 const struct lu_fid *fid);
555 int update_records_object_destroy_pack(const struct lu_env *env,
556 struct update_ops *ops,
557 unsigned int *op_count,
558 size_t *max_ops_size,
559 struct update_params *params,
560 unsigned int *param_count,
561 size_t *max_param_size,
562 const struct lu_fid *fid);
563 int update_records_index_insert_pack(const struct lu_env *env,
564 struct update_ops *ops,
565 unsigned int *op_count,
566 size_t *max_ops_size,
567 struct update_params *params,
568 unsigned int *param_count,
569 size_t *max_param_size,
570 const struct lu_fid *fid,
571 const struct dt_rec *rec,
572 const struct dt_key *key);
573 int update_records_index_delete_pack(const struct lu_env *env,
574 struct update_ops *ops,
575 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 const struct dt_key *key);
582 int update_records_xattr_set_pack(const struct lu_env *env,
583 struct update_ops *ops,
584 unsigned int *op_count,
585 size_t *max_ops_size,
586 struct update_params *params,
587 unsigned int *param_count,
588 size_t *max_param_size,
589 const struct lu_fid *fid,
590 const struct lu_buf *buf, const char *name,
592 int update_records_xattr_del_pack(const struct lu_env *env,
593 struct update_ops *ops,
594 unsigned int *op_count,
595 size_t *max_ops_size,
596 struct update_params *params,
597 unsigned int *param_count,
598 size_t *max_param_size,
599 const struct lu_fid *fid,
601 int update_records_write_pack(const struct lu_env *env,
602 struct update_ops *ops,
603 unsigned int *op_count,
604 size_t *max_ops_size,
605 struct update_params *params,
606 unsigned int *param_count,
607 size_t *max_param_size,
608 const struct lu_fid *fid,
609 const struct lu_buf *buf,
611 int update_records_punch_pack(const struct lu_env *env,
612 struct update_ops *ops,
613 unsigned int *op_count,
614 size_t *max_ops_size,
615 struct update_params *params,
616 unsigned int *param_count,
617 size_t *max_param_size,
618 const struct lu_fid *fid,
619 __u64 start, __u64 end);
620 int update_records_noop_pack(const struct lu_env *env,
621 struct update_ops *ops,
622 unsigned int *op_count,
623 size_t *max_ops_size,
624 struct update_params *params,
625 unsigned int *param_count,
626 size_t *max_param_size,
627 const struct lu_fid *fid);
629 int tur_update_records_extend(struct thandle_update_records *tur,
631 int tur_update_params_extend(struct thandle_update_records *tur,
633 int tur_update_extend(struct thandle_update_records *tur,
634 size_t new_op_size, size_t new_param_size);
636 #define update_record_pack(name, th, ...) \
638 struct top_thandle *top_th; \
639 struct top_multiple_thandle *tmt; \
640 struct thandle_update_records *tur; \
641 struct llog_update_record *lur; \
642 size_t avail_param_size; \
643 size_t avail_op_size; \
647 top_th = container_of(th, struct top_thandle, tt_super);\
648 tmt = top_th->tt_multiple_thandle; \
649 tur = tmt->tmt_update_records; \
650 lur = tur->tur_update_records; \
651 avail_param_size = tur->tur_update_params_buf_size - \
652 update_params_size(tur->tur_update_params, \
653 tur->tur_update_param_count); \
654 avail_op_size = tur->tur_update_records_buf_size - \
655 llog_update_record_size(lur); \
656 ret = update_records_##name##_pack(env, \
657 &lur->lur_update_rec.ur_ops, \
658 &lur->lur_update_rec.ur_update_count, \
660 tur->tur_update_params, \
661 &tur->tur_update_param_count, \
662 &avail_param_size, __VA_ARGS__); \
663 if (ret == -E2BIG) { \
664 ret = tur_update_extend(tur, avail_op_size, \
676 #define update_record_size(env, name, th, ...) \
678 struct top_thandle *top_th; \
679 struct top_multiple_thandle *tmt; \
681 top_th = container_of(th, struct top_thandle, tt_super); \
683 LASSERT(top_th->tt_multiple_thandle != NULL); \
684 tmt = top_th->tt_multiple_thandle; \
685 tmt->tmt_record_size += \
686 update_records_##name##_size(env, __VA_ARGS__); \