// File: 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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x1c99d(%rip), %rcx     # 0x24cc8
movq %rcx, -0x28(%rbp)
leaq 0xc01a(%rip), %rcx      # 0x14350
movq %rcx, -0x30(%rbp)
movl (%rax), %ecx
orl $0x6, %ecx
movl %ecx, (%rax)
xorl %eax, %eax
leaq 0x1c97e(%rip), %rcx     # 0x24cc8
cmpl 0x13fc(%rcx), %eax
jne 0x8357
jmp 0x852d
jmp 0x8359
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x1c964(%rip), %rdx     # 0x24cc8
cmpl 0x10c8(%rdx), %ecx
movb %al, -0x49(%rbp)
jne 0x8383
leaq 0x1c952(%rip), %rax     # 0x24cc8
cmpl $0x0, 0x13fc(%rax)
setne %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x838f
jmp 0x852d
leaq 0x1c932(%rip), %rax     # 0x24cc8
movl 0x13f8(%rax), %eax
movl $0x200, %ecx            # imm = 0x200
cltd
idivl %ecx
movl %eax, -0xc(%rbp)
leaq 0x1c91a(%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 0x1c8f3(%rip), %rsi     # 0x24cc8
addq $0x13f0, %rsi           # imm = 0x13F0
addq $0xc, %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 0x8403
jmp 0x852d
movl $0x200, %eax            # imm = 0x200
cmpl -0x18(%rbp), %eax
jne 0x8422
movl -0x10(%rbp), %esi
leaq 0xbf39(%rip), %rdi      # 0x14350
callq 0x3ca0
movq %rax, -0x20(%rbp)
jmp 0x8435
movl -0x10(%rbp), %esi
leaq 0xbf24(%rip), %rdi      # 0x14350
callq 0x3f10
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x1c875(%rip), %rax     # 0x24cc8
movq 0x13f0(%rax), %rsi
movslq -0x18(%rbp), %rdx
callq 0x3260
movl -0x18(%rbp), %eax
leaq 0x1c85b(%rip), %rcx     # 0x24cc8
movq 0x13f0(%rcx), %rcx
cltq
addq %rax, %rcx
leaq 0x1c848(%rip), %rax     # 0x24cc8
movq %rcx, 0x13f0(%rax)
movl -0x18(%rbp), %ecx
leaq 0x1c837(%rip), %rax     # 0x24cc8
addl 0x13f8(%rax), %ecx
leaq 0x1c82a(%rip), %rax     # 0x24cc8
movl %ecx, 0x13f8(%rax)
movl -0x18(%rbp), %eax
leaq 0x1c81a(%rip), %rcx     # 0x24cc8
movl 0x13fc(%rcx), %ecx
subl %eax, %ecx
leaq 0x1c80b(%rip), %rax     # 0x24cc8
movl %ecx, 0x13fc(%rax)
leaq 0x1c7fe(%rip), %rax     # 0x24cc8
cmpl $0x0, 0x10c8(%rax)
je 0x84e3
movq -0x20(%rbp), %rsi
leaq 0xbe72(%rip), %rdi      # 0x14350
callq 0x3e80
movq -0x20(%rbp), %rsi
leaq 0xbe62(%rip), %rdi      # 0x14350
callq 0x4080
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
leaq 0x1c7c7(%rip), %rcx     # 0x24cc8
cmpl 0x13f8(%rcx), %eax
jge 0x851d
movq -0x48(%rbp), %rax
leaq 0x1c7b4(%rip), %rcx     # 0x24cc8
movl 0x13f8(%rcx), %ecx
movl %ecx, 0x1c(%rax)
movq -0x48(%rbp), %rax
movl (%rax), %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
jmp 0x8359
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)