[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
inode.c File Reference
#include "filesys/inode.h"
#include <list.h>
#include <debug.h>
#include <round.h>
#include <string.h>
#include "filesys/filesys.h"
#include "filesys/free-map.h"
#include "threads/malloc.h"
Include dependency graph for inode.c:

Classes

struct  inode_disk
 
struct  inode
 

Macros

#define INODE_MAGIC   0x494e4f44
 

Functions

static size_t bytes_to_sectors (off_t size)
 
static disk_sector_t byte_to_sector (const struct inode *inode, off_t pos)
 
void inode_init (void)
 
bool inode_create (disk_sector_t sector, off_t length)
 
struct inodeinode_open (disk_sector_t sector)
 
struct inodeinode_reopen (struct inode *inode)
 
disk_sector_t inode_get_inumber (const struct inode *inode)
 
void inode_close (struct inode *inode)
 
void inode_remove (struct inode *inode)
 
off_t inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset)
 
off_t inode_write_at (struct inode *inode, const void *buffer_, off_t size, off_t offset)
 
void inode_deny_write (struct inode *inode)
 
void inode_allow_write (struct inode *inode)
 
off_t inode_length (const struct inode *inode)
 

Variables

static struct list open_inodes
 

Macro Definition Documentation

◆ INODE_MAGIC

#define INODE_MAGIC   0x494e4f44

Function Documentation

◆ byte_to_sector()

static disk_sector_t byte_to_sector ( const struct inode inode,
off_t  pos 
)
static
44 {
45 ASSERT (inode != NULL);
46 if (pos < inode->data.length)
47 return inode->data.start + pos / DISK_SECTOR_SIZE;
48 else
49 return -1;
50}
#define ASSERT(CONDITION)
Definition: debug.h:30
#define DISK_SECTOR_SIZE
Definition: disk.h:8
#define NULL
Definition: stddef.h:4
disk_sector_t start
Definition: inode.c:16
Definition: inode.c:30
struct inode_disk data
Definition: inode.c:36
Here is the caller graph for this function:

◆ bytes_to_sectors()

static size_t bytes_to_sectors ( off_t  size)
inlinestatic
25 {
27}
uint16_t size
Definition: mmu.h:0
#define DIV_ROUND_UP(X, STEP)
Definition: round.h:10
Here is the caller graph for this function:

◆ inode_allow_write()

void inode_allow_write ( struct inode inode)
303 {
307}
int deny_write_cnt
Definition: inode.c:35
int open_cnt
Definition: inode.c:33
Here is the caller graph for this function:

◆ inode_close()

void inode_close ( struct inode inode)
150 {
151 /* Ignore null pointer. */
152 if (inode == NULL)
153 return;
154
155 /* Release resources if this was the last opener. */
156 if (--inode->open_cnt == 0) {
157 /* Remove from inode list and release lock. */
159
160 /* Deallocate blocks if removed. */
161 if (inode->removed) {
165 }
166
167 free (inode);
168 }
169}
void free_map_release(disk_sector_t sector, size_t cnt)
Definition: free-map.c:41
static size_t bytes_to_sectors(off_t size)
Definition: inode.c:25
struct list_elem * list_remove(struct list_elem *)
Definition: list.c:241
void free(void *)
Definition: malloc.c:202
off_t length
Definition: inode.c:17
struct list_elem elem
Definition: inode.c:31
disk_sector_t sector
Definition: inode.c:32
bool removed
Definition: inode.c:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_create()

bool inode_create ( disk_sector_t  sector,
off_t  length 
)
68 {
69 struct inode_disk *disk_inode = NULL;
70 bool success = false;
71
72 ASSERT (length >= 0);
73
74 /* If this assertion fails, the inode structure is not exactly
75 * one sector in size, and you should fix that. */
76 ASSERT (sizeof *disk_inode == DISK_SECTOR_SIZE);
77
78 disk_inode = calloc (1, sizeof *disk_inode);
79 if (disk_inode != NULL) {
80 size_t sectors = bytes_to_sectors (length);
81 disk_inode->length = length;
82 disk_inode->magic = INODE_MAGIC;
83 if (free_map_allocate (sectors, &disk_inode->start)) {
84 disk_write (filesys_disk, sector, disk_inode);
85 if (sectors > 0) {
86 static char zeros[DISK_SECTOR_SIZE];
87 size_t i;
88
89 for (i = 0; i < sectors; i++)
90 disk_write (filesys_disk, disk_inode->start + i, zeros);
91 }
92 success = true;
93 }
94 free (disk_inode);
95 }
96 return success;
97}
void disk_write(struct disk *d, disk_sector_t sec_no, const void *buffer)
Definition: disk.c:235
struct disk * filesys_disk
Definition: filesys.c:12
bool free_map_allocate(size_t cnt, disk_sector_t *sectorp)
Definition: free-map.c:26
#define INODE_MAGIC
Definition: inode.c:11
void * calloc(size_t, size_t) __attribute__((malloc))
Definition: malloc.c:149
Definition: inode.c:15
unsigned magic
Definition: inode.c:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_deny_write()

void inode_deny_write ( struct inode inode)
294{
297}
Here is the caller graph for this function:

◆ inode_get_inumber()

disk_sector_t inode_get_inumber ( const struct inode inode)
142 {
143 return inode->sector;
144}

◆ inode_init()

void inode_init ( void  )
58 {
60}
static struct list open_inodes
Definition: inode.c:54
void list_init(struct list *)
Definition: list.c:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_length()

off_t inode_length ( const struct inode inode)
311 {
312 return inode->data.length;
313}
Here is the caller graph for this function:

◆ inode_open()

