AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
remove.c File Reference

ext2: removing, linking and renaming entries, and freeing what a file used. More...

#include <ext2.h>
#include <core/time.h>
#include <ds/bitmap.h>
#include <fs/vcache.h>

Go to the source code of this file.

Functions

static uint32_t free_tree (struct ext2 *desc, struct ext2_inode *inode, uint32_t block, int depth, size_t from)
 Frees the blocks of the tree at block (a data block at depth 0, or an indirect block depth levels above the data), from data block from of the tree on, and counts them out of the inode's blocks.
void ext2_blocks_free (struct ext2 *desc, struct ext2_inode *inode, size_t from)
 Frees a file's blocks, from a block on, when the file is truncated or removed.
static void ext2_inode_free (struct ext2 *desc, ino_t ino, int dir)
 Marks an inode free in its group's inode bitmap, and counts it as free.
void ext2_inode_discard (struct ext2 *desc, ino_t ino)
 Frees an inode that has no links left, and the blocks it uses.
int ext2_close (struct vnode *vnode)
 Releases a vnode when its last reference goes.
static int dentry_update (struct vnode *dir, const char *name, void(*fn)(struct ext2_dentry *d, struct ext2_dentry *prev, void *arg), void *arg, ino_t *ino)
 Finds the entry name of directory dir, calls fn on it, and writes its block back.
static void dentry_remove (struct ext2_dentry *d, struct ext2_dentry *prev, void *arg)
 Removes an entry: the previous entry takes its space, or, for the first entry of a block, it's marked unused.
static void dentry_set_ino (struct ext2_dentry *d, struct ext2_dentry *prev, void *arg)
 Points an entry to another inode, the one arg points to.
int ext2_vunlink (struct vnode *dir, const char *fn, struct uio *uio)
 Removes an entry from a directory, and drops a link to its file.
int ext2_vlink (struct vnode *dir, const char *fn, struct vnode *vnode, struct uio *uio)
 Adds an entry for an existing file (a hard link) to a directory.
int ext2_vrename (struct vnode *odir, const char *ofn, struct vnode *ndir, const char *nfn, struct uio *uio)
 Renames an entry, possibly to another directory, replacing an entry at the new name (the VFS checked its type already).

Detailed Description

ext2: removing, linking and renaming entries, and freeing what a file used.

Unlinking only drops the file's link count. The inode and its blocks are freed when the vnode is closed for the last time (ext2_close()), so a file that is still open stays readable.

Definition in file remove.c.

Function Documentation

◆ dentry_remove()

void dentry_remove ( struct ext2_dentry * d,
struct ext2_dentry * prev,
void * arg )
static

Removes an entry: the previous entry takes its space, or, for the first entry of a block, it's marked unused.

Definition at line 226 of file remove.c.

References dentry_remove(), ext2_dentry::ino, and ext2_dentry::size.

Referenced by dentry_remove(), ext2_vrename(), and ext2_vunlink().

◆ dentry_set_ino()

void dentry_set_ino ( struct ext2_dentry * d,
struct ext2_dentry * prev,
void * arg )
static

Points an entry to another inode, the one arg points to.

Definition at line 235 of file remove.c.

References dentry_set_ino(), and ext2_dentry::ino.

Referenced by dentry_set_ino(), and ext2_vrename().

◆ dentry_update()

int dentry_update ( struct vnode * dir,
const char * name,
void(* fn )(struct ext2_dentry *d, struct ext2_dentry *prev, void *arg),
void * arg,
ino_t * ino )
static

Finds the entry name of directory dir, calls fn on it, and writes its block back.

