-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigetput.c
108 lines (92 loc) · 2.48 KB
/
igetput.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "stdio.h"
#include "stdlib.h"
#include "filesys.h"
struct inode *iget(unsigned int dinodeid) /*iget( )*/
{
int existed = 0, inodeid;
long addr;
struct inode *temp;
struct inode *newinode;
inodeid = dinodeid % NHINO;
if (hinode[inodeid].i_forw == NULL)
{
existed = 0;
}
else
{
temp = hinode[inodeid].i_forw;
while (temp)
if (temp->i_ino == inodeid) /* existed */
{
existed = 1;
temp->i_count++;
return temp;
}
else
temp = temp->i_forw; /* not existed */
}
/* not existed */
/*1. calculate the addr of the dinode in the file sys column */
addr = DINODESTART + dinodeid * sizeof(struct dinode);
/*2. malloc the new inode */
newinode = (struct inode *)malloc(sizeof(struct inode));
memset(newinode, 0, (sizeof(struct inode)));
/*3. read the dinode to the inode */
fseek(fd, addr, SEEK_SET);
fread(&(newinode->di_number), sizeof(struct dinode), 1, fd);
/*4. put it into hinode [inodeid] queue */
newinode->i_forw = hinode[inodeid].i_forw;
newinode->i_back = newinode;
if (newinode->i_forw != NULL)
newinode->i_forw->i_back = newinode;
hinode[inodeid].i_forw = newinode;
/*5. initialize the inode */
newinode->i_count = 1;
newinode->i_flag = 0; /* flag for not update */
newinode->i_ino = dinodeid;
return newinode;
}
int iput(struct inode *pinode) /*iput( )*/
{
int i = 0, inodeid;
long addr;
unsigned int block_num;
struct inode temp;
inodeid = pinode->i_ino % NHINO;
if (pinode->i_count > 1)
{
pinode->i_count--;
return 1;
}
else
{
if (pinode->di_number != 0)
{ /* write back the inode */
addr = DINODESTART + pinode->i_ino * sizeof(struct dinode);
fseek(fd, addr, SEEK_SET);
fwrite(&(pinode->di_number), sizeof(struct dinode), 1, fd);
fseek(fd, addr, SEEK_SET);
fread(&(temp.di_number), sizeof(struct dinode), 1, fd);
}
else
{
/* rm the inoide & the block of the file in the disk */
block_num = pinode->di_size / BLOCKSIZ;
for (i = 0; i < block_num; i++)
//bfree(pinode->di_addr[i]);
ifree(pinode->i_ino);
}
/*free the inode in the memory */
if (pinode->i_forw == NULL)
pinode->i_back->i_forw = NULL;
else
{
pinode->i_forw->i_back = pinode->i_back;
pinode->i_back->i_forw = pinode->i_forw;
}
if (pinode->i_back == pinode) //hash±íÖиÃÁÐÊ£ÏÂ×îºóÒ»¸öinode
hinode[inodeid].i_forw = NULL;
free(pinode);
}
return 0;
}