struct inode * inode_open ( disk_sector_t  sector)
103 {
104 struct list_elem *e;
105 struct inode *inode;
106
107 /* Check whether this inode is already open. */
108 for (e = list_begin (&open_inodes); e != list_end (&open_inodes);
109 e = list_next (e)) {
110 inode = list_entry (e, struct inode, elem);
111 if (inode->sector == sector) {
113 return inode;
114 }
115 }
116
117 /* Allocate memory. */
118 inode = malloc (sizeof *inode);
119 if (inode == NULL)
120 return NULL;
121
122 /* Initialize. */
125 inode->open_cnt = 1;
127 inode->removed = false;
129 return inode;
130}
void disk_read(struct disk *d, disk_sector_t sec_no, void *buffer)
Definition: disk.c:211
struct inode * inode_reopen(struct inode *inode)
Definition: inode.c:134
struct list_elem * list_begin(struct list *)
Definition: list.c:68
struct list_elem * list_next(struct list_elem *)
Definition: list.c:77
void list_push_front(struct list *, struct list_elem *)
Definition: list.c:195
struct list_elem * list_end(struct list *)
Definition: list.c:88
#define list_entry(LIST_ELEM, STRUCT, MEMBER)
Definition: list.h:103
void * malloc(size_t) __attribute__((malloc))
Definition: malloc.c:85
Definition: list.h:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_read_at()

off_t inode_read_at ( struct inode inode,
void *  buffer_,
off_t  size,
off_t  offset 
)
183 {
184 uint8_t *buffer = buffer_;
185 off_t bytes_read = 0;
186 uint8_t *bounce = NULL;
187
188 while (size > 0) {
189 /* Disk sector to read, starting byte offset within sector. */
190 disk_sector_t sector_idx = byte_to_sector (inode, offset);
191 int sector_ofs = offset % DISK_SECTOR_SIZE;
192
193 /* Bytes left in inode, bytes left in sector, lesser of the two. */
194 off_t inode_left = inode_length (inode) - offset;
195 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
196 int min_left = inode_left < sector_left ? inode_left : sector_left;
197
198 /* Number of bytes to actually copy out of this sector. */
199 int chunk_size = size < min_left ? size : min_left;
200 if (chunk_size <= 0)
201 break;
202
203 if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) {
204 /* Read full sector directly into caller's buffer. */
205 disk_read (filesys_disk, sector_idx, buffer + bytes_read);
206 } else {
207 /* Read sector into bounce buffer, then partially copy
208 * into caller's buffer. */
209 if (bounce == NULL) {
210 bounce = malloc (DISK_SECTOR_SIZE);
211 if (bounce == NULL)
212 break;
213 }
214 disk_read (filesys_disk, sector_idx, bounce);
215 memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
216 }
217
218 /* Advance. */
219 size -= chunk_size;
220 offset += chunk_size;
221 bytes_read += chunk_size;
222 }
223 free (bounce);
224
225 return bytes_read;
226}
uint32_t disk_sector_t
Definition: disk.h:12
off_t inode_length(const struct inode *inode)
Definition: inode.c:311
static disk_sector_t byte_to_sector(const struct inode *inode, off_t pos)
Definition: inode.c:44
static struct intq buffer
Definition: input.c:7
int32_t off_t
Definition: off_t.h:9
unsigned char uint8_t
Definition: stdint.h:20
void * memcpy(void *, const void *, size_t)
Definition: string.c:7
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_remove()

void inode_remove ( struct inode inode)
174 {
175 ASSERT (inode != NULL);
176 inode->removed = true;
177}
Here is the caller graph for this function:

◆ inode_reopen()

struct inode * inode_reopen ( struct inode inode)
134 {
135 if (inode != NULL)
136 inode->open_cnt++;
137 return inode;
138}
Here is the caller graph for this function:

◆ inode_write_at()

off_t inode_write_at ( struct inode inode,
const void *  buffer_,
off_t  size,
off_t  offset 
)
235 {
236 const uint8_t *buffer = buffer_;
237 off_t bytes_written = 0;
238 uint8_t *bounce = NULL;
239
241 return 0;
242
243 while (size > 0) {
244 /* Sector to write, starting byte offset within sector. */
245 disk_sector_t sector_idx = byte_to_sector (inode, offset);
246 int sector_ofs = offset % DISK_SECTOR_SIZE;
247
248 /* Bytes left in inode, bytes left in sector, lesser of the two. */
249 off_t inode_left = inode_length (inode) - offset;
250 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
251 int min_left = inode_left < sector_left ? inode_left : sector_left;
252
253 /* Number of bytes to actually write into this sector. */
254 int chunk_size = size < min_left ? size : min_left;
255 if (chunk_size <= 0)
256 break;
257
258 if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) {
259 /* Write full sector directly to disk. */
260 disk_write (filesys_disk, sector_idx, buffer + bytes_written);
261 } else {
262 /* We need a bounce buffer. */
263 if (bounce == NULL) {
264 bounce = malloc (DISK_SECTOR_SIZE);
265 if (bounce == NULL)
266 break;
267 }
268
269 /* If the sector contains data before or after the chunk
270 we're writing, then we need to read in the sector
271 first. Otherwise we start with a sector of all zeros. */
272 if (sector_ofs > 0 || chunk_size < sector_left)
273 disk_read (filesys_disk, sector_idx, bounce);
274 else
275 memset (bounce, 0, DISK_SECTOR_SIZE);
276 memcpy (bounce + sector_ofs, buffer + bytes_written, chunk_size);
277 disk_write (filesys_disk, sector_idx, bounce);
278 }
279
280 /* Advance. */
281 size -= chunk_size;
282 offset += chunk_size;
283 bytes_written += chunk_size;
284 }
285 free (bounce);
286
287 return bytes_written;
288}
void * memset(void *, int, size_t)
Definition: string.c:258
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ open_inodes

struct list open_inodes
static