fn gets the entry, the entry before it in its block (NULL if it's the first), and arg. The entry's inode number goes in *ino. Returns 0, -ENOENT, or a negative errno.

Definition at line 179 of file remove.c.

References ext2::bs, dentry_update(), ext2_inode_block_read(), ext2_inode_block_write(), ext2_inode_read(), ext2_dentry::ino, vnode::ino, kfree(), kmalloc(), ext2_dentry::length, memcmp(), ext2_dentry::name, NULL, vnode::p, ext2_dentry::size, and ext2_inode::size.

Referenced by dentry_update(), ext2_vrename(), and ext2_vunlink().

◆ ext2_blocks_free()

void ext2_blocks_free ( struct ext2 * desc,
struct ext2_inode * inode,
size_t from )

Frees a file's blocks, from a block on, when the file is truncated or removed.

Parameters
descthe mounted filesystem
inodethe file's inode, its block pointers cleared here
fromthe index of the first block to free

Definition at line 69 of file remove.c.

References ext2_inode::blocks, ext2::bs, ext2_inode::direct_pointer, ext2_inode::doubly_indirect_pointer, ext2_blocks_free(), EXT2_DIRECT_POINTERS, free_tree(), ext2_inode::mode, ext2_inode::singly_indirect_pointer, and ext2_inode::triply_indirect_pointer.

Referenced by ext2_blocks_free(), ext2_inode_discard(), and ext2_trunc().

◆ ext2_close()

int ext2_close ( struct vnode * vnode)

Releases a vnode when its last reference goes.

A file that was unlinked while it was open is freed now, with its inode and blocks.

Parameters
vnodethe vnode
Returns
0

Definition at line 158 of file remove.c.

References ext2_close(), ext2_inode_discard(), vnode::ino, kfree(), vnode::nlink, vnode::p, ext2::vcache, vcache_remove(), and vnode_pages_free().

Referenced by ext2_close().

◆ ext2_inode_discard()

void ext2_inode_discard ( struct ext2 * desc,
ino_t ino )

Frees an inode that has no links left, and the blocks it uses.

Parameters
descthe mounted filesystem
inothe inode number

Definition at line 131 of file remove.c.

References ext2_inode::dtime, ext2_blocks_free(), ext2_inode_discard(), ext2_inode_free(), ext2_inode_read(), ext2_inode_write(), gettime(), ext2_inode::mode, ext2_inode::nlink, ext2_inode::size, and timespec::tv_sec.

Referenced by ext2_close(), ext2_inode_discard(), and ext2_vmknod().

◆ ext2_inode_free()

void ext2_inode_free ( struct ext2 * desc,
ino_t ino,
int dir )
static

Marks an inode free in its group's inode bitmap, and counts it as free.

If the bitmap can't be read, the inode stays used.

Definition at line 94 of file remove.c.

References ext2::bs, ext2_group::directories_count, ext2_bgd_table_rewrite(), ext2_block_read(), ext2_block_write(), ext2_inode_free(), ext2_superblock_rewrite(), ext2_group::free_inodes, ext2_superblock::free_inodes, ext2_superblock::ginodes, ext2::groups, ext2_group::imap, kfree(), kmalloc(), and ext2::superblock.

Referenced by ext2_inode_discard(), and ext2_inode_free().

◆ ext2_vlink()

int ext2_vlink ( struct vnode * dir,
const char * fn,
struct vnode * vnode,
struct uio * uio )

Adds an entry for an existing file (a hard link) to a directory.

Parameters
dirthe directory
fnthe new name
vnodethe file
uiowho is asking
Returns
0, -EEXIST, or a negative errno

Definition at line 298 of file remove.c.

References vnode::ctime, ext2_dentry_create(), ext2_dentry_find(), ext2_inode_read(), ext2_inode_sync(), ext2_vlink(), gettime(), vnode::ino, vnode::mode, vnode::nlink, and vnode::p.

Referenced by ext2_vlink().

◆ ext2_vrename()

int ext2_vrename ( struct vnode * odir,
const char * ofn,
struct vnode * ndir,
const char * nfn,
struct uio * uio )

Renames an entry, possibly to another directory, replacing an entry at the new name (the VFS checked its type already).

A directory that moves gets its .. pointed to its new parent.

Parameters
odirthe directory the entry is in
ofnits name
ndirthe directory it goes to
nfnits new name
uiowho is asking
Returns
0, -ENOENT, or a negative errno

Definition at line 337 of file remove.c.

References vnode::ctime, dentry_remove(), dentry_set_ino(), dentry_update(), ext2_dentry_create(), ext2_dentry_find(), ext2_inode_build(), ext2_inode_read(), ext2_inode_sync(), ext2_vrename(), ext2_vunlink(), gettime(), vnode::ino, vnode::mode, vnode::mtime, vnode::nlink, NULL, and vnode::p.

Referenced by ext2_vrename().

◆ ext2_vunlink()

int ext2_vunlink ( struct vnode * dir,
const char * fn,
struct uio * uio )

Removes an entry from a directory, and drops a link to its file.

The file is freed when it has no links and nothing has it open.

Parameters
dirthe directory
fnthe entry's name
uiowho is asking
Returns
0, -ENOENT, or a negative errno

Definition at line 249 of file remove.c.

References vnode::ctime, dentry_remove(), dentry_update(), ext2_inode_build(), ext2_inode_sync(), ext2_vunlink(), gettime(), vnode::mode, vnode::mtime, vnode::nlink, NULL, vnode::p, vnode::ref, and vfs_close().

Referenced by ext2_vrename(), and ext2_vunlink().

◆ free_tree()

uint32_t free_tree ( struct ext2 * desc,
struct ext2_inode * inode,
uint32_t block,
int depth,
size_t from )
static

Frees the blocks of the tree at block (a data block at depth 0, or an indirect block depth levels above the data), from data block from of the tree on, and counts them out of the inode's blocks.

Returns the block, or 0 if it was freed.

Definition at line 21 of file remove.c.

References ext2_inode::blocks, ext2::bs, ext2_block_free(), ext2_block_read(), ext2_block_write(), free_tree(), kfree(), kmalloc(), and table().