PKUOS - Pintos
Pintos source browser for PKU Operating System course
Data Structures | Macros | Functions | Variables
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:

Go to the source code of this file.

Data Structures

struct  inode_disk
 On-disk inode. More...
 
struct  inode
 In-memory inode. More...
 

Macros

#define INODE_MAGIC   0x494e4f44
 Identifies an inode. More...
 

Functions

static size_t bytes_to_sectors (off_t size)
 Returns the number of sectors to allocate for an inode SIZE bytes long. More...
 
static block_sector_t byte_to_sector (const struct inode *inode, off_t pos)
 Returns the block device sector that contains byte offset POS within INODE. More...
 
void inode_init (void)
 Initializes the inode module. More...
 
bool inode_create (block_sector_t sector, off_t length)
 Initializes an inode with LENGTH bytes of data and writes the new inode to sector SECTOR on the file system device. More...
 
struct inodeinode_open (block_sector_t sector)
 Reads an inode from SECTOR and returns a ‘struct inode’ that contains it. More...
 
struct inodeinode_reopen (struct inode *inode)
 Reopens and returns INODE. More...
 
block_sector_t inode_get_inumber (const struct inode *inode)
 Returns INODE's inode number. More...
 
void inode_close (struct inode *inode)
 Closes INODE and writes it to disk. More...
 
void inode_remove (struct inode *inode)
 Marks INODE to be deleted when it is closed by the last caller who has it open. More...
 
off_t inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset)
 Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET. More...
 
off_t inode_write_at (struct inode *inode, const void *buffer_, off_t size, off_t offset)
 Writes SIZE bytes from BUFFER into INODE, starting at OFFSET. More...
 
void inode_deny_write (struct inode *inode)
 Disables writes to INODE. More...
 
void inode_allow_write (struct inode *inode)
 Re-enables writes to INODE. More...
 
off_t inode_length (const struct inode *inode)
 Returns the length, in bytes, of INODE's data. More...
 

Variables

static struct list open_inodes
 List of open inodes, so that opening a single inode twice returns the same ‘struct inode’. More...
 

Macro Definition Documentation

◆ INODE_MAGIC

#define INODE_MAGIC   0x494e4f44

Identifies an inode.

Definition at line 11 of file inode.c.

Function Documentation

◆ byte_to_sector()

static block_sector_t byte_to_sector ( const struct inode inode,
off_t  pos 
)
static

Returns the block device sector that contains byte offset POS within INODE.

Returns -1 if INODE does not contain data for a byte at offset POS.

Definition at line 47 of file inode.c.

References ASSERT, BLOCK_SECTOR_SIZE, inode::data, NULL, and inode_disk::start.

Referenced by inode_read_at(), and inode_write_at().

Here is the caller graph for this function:

◆ bytes_to_sectors()

static size_t bytes_to_sectors ( off_t  size)
inlinestatic

Returns the number of sectors to allocate for an inode SIZE bytes long.

Definition at line 26 of file inode.c.

References BLOCK_SECTOR_SIZE, and DIV_ROUND_UP.

Referenced by inode_close(), and inode_create().

Here is the caller graph for this function:

◆ inode_allow_write()

void inode_allow_write ( struct inode inode)

Re-enables writes to INODE.

Must be called once by each inode opener who has called inode_deny_write() on the inode, before closing the inode.

Definition at line 333 of file inode.c.

References ASSERT, inode::deny_write_cnt, and inode::open_cnt.

Referenced by file_allow_write().

Here is the caller graph for this function:

◆ inode_close()

void inode_close ( struct inode inode)

Closes INODE and writes it to disk.

If this was the last reference to INODE, frees its memory. If INODE was also a removed inode, frees its blocks.

Definition at line 164 of file inode.c.

References bytes_to_sectors(), inode::data, inode::elem, free(), free_map_release(), inode_disk::length, list_remove(), NULL, inode::open_cnt, inode::removed, inode::sector, and inode_disk::start.

