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, 2015, 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_INIT_BUFFER_SIZE 4096
38 #define OUT_UPDATE_REPLY_SIZE 4096
39 #define OUT_BULK_BUFFER_SIZE 4096
43 struct object_update_param;
44 struct llog_update_record;
46 static inline size_t update_params_size(const struct update_params *params,
47 unsigned int param_count)
49 struct object_update_param *param;
50 size_t total_size = sizeof(*params);
53 param = (struct object_update_param *)¶ms->up_params[0];
54 for (i = 0; i < param_count; i++) {
55 size_t size = object_update_param_size(param);
57 param = (struct object_update_param *)((char *)param + size);
64 static inline struct object_update_param *
65 update_params_get_param(const struct update_params *params,
66 unsigned int index, unsigned int param_count)
68 struct object_update_param *param;
71 if (index > param_count)
74 param = (struct object_update_param *)¶ms->up_params[0];
75 for (i = 0; i < index; i++)
76 param = (struct object_update_param *)((char *)param +
77 object_update_param_size(param));
83 update_params_get_param_buf(const struct update_params *params, __u16 index,
84 unsigned int param_count, __u16 *size)
86 struct object_update_param *param;
88 param = update_params_get_param(params, (unsigned int)index,
94 *size = param->oup_len;
96 return param->oup_buf;
100 update_op_size(unsigned int param_count)
102 return offsetof(struct update_op, uop_params_off[param_count]);
105 static inline struct update_op *
106 update_op_next_op(const struct update_op *uop)
108 return (struct update_op *)((char *)uop +
109 update_op_size(uop->uop_param_count));
112 static inline size_t update_ops_size(const struct update_ops *ops,
113 unsigned int update_count)
115 struct update_op *op;
116 size_t total_size = sizeof(*ops);
119 op = (struct update_op *)&ops->uops_op[0];
120 for (i = 0; i < update_count; i++, op = update_op_next_op(op))
121 total_size += update_op_size(op->uop_param_count);
126 static inline struct update_params *
127 update_records_get_params(const struct update_records *record)
129 return (struct update_params *)((char *)record +
130 offsetof(struct update_records, ur_ops) +
131 update_ops_size(&record->ur_ops, record->ur_update_count));
135 update_records_size(const struct update_records *record)
138 size_t param_size = 0;
140 if (record->ur_update_count > 0)
141 op_size = update_ops_size(&record->ur_ops,
142 record->ur_update_count);
143 if (record->ur_param_count > 0) {
144 struct update_params *params;
146 params = update_records_get_params(record);
147 param_size = update_params_size(params, record->ur_param_count);
150 return cfs_size_round(offsetof(struct update_records, ur_ops) +
151 op_size + param_size);
155 llog_update_record_size(const struct llog_update_record *lur)
157 return cfs_size_round(sizeof(lur->lur_hdr) +
158 update_records_size(&lur->lur_update_rec) +
159 sizeof(struct llog_rec_tail));
162 static inline struct update_op *
163 update_ops_get_op(const struct update_ops *ops, unsigned int index,
164 unsigned int update_count)
166 struct update_op *op;
169 if (index > update_count)
172 op = (struct update_op *)&ops->uops_op[0];
173 for (i = 0; i < index; i++)
174 op = update_op_next_op(op);
180 *object_update_param_get(const struct object_update *update, size_t index,
183 const struct object_update_param *param;
186 if (index >= update->ou_params_count)
187 return ERR_PTR(-EINVAL);
189 param = &update->ou_params[0];
190 for (i = 0; i < index; i++)
191 param = (struct object_update_param *)((char *)param +
192 object_update_param_size(param));
195 *size = param->oup_len;
197 if (param->oup_len == 0)
198 return ERR_PTR(-ENODATA);
200 return (void *)¶m->oup_buf[0];
203 static inline unsigned long
204 object_update_request_size(const struct object_update_request *our)
209 size = offsetof(struct object_update_request, ourq_updates[0]);
210 for (i = 0; i < our->ourq_count; i++) {
211 struct object_update *update;
213 update = (struct object_update *)((char *)our + size);
214 size += object_update_size(update);
220 object_update_result_insert(struct object_update_reply *reply,
221 void *data, size_t data_len, size_t index,
224 struct object_update_result *update_result;
227 update_result = object_update_result_get(reply, index, NULL);
228 LASSERT(update_result != NULL);
230 update_result->our_rc = ptlrpc_status_hton(rc);
231 if (data != NULL && data_len > 0) {
232 LASSERT(data != NULL);
233 ptr = (char *)update_result +
234 cfs_size_round(sizeof(struct object_update_reply));
235 update_result->our_datalen = data_len;
236 memcpy(ptr, data, data_len);
239 reply->ourp_lens[index] = cfs_size_round(data_len +
240 sizeof(struct object_update_result));
244 object_update_result_data_get(const struct object_update_reply *reply,
245 struct lu_buf *lbuf, size_t index)
247 struct object_update_result *update_result;
251 LASSERT(lbuf != NULL);
252 update_result = object_update_result_get(reply, index, &size);
253 if (update_result == NULL ||
254 size < cfs_size_round(sizeof(struct object_update_reply)) ||
255 update_result->our_datalen > size)
258 result = ptlrpc_status_ntoh(update_result->our_rc);
262 lbuf->lb_buf = update_result->our_data;
263 lbuf->lb_len = update_result->our_datalen;
269 * Attached in the thandle to record the updates for distribute
272 struct thandle_update_records {
273 /* All of updates for the cross-MDT operation, vmalloc'd. */
274 struct llog_update_record *tur_update_records;
275 size_t tur_update_records_buf_size;
277 /* All of parameters for the cross-MDT operation, vmalloc'd */
278 struct update_params *tur_update_params;
279 unsigned int tur_update_param_count;
280 size_t tur_update_params_buf_size;
283 #define TOP_THANDLE_MAGIC 0x20140917
284 struct top_multiple_thandle {
285 struct dt_device *tmt_master_sub_dt;
286 atomic_t tmt_refcount;
287 /* Other sub transactions will be listed here. */
288 struct list_head tmt_sub_thandle_list;
289 spinlock_t tmt_sub_lock;
291 struct list_head tmt_commit_list;
292 /* All of update records will packed here */
293 struct thandle_update_records *tmt_update_records;
295 wait_queue_head_t tmt_stop_waitq;
299 size_t tmt_record_size;
300 __u32 tmt_committed:1;
303 /* {top,sub}_thandle are used to manage distributed transactions which
304 * include updates on several nodes. A top_handle represents the
305 * whole operation, and sub_thandle represents updates on each node. */
307 struct thandle tt_super;
308 /* The master sub transaction. */
309 struct thandle *tt_master_sub_thandle;
311 struct top_multiple_thandle *tt_multiple_thandle;
314 struct sub_thandle_cookie {
315 struct llog_cookie stc_cookie;
316 struct list_head stc_list;
319 /* Sub thandle is used to track multiple sub thandles under one parent
322 struct thandle *st_sub_th;
323 struct dt_device *st_dt;
324 struct list_head st_cookie_list;
325 struct dt_txn_commit_cb st_commit_dcb;
326 struct dt_txn_commit_cb st_stop_dcb;
329 /* linked to top_thandle */
330 struct list_head st_sub_list;
332 /* If this sub thandle is committed */
339 typedef int (*tx_exec_func_t)(const struct lu_env *env, struct thandle *th,
342 /* Structure for holding one update execution */
344 tx_exec_func_t exec_fn;
345 tx_exec_func_t undo_fn;
346 struct dt_object *object;
348 struct object_update_reply *reply;
353 struct dt_insert_rec rec;
354 const struct dt_key *key;
369 struct dt_allocation_hint hint;
370 struct dt_object_format dof;
378 struct ost_body *body;
383 /* Structure for holding all update executations of one transaction */
384 struct thandle_exec_args {
385 struct thandle *ta_handle;
386 int ta_argno; /* used args */
387 int ta_alloc_args; /* allocated args count */
388 struct tx_arg **ta_args;
391 /* target/out_lib.c */
392 int out_update_pack(const struct lu_env *env, struct object_update *update,
393 size_t *max_update_size, enum update_type op,
394 const struct lu_fid *fid, unsigned int params_count,
395 __u16 *param_sizes, const void **param_bufs,
397 int out_create_pack(const struct lu_env *env, struct object_update *update,
398 size_t *max_update_size, const struct lu_fid *fid,
399 const struct lu_attr *attr, struct dt_allocation_hint *hint,
400 struct dt_object_format *dof);
401 int out_object_destroy_pack(const struct lu_env *env,
402 struct object_update *update,
403 size_t *max_update_size,
404 const struct lu_fid *fid);
405 int out_index_delete_pack(const struct lu_env *env,
406 struct object_update *update, size_t *max_update_size,
407 const struct lu_fid *fid, const struct dt_key *key);
408 int out_index_insert_pack(const struct lu_env *env,
409 struct object_update *update, size_t *max_update_size,
410 const struct lu_fid *fid, const struct dt_rec *rec,
411 const struct dt_key *key);
412 int out_xattr_set_pack(const struct lu_env *env,
413 struct object_update *update, size_t *max_update_size,
414 const struct lu_fid *fid, const struct lu_buf *buf,
415 const char *name, __u32 flag);
416 int out_xattr_del_pack(const struct lu_env *env,
417 struct object_update *update, size_t *max_update_size,
418 const struct lu_fid *fid, const char *name);
419 int out_attr_set_pack(const struct lu_env *env,
420 struct object_update *update, size_t *max_update_size,
421 const struct lu_fid *fid, const struct lu_attr *attr);
422 int out_ref_add_pack(const struct lu_env *env,
423 struct object_update *update, size_t *max_update_size,
424 const struct lu_fid *fid);
425 int out_ref_del_pack(const struct lu_env *env,
426 struct object_update *update, size_t *max_update_size,
427 const struct lu_fid *fid);
428 int out_write_pack(const struct lu_env *env,
429 struct object_update *update, size_t *max_update_size,
430 const struct lu_fid *fid, const struct lu_buf *buf,
432 int out_attr_get_pack(const struct lu_env *env,
433 struct object_update *update, size_t *max_update_size,
434 const struct lu_fid *fid);
435 int out_index_lookup_pack(const struct lu_env *env,
436 struct object_update *update, size_t *max_update_size,
437 const struct lu_fid *fid, struct dt_rec *rec,
438 const struct dt_key *key);
439 int out_xattr_get_pack(const struct lu_env *env,
440 struct object_update *update, size_t *max_update_size,
441 const struct lu_fid *fid, const char *name,
443 int out_read_pack(const struct lu_env *env, struct object_update *update,
444 size_t *max_update_length, const struct lu_fid *fid,
445 size_t size, loff_t pos);
447 const char *update_op_str(__u16 opcode);
449 /* target/update_trans.c */
450 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
452 struct dt_device *sub_dt);
454 static inline struct thandle *
455 thandle_get_sub(const struct lu_env *env, struct thandle *th,
456 const struct dt_object *sub_obj)
458 return thandle_get_sub_by_dt(env, th, lu2dt_dev(sub_obj->do_lu.lo_dev));
462 top_trans_create(const struct lu_env *env, struct dt_device *master_dev);
463 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
465 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
467 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt);
469 static inline void top_multiple_thandle_get(struct top_multiple_thandle *tmt)
471 atomic_inc(&tmt->tmt_refcount);
474 static inline void top_multiple_thandle_put(struct top_multiple_thandle *tmt)
476 if (atomic_dec_and_test(&tmt->tmt_refcount))
477 top_multiple_thandle_destroy(tmt);
480 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
481 struct dt_device *dt_dev);
482 int sub_thandle_trans_create(const struct lu_env *env,
483 struct top_thandle *top_th,
484 struct sub_thandle *st);
486 /* update_records.c */
487 size_t update_records_create_size(const struct lu_env *env,
488 const struct lu_fid *fid,
489 const struct lu_attr *attr,
490 const struct dt_allocation_hint *hint,
491 struct dt_object_format *dof);
492 size_t update_records_attr_set_size(const struct lu_env *env,
493 const struct lu_fid *fid,
494 const struct lu_attr *attr);
495 size_t update_records_ref_add_size(const struct lu_env *env,
496 const struct lu_fid *fid);
497 size_t update_records_ref_del_size(const struct lu_env *env,
498 const struct lu_fid *fid);
499 size_t update_records_object_destroy_size(const struct lu_env *env,
500 const struct lu_fid *fid);
501 size_t update_records_index_insert_size(const struct lu_env *env,
502 const struct lu_fid *fid,
503 const struct dt_rec *rec,
504 const struct dt_key *key);
505 size_t update_records_index_delete_size(const struct lu_env *env,
506 const struct lu_fid *fid,
507 const struct dt_key *key);
508 size_t update_records_xattr_set_size(const struct lu_env *env,
509 const struct lu_fid *fid,
510 const struct lu_buf *buf,
513 size_t update_records_xattr_del_size(const struct lu_env *env,
514 const struct lu_fid *fid,
516 size_t update_records_write_size(const struct lu_env *env,
517 const struct lu_fid *fid,
518 const struct lu_buf *buf,
520 size_t update_records_punch_size(const struct lu_env *env,
521 const struct lu_fid *fid,
522 __u64 start, __u64 end);
524 int update_records_create_pack(const struct lu_env *env,
525 struct update_ops *ops,
526 unsigned int *op_count,
527 size_t *max_ops_size,
528 struct update_params *params,
529 unsigned int *param_count,
530 size_t *max_param_size,
531 const struct lu_fid *fid,
532 const struct lu_attr *attr,
533 const struct dt_allocation_hint *hint,
534 struct dt_object_format *dof);
535 int update_records_attr_set_pack(const struct lu_env *env,
536 struct update_ops *ops,
537 unsigned int *op_count,
538 size_t *max_ops_size,
539 struct update_params *params,
540 unsigned int *param_count,
541 size_t *max_param_size,
542 const struct lu_fid *fid,
543 const struct lu_attr *attr);
544 int update_records_ref_add_pack(const struct lu_env *env,
545 struct update_ops *ops,
546 unsigned int *op_count,
547 size_t *max_ops_size,
548 struct update_params *params,
549 unsigned int *param_count,
550 size_t *max_param_size,
551 const struct lu_fid *fid);
552 int update_records_ref_del_pack(const struct lu_env *env,
553 struct update_ops *ops,
554 unsigned int *op_count,
555 size_t *max_ops_size,
556 struct update_params *params,
557 unsigned int *param_count,
558 size_t *max_param_size,
559 const struct lu_fid *fid);
560 int update_records_object_destroy_pack(const struct lu_env *env,
561 struct update_ops *ops,
562 unsigned int *op_count,
563 size_t *max_ops_size,
564 struct update_params *params,
565 unsigned int *param_count,
566 size_t *max_param_size,
567 const struct lu_fid *fid);
568 int update_records_index_insert_pack(const struct lu_env *env,
569 struct update_ops *ops,
570 unsigned int *op_count,
571 size_t *max_ops_size,
572 struct update_params *params,
573 unsigned int *param_count,
574 size_t *max_param_size,
575 const struct lu_fid *fid,
576 const struct dt_rec *rec,
577 const struct dt_key *key);
578 int update_records_index_delete_pack(const struct lu_env *env,
579 struct update_ops *ops,
580 unsigned int *op_count,
581 size_t *max_ops_size,
582 struct update_params *params,
583 unsigned int *param_count,
584 size_t *max_param_size,
585 const struct lu_fid *fid,
586 const struct dt_key *key);
587 int update_records_xattr_set_pack(const struct lu_env *env,
588 struct update_ops *ops,
589 unsigned int *op_count,
590 size_t *max_ops_size,
591 struct update_params *params,
592 unsigned int *param_count,
593 size_t *max_param_size,
594 const struct lu_fid *fid,
595 const struct lu_buf *buf, const char *name,
597 int update_records_xattr_del_pack(const struct lu_env *env,
598 struct update_ops *ops,
599 unsigned int *op_count,
600 size_t *max_ops_size,
601 struct update_params *params,
602 unsigned int *param_count,
603 size_t *max_param_size,
604 const struct lu_fid *fid,
606 int update_records_write_pack(const struct lu_env *env,
607 struct update_ops *ops,
608 unsigned int *op_count,
609 size_t *max_ops_size,
610 struct update_params *params,
611 unsigned int *param_count,
612 size_t *max_param_size,
613 const struct lu_fid *fid,
614 const struct lu_buf *buf,
616 int update_records_punch_pack(const struct lu_env *env,
617 struct update_ops *ops,
618 unsigned int *op_count,
619 size_t *max_ops_size,
620 struct update_params *params,
621 unsigned int *param_count,
622 size_t *max_param_size,
623 const struct lu_fid *fid,
624 __u64 start, __u64 end);
625 int update_records_noop_pack(const struct lu_env *env,
626 struct update_ops *ops,
627 unsigned int *op_count,
628 size_t *max_ops_size,
629 struct update_params *params,
630 unsigned int *param_count,
631 size_t *max_param_size,
632 const struct lu_fid *fid);
634 int tur_update_records_extend(struct thandle_update_records *tur,
636 int tur_update_params_extend(struct thandle_update_records *tur,
638 int tur_update_extend(struct thandle_update_records *tur,
639 size_t new_op_size, size_t new_param_size);
641 #define update_record_pack(name, th, ...) \
643 struct top_thandle *top_th; \
644 struct top_multiple_thandle *tmt; \
645 struct thandle_update_records *tur; \
646 struct llog_update_record *lur; \
647 size_t avail_param_size; \
648 size_t avail_op_size; \
652 top_th = container_of(th, struct top_thandle, tt_super);\
653 tmt = top_th->tt_multiple_thandle; \
654 tur = tmt->tmt_update_records; \
655 lur = tur->tur_update_records; \
656 avail_param_size = tur->tur_update_params_buf_size - \
657 update_params_size(tur->tur_update_params, \
658 tur->tur_update_param_count); \
659 avail_op_size = tur->tur_update_records_buf_size - \
660 llog_update_record_size(lur); \
661 ret = update_records_##name##_pack(env, \
662 &lur->lur_update_rec.ur_ops, \
663 &lur->lur_update_rec.ur_update_count, \
665 tur->tur_update_params, \
666 &tur->tur_update_param_count, \
667 &avail_param_size, __VA_ARGS__); \
668 if (ret == -E2BIG) { \
669 ret = tur_update_extend(tur, avail_op_size, \
681 #define update_record_size(env, name, th, ...) \
683 struct top_thandle *top_th; \
684 struct top_multiple_thandle *tmt; \
686 top_th = container_of(th, struct top_thandle, tt_super); \
688 LASSERT(top_th->tt_multiple_thandle != NULL); \
689 tmt = top_th->tt_multiple_thandle; \
690 tmt->tmt_record_size += \
691 update_records_##name##_size(env, __VA_ARGS__); \