// File: ljl0222[P]SecondFileSystem/INode.cpp
// Function: Inode::ReadI()
void Inode::ReadI()
{
	int lbn;	/* �ļ��߼����� */
	int bn;		/* lbn��Ӧ�������̿��� */
	int offset;	/* ��ǰ�ַ�������ʼ����λ�� */
	int nbytes;	/* �������û�Ŀ�����ֽ����� */

	Buf* pBuf;

	User& u = globalUser;
	BufferManager& bufMgr = globalBufferManager;

	if (0 == u.u_IOParam.m_Count)
	{
		/* ��Ҫ���ֽ���Ϊ�㣬�򷵻� */
		return;
	}

	this->i_flag |= Inode::IACC;

	/* һ��һ���ַ����ض�������ȫ�����ݣ�ֱ�������ļ�β */
	while (User::myNOERROR == u.u_error && u.u_IOParam.m_Count != 0)
	{
		lbn = bn = u.u_IOParam.m_Offset / Inode::BLOCK_SIZE;
		offset = u.u_IOParam.m_Offset % Inode::BLOCK_SIZE;
		/* ���͵��û������ֽ�������ȡ��������ʣ���ֽ����뵱ǰ�ַ�������Ч�ֽ�����Сֵ */
		nbytes = min(Inode::BLOCK_SIZE - offset, u.u_IOParam.m_Count);

		if (1)
		{	/* ���������������豸�ļ� */

			int remain = this->i_size - u.u_IOParam.m_Offset;
			/* �����Ѷ��������ļ���β */
			if (remain <= 0)
			{
				return;
			}
			/* ���͵��ֽ�������ȡ����ʣ���ļ��ĳ��� */
			nbytes = min(nbytes, remain);

			/* ���߼�����lbnת���������̿���bn ��Bmap������Inode::rablock����UNIX��Ϊ��ȡԤ�����Ŀ���̫��ʱ��
			 * ������Ԥ������ʱ Inode::rablock ֵΪ 0��
			 * */
			if ((bn = this->Bmap(lbn)) == 0)
			{
				return;
			}
		}

		pBuf = bufMgr.Bread(bn);

		/* ��¼������ȡ�ַ������߼����� */
		this->i_lastr = lbn;

		/* ������������ʼ��λ�� */
		unsigned char* start = pBuf->b_addr + offset;

		/* ������: �ӻ������������û�Ŀ����
		 * i386оƬ��ͬһ��ҳ��ӳ���û��ռ����ں˿ռ䣬��һ��Ӳ���ϵĲ��� ʹ��i386��ʵ�� iomove����
		 * ��PDP-11Ҫ��������*/
		memcpy(u.u_IOParam.m_Base, start, nbytes);

		/* �ô����ֽ���nbytes���¶�дλ�� */
		u.u_IOParam.m_Base += nbytes;
		u.u_IOParam.m_Offset += nbytes;
		u.u_IOParam.m_Count -= nbytes;

		bufMgr.Brelse(pBuf);	/* ʹ���껺�棬�ͷŸ���Դ */
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x1ce8d(%rip), %rax     # 0x24cc8
movq %rax, -0x28(%rbp)
leaq 0xc50a(%rip), %rax      # 0x14350
movq %rax, -0x30(%rbp)
xorl %eax, %eax
leaq 0x1ce75(%rip), %rcx     # 0x24cc8
cmpl 0x13fc(%rcx), %eax
jne 0x7e60
jmp 0x8012
movq -0x48(%rbp), %rax
movl (%rax), %ecx
orl $0x4, %ecx
movl %ecx, (%rax)
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x1ce52(%rip), %rdx     # 0x24cc8
cmpl 0x10c8(%rdx), %ecx
movb %al, -0x49(%rbp)
jne 0x7e95
leaq 0x1ce40(%rip), %rax     # 0x24cc8
cmpl $0x0, 0x13fc(%rax)
setne %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x7ea1
jmp 0x8012
leaq 0x1ce20(%rip), %rax     # 0x24cc8
movl 0x13f8(%rax), %eax
movl $0x200, %ecx            # imm = 0x200
cltd
idivl %ecx
movl %eax, -0x10(%rbp)
movl %eax, -0xc(%rbp)
leaq 0x1ce05(%rip), %rax     # 0x24cc8
movl 0x13f8(%rax), %eax
movl $0x200, %ecx            # imm = 0x200
cltd
idivl %ecx
movl %edx, -0x14(%rbp)
movl $0x200, %eax            # imm = 0x200
subl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rdi
leaq 0x1cdde(%rip), %rsi     # 0x24cc8
addq $0x13f0, %rsi           # imm = 0x13F0
addq $0xc, %rsi
callq 0x8a90
movq %rax, %rcx
movq -0x48(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x18(%rbp)
movl 0x1c(%rax), %eax
leaq 0x1cdb8(%rip), %rcx     # 0x24cc8
subl 0x13f8(%rcx), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jg 0x7f24
jmp 0x8012
leaq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x8a90
movq -0x48(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x18(%rbp)
movl -0xc(%rbp), %esi
callq 0x8020
movl %eax, -0x10(%rbp)
cmpl $0x0, %eax
jne 0x7f4f
jmp 0x8012
movl -0x10(%rbp), %esi
leaq 0xc3f7(%rip), %rdi      # 0x14350
callq 0x3f10
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, -0x20(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, 0x48(%rax)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
leaq 0x1cd3f(%rip), %rax     # 0x24cc8
movq 0x13f0(%rax), %rdi
movq -0x40(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x3260
movl -0x18(%rbp), %eax
leaq 0x1cd21(%rip), %rcx     # 0x24cc8
movq 0x13f0(%rcx), %rcx
cltq
addq %rax, %rcx
leaq 0x1cd0e(%rip), %rax     # 0x24cc8
movq %rcx, 0x13f0(%rax)
movl -0x18(%rbp), %ecx
leaq 0x1ccfd(%rip), %rax     # 0x24cc8
addl 0x13f8(%rax), %ecx
leaq 0x1ccf0(%rip), %rax     # 0x24cc8
movl %ecx, 0x13f8(%rax)
movl -0x18(%rbp), %eax
leaq 0x1cce0(%rip), %rcx     # 0x24cc8
movl 0x13fc(%rcx), %ecx
subl %eax, %ecx
leaq 0x1ccd1(%rip), %rax     # 0x24cc8
movl %ecx, 0x13fc(%rax)
movq -0x20(%rbp), %rsi
leaq 0xc348(%rip), %rdi      # 0x14350
callq 0x3e80
jmp 0x7e6b
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)