Skip to content

Commit 90b53be

Browse files
authored
Merge pull request #103 from avsm/fsync-op
add Uring.fsync and Uring.fdatasync ops
2 parents 853e501 + 5fafe7a commit 90b53be

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

lib/uring/uring.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ module Uring = struct
333333
external submit_unlinkat : t -> id -> Unix.file_descr -> Sketch.ptr -> bool -> bool = "ocaml_uring_submit_unlinkat" [@@noalloc]
334334
external submit_send_msg : t -> id -> Unix.file_descr -> Msghdr.t -> Sketch.ptr -> bool = "ocaml_uring_submit_send_msg" [@@noalloc]
335335
external submit_recv_msg : t -> id -> Unix.file_descr -> Msghdr.t -> Sketch.ptr -> bool = "ocaml_uring_submit_recv_msg" [@@noalloc]
336+
external submit_fsync : t -> id -> Unix.file_descr -> int64 -> int -> bool = "ocaml_uring_submit_fsync" [@@noalloc]
337+
external submit_fdatasync : t -> id -> Unix.file_descr -> int64 -> int -> bool = "ocaml_uring_submit_fdatasync" [@@noalloc]
336338

337339
type cqe_option = private
338340
| Cqe_none
@@ -555,6 +557,12 @@ let recv_msg t fd msghdr user_data =
555557
let iovec = Sketch.Iovec.alloc t.sketch buffers in
556558
Uring.submit_recv_msg t.uring id fd msghdr iovec) user_data ~extra_data:msghdr
557559

560+
let fsync t ?(off=0L) ?(len=0) fd user_data =
561+
with_id t (fun id -> Uring.submit_fsync t.uring id fd off len) user_data
562+
563+
let fdatasync t ?(off=0L) ?(len=0) fd user_data =
564+
with_id t (fun id -> Uring.submit_fdatasync t.uring id fd off len) user_data
565+
558566
let cancel t job user_data =
559567
ignore (Heap.ptr job : Uring.id); (* Check it's still valid *)
560568
with_id t (fun id -> Uring.submit_cancel t.uring id (Heap.ptr job)) user_data

lib/uring/uring.mli

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,16 @@ val recv_msg : 'a t -> Unix.file_descr -> Msghdr.t -> 'a -> 'a job option
607607
(** [recv_msg t fd msghdr d] will submit a [recvmsg(2)] request. If the request is
608608
successful then the [msghdr] will contain the sender address and the data received. *)
609609

610+
val fsync : 'a t -> ?off:int64 -> ?len:int -> Unix.file_descr -> 'a -> 'a job option
611+
(** [fsync t ?off ?len fd d] will submit an [fsync(2)] request, with the optional
612+
offset [off] and length [len] specifying the subset of the file to perform the
613+
synchronisation on. *)
614+
615+
val fdatasync : 'a t -> ?off:int64 -> ?len:int -> Unix.file_descr -> 'a -> 'a job option
616+
(** [fdatasync t ?off ?len fd d] will submit an [fdatasync(2)] request, with the optional
617+
offset [off] and length [len] specifying the subset of the file to perform the
618+
synchronisation on. *)
619+
610620
(** {2 Probing}
611621
612622
You can check which operations are supported by the running kernel. *)

lib/uring/uring_stubs.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,32 @@ ocaml_uring_submit_unlinkat(value v_uring, value v_id, value v_fd, value v_sketc
849849
return (Val_true);
850850
}
851851

852+
value /* noalloc */
853+
ocaml_uring_submit_fsync(value v_uring, value v_id, value v_fd, value v_off, value v_len)
854+
{
855+
struct io_uring *ring = Ring_val(v_uring);
856+
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
857+
if (!sqe) return (Val_false);
858+
io_uring_prep_fsync(sqe, Int_val(v_fd), 0);
859+
sqe->off = Int64_val(v_off);
860+
sqe->len = Int_val(v_len);
861+
io_uring_sqe_set_data(sqe, (void *)Long_val(v_id));
862+
return (Val_true);
863+
}
864+
865+
value /* noalloc */
866+
ocaml_uring_submit_fdatasync(value v_uring, value v_id, value v_fd, value v_off, value v_len)
867+
{
868+
struct io_uring *ring = Ring_val(v_uring);
869+
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
870+
if (!sqe) return (Val_false);
871+
io_uring_prep_fsync(sqe, Int_val(v_fd), IORING_FSYNC_DATASYNC);
872+
sqe->off = Int64_val(v_off);
873+
sqe->len = Int_val(v_len);
874+
io_uring_sqe_set_data(sqe, (void *)Long_val(v_id));
875+
return (Val_true);
876+
}
877+
852878
value /* noalloc */
853879
ocaml_uring_submit_cancel(value v_uring, value v_id, value v_target) {
854880
struct io_uring *ring = Ring_val(v_uring);

tests/main.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ val fd : Unix.file_descr = <abstr>
184184
x.st_kind, Printf.sprintf "0o%o" x.st_perm, x.st_size;;
185185
- : Unix.file_kind * string * int = (Unix.S_REG, "0o600", 9)
186186
187+
# Uring.fsync t fd `Create;;
188+
- : [ `Create ] Uring.job option = Some <abstr>
189+
# Uring.submit t;;
190+
- : int = 1
191+
# let v, read = consume t;;
192+
val v : [ `Create ] = `Create
193+
val read : int = 0
194+
195+
# Uring.fdatasync t ~off:1L ~len:5 fd `Create;;
196+
- : [ `Create ] Uring.job option = Some <abstr>
197+
# Uring.submit t;;
198+
- : int = 1
199+
# let v, read = consume t;;
200+
val v : [ `Create ] = `Create
201+
val read : int = 0
202+
187203
# let fd : unit = Unix.close fd;;
188204
val fd : unit = ()
189205

0 commit comments

Comments
 (0)