// File: 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:
subq $0x148, %rsp            # imm = 0x148
movq %rsi, 0x88(%rsp)
movb %dl, %al
movq %rdi, 0x138(%rsp)
movq %rsi, 0x130(%rsp)
andb $0x1, %al
movb %al, 0x12f(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x90(%rsp)
testb $0x1, 0x40(%rax)
je 0x292fa
jmp 0x29276
movq 0x47d5b(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x80(%rsp)
callq 0x8480
movq 0x80(%rsp), %rdi
movq %rax, %rdx
leaq 0x29212(%rip), %rsi     # 0x524b1
leaq 0x29b97(%rip), %rcx     # 0x52e3d
leaq 0x29ae9(%rip), %r8      # 0x52d96
movl $0x5a, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x47d17(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x29b77(%rip), %rsi     # 0x52e42
movb $0x0, %al
callq 0x86f0
movq 0x47cff(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x29ae2(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movl $0xffffffff, 0x144(%rsp) # imm = 0xFFFFFFFF
jmp 0x29862
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
addq $0x20, %rdi
callq 0x8060
movq 0x90(%rsp), %rdi
movl $0x0, 0x128(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, %rdi
movl $0x5002, %esi           # imm = 0x5002
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x8390
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x8(%rax)
jge 0x29403
jmp 0x2935c
movq 0x47c75(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x68(%rsp)
callq 0x8480
movq 0x68(%rsp), %rdi
movq %rax, %rdx
leaq 0x29132(%rip), %rsi     # 0x524b1
leaq 0x29ab7(%rip), %rcx     # 0x52e3d
leaq 0x29a09(%rip), %r8      # 0x52d96
movl $0x64, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rdi
movq 0x47c2f(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x70(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, 0x78(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq %rax, %rcx
leaq 0x29a82(%rip), %rsi     # 0x52e61
movb $0x0, %al
callq 0x86f0
movq 0x47beb(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x299ce(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
jmp 0x29821
testb $0x1, 0x12f(%rsp)
je 0x29441
movq 0x90(%rsp), %rdi
addq $0x20, %rdi
callq 0x8180
movq %rax, %rdi
movl $0x1002, %esi           # imm = 0x1002
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x8390
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x29473
movq 0x90(%rsp), %rdi
addq $0x20, %rdi
callq 0x8180
movq %rax, %rdi
movl $0x2, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x8390
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x90(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jge 0x2952e
jmp 0x29487
movq 0x47b4a(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x50(%rsp)
callq 0x8480
movq 0x50(%rsp), %rdi
movq %rax, %rdx
leaq 0x29007(%rip), %rsi     # 0x524b1
leaq 0x2998c(%rip), %rcx     # 0x52e3d
leaq 0x298de(%rip), %r8      # 0x52d96
movl $0x70, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rdi
movq 0x47b04(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x58(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, 0x60(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq %rax, %rcx
leaq 0x29957(%rip), %rsi     # 0x52e61
movb $0x0, %al
callq 0x86f0
movq 0x47ac0(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x298a3(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
jmp 0x29821
movq 0x90(%rsp), %rax
movl 0xc(%rax), %edi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x83b0
movl %eax, 0x128(%rsp)
cmpl $0x0, 0x128(%rsp)
jge 0x29608
jmp 0x29561
movq 0x47a70(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x38(%rsp)
callq 0x8480
movq 0x38(%rsp), %rdi
movq %rax, %rdx
leaq 0x28f2d(%rip), %rsi     # 0x524b1
leaq 0x298b2(%rip), %rcx     # 0x52e3d
leaq 0x29804(%rip), %r8      # 0x52d96
movl $0x76, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rdi
movq 0x47a2a(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x40(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, 0x48(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq %rax, %rcx
leaq 0x29899(%rip), %rsi     # 0x52e7d
movb $0x0, %al
callq 0x86f0
movq 0x479e6(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x297c9(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
jmp 0x29821
movq 0x90(%rsp), %rdi
callq 0x291b0
movl %eax, 0x128(%rsp)
cmpl $0x0, 0x128(%rsp)
jge 0x296d3
jmp 0x2962c
movq 0x479a5(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x8480
movq 0x20(%rsp), %rdi
movq %rax, %rdx
leaq 0x28e62(%rip), %rsi     # 0x524b1
leaq 0x297e7(%rip), %rcx     # 0x52e3d
leaq 0x29739(%rip), %r8      # 0x52d96
movl $0x7c, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rdi
movq 0x4795f(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x28(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, 0x30(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rdx
movq %rax, %rcx
leaq 0x297f4(%rip), %rsi     # 0x52ea3
movb $0x0, %al
callq 0x86f0
movq 0x4791b(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x296fe(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
jmp 0x29821
movq 0x90(%rsp), %rax
movl 0x8(%rax), %edi
leaq 0x98(%rsp), %rsi
callq 0x87d0
movl %eax, 0x128(%rsp)
cmpl $0x0, 0x128(%rsp)
jge 0x297a6
jmp 0x29702
movq 0x478cf(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x8480
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x28d8c(%rip), %rsi     # 0x524b1
leaq 0x29711(%rip), %rcx     # 0x52e3d
leaq 0x29663(%rip), %r8      # 0x52d96
movl $0x83, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rdi
movq 0x47889(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
addq $0x20, %rdi
callq 0x8180
movq %rax, 0x18(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
leaq 0x2973c(%rip), %rsi     # 0x52ec1
movb $0x0, %al
callq 0x86f0
movq 0x47845(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x29628(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
jmp 0x29821
movl 0xb0(%rsp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x6000, %eax           # imm = 0x6000
jne 0x297e2
movq 0x90(%rsp), %rdi
callq 0x290d0
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0xd0(%rsp), %rcx
movl %ecx, 0x18(%rax)
jmp 0x29808
movq 0x90(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq %rcx, 0x10(%rax)
callq 0x8860
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x90(%rsp), %rax
movb $0x1, 0x40(%rax)
movl $0x0, 0x144(%rsp)
jmp 0x29862
movq 0x90(%rsp), %rax
movl 0xc(%rax), %edi
callq 0x8900
movq 0x90(%rsp), %rax
movl 0x8(%rax), %edi
callq 0x8900
movq 0x90(%rsp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x144(%rsp) # imm = 0xFFFFFFFF
movl 0x144(%rsp), %eax
addq $0x148, %rsp            # imm = 0x148
retq
nopw %cs:(%rax,%rax)