Referenced by dir_close(), dir_open(), dir_remove(), file_close(), and file_open().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_create()

bool inode_create ( block_sector_t  sector,
off_t  length 
)

Initializes an inode with LENGTH bytes of data and writes the new inode to sector SECTOR on the file system device.

Returns true if successful. Returns false if memory or disk allocation fails.

Definition at line 73 of file inode.c.

References ASSERT, BLOCK_SECTOR_SIZE, block_write(), bytes_to_sectors(), calloc(), free(), free_map_allocate(), fs_device, INODE_MAGIC, inode_disk::length, inode_disk::magic, NULL, and inode_disk::start.

Referenced by dir_create(), filesys_create(), and free_map_create().

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)

Disables writes to INODE.

May be called at most once per inode opener.

Definition at line 323 of file inode.c.

References ASSERT, inode::deny_write_cnt, and inode::open_cnt.

Referenced by file_deny_write().

Here is the caller graph for this function:

◆ inode_get_inumber()

block_sector_t inode_get_inumber ( const struct inode inode)

Returns INODE's inode number.

Definition at line 155 of file inode.c.

References inode::sector.

◆ inode_init()

void inode_init ( void  )

Initializes the inode module.

Definition at line 62 of file inode.c.

References list_init(), and open_inodes.

Referenced by filesys_init().

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)

Returns the length, in bytes, of INODE's data.

filesys/inode.h

Definition at line 342 of file inode.c.

References inode::data, and inode_disk::length.

Referenced by file_length(), inode_read_at(), and inode_write_at().

Here is the caller graph for this function:

◆ inode_open()

struct inode * inode_open ( block_sector_t  sector)

Reads an inode from SECTOR and returns a ‘struct inode’ that contains it.

Returns a null pointer if memory allocation fails.

Definition at line 112 of file inode.c.

References block_read(), inode::data, inode::deny_write_cnt, inode::elem, fs_device, inode_reopen(), list_begin(), list_end(), list_entry, list_next(), list_push_front(), malloc(), NULL, inode::open_cnt, open_inodes, inode::removed, and inode::sector.

Referenced by dir_lookup(), dir_open_root(), dir_remove(), free_map_create(), and free_map_open().

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 
)

Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.

Returns the number of bytes actually read, which may be less than SIZE if an error occurs or end of file is reached.

Definition at line 201 of file inode.c.

References block_read(), BLOCK_SECTOR_SIZE, buffer, byte_to_sector(), free(), fs_device, inode_length(), malloc(), memcpy(), and NULL.

Referenced by dir_add(), dir_readdir(), file_read(), file_read_at(), and lookup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inode_remove()

void inode_remove ( struct inode inode)

Marks INODE to be deleted when it is closed by the last caller who has it open.

Definition at line 191 of file inode.c.

References ASSERT, NULL, and inode::removed.

Referenced by dir_remove().

Here is the caller graph for this function:

◆ inode_reopen()

struct inode * inode_reopen ( struct inode inode)

Reopens and returns INODE.

Definition at line 146 of file inode.c.

References NULL, and inode::open_cnt.

Referenced by dir_reopen(), file_reopen(), and inode_open().

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 
)

Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.

Returns the number of bytes actually written, which may be less than SIZE if end of file is reached or an error occurs. (Normally a write at end of file would extend the inode, but growth is not yet implemented.)

Definition at line 258 of file inode.c.

References block_read(), BLOCK_SECTOR_SIZE, block_write(), buffer, byte_to_sector(), inode::deny_write_cnt, free(), fs_device, inode_length(), malloc(), memcpy(), memset(), and NULL.

Referenced by dir_add(), dir_remove(), file_write(), and file_write_at().

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

List of open inodes, so that opening a single inode twice returns the same ‘struct inode’.

Definition at line 58 of file inode.c.

Referenced by inode_init(), and inode_open().