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

	Buf* pBuf;
	User& u = globalUser;
	BufferManager& bufMgr = globalBufferManager;

	/* ����Inode�����ʱ�־λ */
	this->i_flag |= (Inode::IACC | Inode::IUPD);


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

	while (User::myNOERROR == u.u_error && u.u_IOParam.m_Count != 0)
	{
		lbn = 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);

		/* ��ͨ�ļ� */

		/* ���߼�����lbnת���������̿���bn */
		if ((bn = this->Bmap(lbn)) == 0)
		{
			return;
		}

		if (Inode::BLOCK_SIZE == nbytes)
		{
			/* ����д������������һ���ַ��飬��Ϊ�����仺�� */
			pBuf = bufMgr.GetBlk(bn);
		}
		else
		{
			/* д�����ݲ���һ���ַ��飬�ȶ���д���������ַ����Ա�������Ҫ��д�����ݣ� */
			pBuf = bufMgr.Bread(bn);
		}

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

		/* д����: ���û�Ŀ�����������ݵ������� */
		memcpy(start, u.u_IOParam.m_Base, nbytes);

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

		if (u.u_error != User::myNOERROR)	/* д�����г��� */
		{
			bufMgr.Brelse(pBuf);
		}

		/* ����������Ϊ�ӳ�д�������ڽ���I/O�������ַ��������������� */
		bufMgr.Bdwrite(pBuf);


		/* ��ͨ�ļ��������� */
		if (this->i_size < u.u_IOParam.m_Offset)
		{
			this->i_size = u.u_IOParam.m_Offset;
		}

		/*
		 * ֮ǰ�����ж��̿��ܵ��½����л����ڽ���˯���ڼ䵱ǰ�ڴ�Inode����
		 * ��ͬ��������Inode���ڴ���Ҫ�������ø��±�־λ��
		 * ����û�б�Ҫѽ����ʹwriteϵͳ����û��������iput����i_count����0֮���ŻὫ�ڴ�i�ڵ�ͬ���ش��̡�������
		 * �ļ�û��close֮ǰ�ǲ��ᷢ���ġ�
		 * ���ǵ�ϵͳ��writeϵͳ���������͸������ܳ������������ˡ�
		 * ����������ȥ����
		 */
		this->i_flag |= Inode::IUPD;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
orb $0x6, (%rdi)
leaq 0x1833a(%rip), %r13     # 0x1dbd8
cmpl $0x0, 0x13fc(%r13)
je 0x59b6
cmpl $0x0, 0x10c8(%r13)
sete %cl
movl 0x13fc(%r13), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x59b6
movq %rdi, %rbx
movl 0x13f8(%r13), %r12d
leal 0x1ff(%r12), %ecx
testl %r12d, %r12d
cmovnsl %r12d, %ecx
movl %ecx, %esi
sarl $0x9, %esi
andl $0xfffffe00, %ecx       # imm = 0xFFFFFE00
subl %ecx, %r12d
movl $0x200, %r14d           # imm = 0x200
subl %r12d, %r14d
cmpl %r14d, %eax
cmovll %eax, %r14d
movq %rbx, %rdi
callq 0x5684
testl %eax, %eax
je 0x59b6
leaq 0x7944(%rip), %rdi      # 0xd260
movl %eax, %esi
cmpl $0x200, %r14d           # imm = 0x200
jne 0x592e
callq 0x38f0
jmp 0x5933
callq 0x3af0
movq %rax, %r15
movslq %r12d, %rdi
addq 0x20(%rax), %rdi
movq 0x13f0(%r13), %rsi
movslq %r14d, %r12
movq %r12, %rdx
callq 0x31b0
addq %r12, 0x13f0(%r13)
addl %r14d, 0x13f8(%r13)
subl %r14d, 0x13fc(%r13)
cmpl $0x0, 0x10c8(%r13)
leaq 0x78ed(%rip), %r14      # 0xd260
je 0x5980
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a9c
movq %r14, %rdi
movq %r15, %rsi
callq 0x3bea
movl 0x13f8(%r13), %eax
cmpl %eax, 0x1c(%rbx)
jge 0x599a
movl %eax, 0x1c(%rbx)
orb $0x2, (%rbx)
cmpl $0x0, 0x10c8(%r13)
jne 0x59b6
movl 0x13fc(%r13), %eax
testl %eax, %eax
jne 0x58d1
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop