// File: /workspace/github_cpp_makefile_star2_O2/yangwanyuan[P]HLKVDS/src/KernelDevice.cc
// Function: hlkvds::KernelDevice::Open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
int KernelDevice::Open(string path, bool dsync) {
    if (isOpen_) {
        __ERROR("This Device is already Open!!!");
        return KD_ERR;
    }

    path_ = path;

    int r = 0;

    directFd_ = open(path_.c_str(), O_RDWR | O_DIRECT | O_DSYNC, 0666);
    if (directFd_ < 0) {
        __ERROR("Could not open file: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    if (dsync) {
        //bufFd_ = open(path_.c_str(), O_RDWR | O_SYNC, 0666);
        bufFd_ = open(path_.c_str(), O_RDWR | O_DSYNC, 0666);
    } else {
        bufFd_ = open(path_.c_str(), O_RDWR, 0666);
    }

    if (bufFd_ < 0) {
        __ERROR("Could not open file: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    r = posix_fadvise(bufFd_, 0, 0, POSIX_FADV_RANDOM);
    if (r < 0) {
        __ERROR("Couldn't posix_fadvise random: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    r = lock_device();
    if (r < 0) {
        __ERROR("failed to lock device: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    struct stat statbuf;
    r = fstat(directFd_, &statbuf);
    if (r < 0) {
        __ERROR("Couldn't read fstat: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    if (S_ISBLK(statbuf.st_mode)) {
        capacity_ = get_block_device_capacity();
        blockSize_ = statbuf.st_blksize;
    } else {
        capacity_ = statbuf.st_size;
        //TODO:() dynamic block size in file mode
        blockSize_ = getpagesize();
    }

    isOpen_ = true;

    return KD_OK;

open_fail:
    close(bufFd_);
    close(directFd_);
    directFd_ = -1;
    bufFd_ = -1;
    return KD_ERR;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpb $0x1, 0x40(%rdi)
je 0xff71
movl %edx, %ebp
movq %rdi, %rbx
addq $0x20, %rdi
callq 0x6240
movq 0x20(%rbx), %rdi
movl $0x5002, %esi           # imm = 0x5002
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6300
movl %eax, 0x8(%rbx)
leaq 0xfdcb(%rip), %r14      # 0x1fc97
testl %eax, %eax
js 0xffcb
movq 0x20(%rbx), %rdi
movzbl %bpl, %esi
shll $0xc, %esi
orl $0x2, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6300
movl %eax, 0xc(%rbx)
testl %eax, %eax
js 0xffcf
pushq $0x1
popq %rcx
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6320
testl %eax, %eax
js 0xffd3
movq %rbx, %rdi
callq 0xfe58
testl %eax, %eax
js 0xffde
movl 0x8(%rbx), %edi
leaq 0x8(%rsp), %rsi
callq 0x6640
testl %eax, %eax
js 0x1007a
movl $0xf000, %eax           # imm = 0xF000
andl 0x20(%rsp), %eax
cmpl $0x6000, %eax           # imm = 0x6000
jne 0xff55
movq %rbx, %rdi
callq 0xfdca
movq %rax, %r14
movl 0x40(%rsp), %eax
jmp 0xff5f
movq 0x38(%rsp), %r14
callq 0x66b0
movq %r14, 0x10(%rbx)
movl %eax, 0x18(%rbx)
movb $0x1, 0x40(%rbx)
xorl %eax, %eax
jmp 0x10068
movq 0x22060(%rip), %r14     # 0x31fd8
movq (%r14), %rbx
callq 0x63c0
leaq 0xf31e(%rip), %rsi      # 0x1f2a5
leaq 0xfce5(%rip), %rcx      # 0x1fc73
leaq 0xfc37(%rip), %r8       # 0x1fbcc
pushq $0x5a
popq %r9
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r14), %rcx
leaq 0xfcc8(%rip), %rdi      # 0x1fc78
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x6750
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
jmp 0x10065
pushq $0x64
jmp 0xffe7
pushq $0x70
jmp 0xffe7
leaq 0xfcd9(%rip), %r14      # 0x1fcb3
pushq $0x76
jmp 0xffe7
leaq 0xfcf4(%rip), %r14      # 0x1fcd9
pushq $0x7c
popq %rbp
movq 0x21fe9(%rip), %r13     # 0x31fd8
movq (%r13), %r15
callq 0x63c0
leaq 0xf2a6(%rip), %rsi      # 0x1f2a5
leaq 0xfc6d(%rip), %rcx      # 0x1fc73
leaq 0xfbbf(%rip), %r8       # 0x1fbcc
movq %r15, %rdi
movq %rax, %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x6580
movq (%r13), %r15
movq 0x20(%rbx), %r12
callq 0x6060
movl (%rax), %edi
callq 0x6110
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x6580
movq (%r13), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
movl 0xc(%rbx), %edi
callq 0x6770
movl 0x8(%rbx), %edi
callq 0x6770
orq $-0x1, 0x8(%rbx)
pushq $-0x1
popq %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfc76(%rip), %r14      # 0x1fcf7
movl $0x83, %ebp
jmp 0xffe8
nop