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 <lustre_net.h>
34 #include <dt_object.h>
36 #define OUT_UPDATE_INIT_BUFFER_SIZE 4096
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));
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)
197 return ERR_PTR(-ENODATA);
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);
230 if (data != NULL && data_len > 0) {
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;
288 spinlock_t tmt_sub_lock;
290 struct list_head tmt_commit_list;
291 /* All of update records will packed here */
292 struct thandle_update_records *tmt_update_records;
294 wait_queue_head_t tmt_stop_waitq;
298 size_t tmt_record_size;
299 __u32 tmt_committed:1;
302 /* {top,sub}_thandle are used to manage distributed transactions which
303 * include updates on several nodes. A top_handle represents the
304 * whole operation, and sub_thandle represents updates on each node. */
306 struct thandle tt_super;
307 /* The master sub transaction. */
308 struct thandle *tt_master_sub_thandle;
310 struct top_multiple_thandle *tt_multiple_thandle;
313 struct sub_thandle_cookie {
314 struct llog_cookie stc_cookie;
315 struct list_head stc_list;
318 /* Sub thandle is used to track multiple sub thandles under one parent
321 struct thandle *st_sub_th;
322 struct dt_device *st_dt;
323 struct list_head st_cookie_list;
324 struct dt_txn_commit_cb st_commit_dcb;
325 struct dt_txn_commit_cb st_stop_dcb;
328 /* linked to top_thandle */
329 struct list_head st_sub_list;
331 /* If this sub thandle is committed */
337 typedef int (*tx_exec_func_t)(const struct lu_env *env, struct thandle *th,
340 /* Structure for holding one update execution */
342 tx_exec_func_t exec_fn;
343 tx_exec_func_t undo_fn;
344 struct dt_object *object;
346 struct object_update_reply *reply;
351 struct dt_insert_rec rec;
352 const struct dt_key *key;
367 struct dt_allocation_hint hint;
368 struct dt_object_format dof;
376 struct ost_body *body;
381 /* Structure for holding all update executations of one transaction */
382 struct thandle_exec_args {
383 struct thandle *ta_handle;
384 int ta_argno; /* used args */
385 int ta_alloc_args; /* allocated args count */
386 struct tx_arg **ta_args;
389 /* target/out_lib.c */
390 int out_update_pack(const struct lu_env *env, struct object_update *update,
391 size_t *max_update_size, enum update_type op,
392 const struct lu_fid *fid, unsigned int params_count,
393 __u16 *param_sizes, const void **param_bufs,
395 int out_create_pack(const struct lu_env *env, struct object_update *update,
396 size_t *max_update_size, const struct lu_fid *fid,
397 const struct lu_attr *attr, struct dt_allocation_hint *hint,
398 struct dt_object_format *dof);
399 int out_object_destroy_pack(const struct lu_env *env,
400 struct object_update *update,
401 size_t *max_update_size,
402 const struct lu_fid *fid);
403 int out_index_delete_pack(const struct lu_env *env,
404 struct object_update *update, size_t *max_update_size,
405 const struct lu_fid *fid, const struct dt_key *key);
406 int out_index_insert_pack(const struct lu_env *env,
407 struct object_update *update, size_t *max_update_size,
408 const struct lu_fid *fid, const struct dt_rec *rec,
409 const struct dt_key *key);
410 int out_xattr_set_pack(const struct lu_env *env,
411 struct object_update *update, size_t *max_update_size,
412 const struct lu_fid *fid, const struct lu_buf *buf,
413 const char *name, __u32 flag);
414 int out_xattr_del_pack(const struct lu_env *env,
415 struct object_update *update, size_t *max_update_size,
416 const struct lu_fid *fid, const char *name);
417 int out_attr_set_pack(const struct lu_env *env,
418 struct object_update *update, size_t *max_update_size,
419 const struct lu_fid *fid, const struct lu_attr *attr);
420 int out_ref_add_pack(const struct lu_env *env,
421 struct object_update *update, size_t *max_update_size,
422 const struct lu_fid *fid);
423 int out_ref_del_pack(const struct lu_env *env,
424 struct object_update *update, size_t *max_update_size,
425 const struct lu_fid *fid);
426 int out_write_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,
430 int out_attr_get_pack(const struct lu_env *env,
431 struct object_update *update, size_t *max_update_size,
432 const struct lu_fid *fid);
433 int out_index_lookup_pack(const struct lu_env *env,
434 struct object_update *update, size_t *max_update_size,
435 const struct lu_fid *fid, struct dt_rec *rec,
436 const struct dt_key *key);
437 int out_xattr_get_pack(const struct lu_env *env,
438 struct object_update *update, size_t *max_update_size,
439 const struct lu_fid *fid, const char *name,
441 int out_read_pack(const struct lu_env *env, struct object_update *update,
442 size_t *max_update_length, const struct lu_fid *fid,
443 size_t size, loff_t pos);
445 const char *update_op_str(__u16 opcode);
447 /* target/update_trans.c */
448 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
450 struct dt_device *sub_dt);
452 static inline struct thandle *
453 thandle_get_sub(const struct lu_env *env, struct thandle *th,
454 const struct dt_object *sub_obj)
456 return thandle_get_sub_by_dt(env, th, lu2dt_dev(sub_obj->do_lu.lo_dev));
460 top_trans_create(const struct lu_env *env, struct dt_device *master_dev);
461 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
463 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
465 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt);
467 static inline void top_multiple_thandle_get(struct top_multiple_thandle *tmt)
469 atomic_inc(&tmt->tmt_refcount);
472 static inline void top_multiple_thandle_put(struct top_multiple_thandle *tmt)
474 if (atomic_dec_and_test(&tmt->tmt_refcount))
475 top_multiple_thandle_destroy(tmt);
478 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
479 struct dt_device *dt_dev);
480 int sub_thandle_trans_create(const struct lu_env *env,
481 struct top_thandle *top_th,
482 struct sub_thandle *st);
484 /* update_records.c */
485 size_t update_records_create_size(const struct lu_env *env,
486 const struct lu_fid *fid,
487 const struct lu_attr *attr,
488 const struct dt_allocation_hint *hint,
489 struct dt_object_format *dof);
490 size_t update_records_attr_set_size(const struct lu_env *env,
491 const struct lu_fid *fid,
492 const struct lu_attr *attr);
493 size_t update_records_ref_add_size(const struct lu_env *env,
494 const struct lu_fid *fid);
495 size_t update_records_ref_del_size(const struct lu_env *env,
496 const struct lu_fid *fid);
497 size_t update_records_object_destroy_size(const struct lu_env *env,
498 const struct lu_fid *fid);
499 size_t update_records_index_insert_size(const struct lu_env *env,
500 const struct lu_fid *fid,
501 const struct dt_rec *rec,
502 const struct dt_key *key);
503 size_t update_records_index_delete_size(const struct lu_env *env,
504 const struct lu_fid *fid,
505 const struct dt_key *key);
506 size_t update_records_xattr_set_size(const struct lu_env *env,
507 const struct lu_fid *fid,
508 const struct lu_buf *buf,
511 size_t update_records_xattr_del_size(const struct lu_env *env,
512 const struct lu_fid *fid,
514 size_t update_records_write_size(const struct lu_env *env,
515 const struct lu_fid *fid,
516 const struct lu_buf *buf,
518 size_t update_records_punch_size(const struct lu_env *env,
519 const struct lu_fid *fid,
520 __u64 start, __u64 end);
522 int update_records_create_pack(const struct lu_env *env,
523 struct update_ops *ops,
524 unsigned int *op_count,
525 size_t *max_ops_size,
526 struct update_params *params,
527 unsigned int *param_count,
528 size_t *max_param_size,
529 const struct lu_fid *fid,
530 const struct lu_attr *attr,
531 const struct dt_allocation_hint *hint,
532 struct dt_object_format *dof);
533 int update_records_attr_set_pack(const struct lu_env *env,
534 struct update_ops *ops,
535 unsigned int *op_count,
536 size_t *max_ops_size,
537 struct update_params *params,
538 unsigned int *param_count,
539 size_t *max_param_size,
540 const struct lu_fid *fid,
541 const struct lu_attr *attr);
542 int update_records_ref_add_pack(const struct lu_env *env,
543 struct update_ops *ops,
544 unsigned int *op_count,
545 size_t *max_ops_size,
546 struct update_params *params,
547 unsigned int *param_count,
548 size_t *max_param_size,
549 const struct lu_fid *fid);
550 int update_records_ref_del_pack(const struct lu_env *env,
551 struct update_ops *ops,
552 unsigned int *op_count,
553 size_t *max_ops_size,
554 struct update_params *params,
555 unsigned int *param_count,
556 size_t *max_param_size,
557 const struct lu_fid *fid);
558 int update_records_object_destroy_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_index_insert_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 const struct dt_rec *rec,
575 const struct dt_key *key);
576 int update_records_index_delete_pack(const struct lu_env *env,
577 struct update_ops *ops,
578 unsigned int *op_count,
579 size_t *max_ops_size,
580 struct update_params *params,
581 unsigned int *param_count,
582 size_t *max_param_size,
583 const struct lu_fid *fid,
584 const struct dt_key *key);
585 int update_records_xattr_set_pack(const struct lu_env *env,
586 struct update_ops *ops,
587 unsigned int *op_count,
588 size_t *max_ops_size,
589 struct update_params *params,
590 unsigned int *param_count,
591 size_t *max_param_size,
592 const struct lu_fid *fid,
593 const struct lu_buf *buf, const char *name,
595 int update_records_xattr_del_pack(const struct lu_env *env,
596 struct update_ops *ops,
597 unsigned int *op_count,
598 size_t *max_ops_size,
599 struct update_params *params,
600 unsigned int *param_count,
601 size_t *max_param_size,
602 const struct lu_fid *fid,
604 int update_records_write_pack(const struct lu_env *env,
605 struct update_ops *ops,
606 unsigned int *op_count,
607 size_t *max_ops_size,
608 struct update_params *params,
609 unsigned int *param_count,
610 size_t *max_param_size,
611 const struct lu_fid *fid,
612 const struct lu_buf *buf,
614 int update_records_punch_pack(const struct lu_env *env,
615 struct update_ops *ops,
616 unsigned int *op_count,
617 size_t *max_ops_size,
618 struct update_params *params,
619 unsigned int *param_count,
620 size_t *max_param_size,
621 const struct lu_fid *fid,
622 __u64 start, __u64 end);
623 int update_records_noop_pack(const struct lu_env *env,
624 struct update_ops *ops,
625 unsigned int *op_count,
626 size_t *max_ops_size,
627 struct update_params *params,
628 unsigned int *param_count,
629 size_t *max_param_size,
630 const struct lu_fid *fid);
632 int tur_update_records_extend(struct thandle_update_records *tur,
634 int tur_update_params_extend(struct thandle_update_records *tur,
636 int tur_update_extend(struct thandle_update_records *tur,
637 size_t new_op_size, size_t new_param_size);
639 #define update_record_pack(name, th, ...) \
641 struct top_thandle *top_th; \
642 struct top_multiple_thandle *tmt; \
643 struct thandle_update_records *tur; \
644 struct llog_update_record *lur; \
645 size_t avail_param_size; \
646 size_t avail_op_size; \
650 top_th = container_of(th, struct top_thandle, tt_super);\
651 tmt = top_th->tt_multiple_thandle; \
652 tur = tmt->tmt_update_records; \
653 lur = tur->tur_update_records; \
654 avail_param_size = tur->tur_update_params_buf_size - \
655 update_params_size(tur->tur_update_params, \
656 tur->tur_update_param_count); \
657 avail_op_size = tur->tur_update_records_buf_size - \
658 llog_update_record_size(lur); \
659 ret = update_records_##name##_pack(env, \
660 &lur->lur_update_rec.ur_ops, \
661 &lur->lur_update_rec.ur_update_count, \
663 tur->tur_update_params, \
664 &tur->tur_update_param_count, \
665 &avail_param_size, __VA_ARGS__); \
666 if (ret == -E2BIG) { \
667 ret = tur_update_extend(tur, avail_op_size, \
679 #define update_record_size(env, name, th, ...) \
681 struct top_thandle *top_th; \
682 struct top_multiple_thandle *tmt; \
684 top_th = container_of(th, struct top_thandle, tt_super); \
686 LASSERT(top_th->tt_multiple_thandle != NULL); \
687 tmt = top_th->tt_multiple_thandle; \
688 tmt->tmt_record_size += \
689 update_records_##name##_size(env, __VA_ARGS__); \