27 struct ext2 *desc = node->
p;
35 if ((
size_t) offset >= inode.
size)
44 char *block =
kmalloc(bs, &M_BUFFER, 0);
53 size_t off = (offset + done) % bs;
54 size_t len =
MIN(bs - off,
size - done);
59 if ((r =
iobuf_out(io, block + off, len)) < 0)
70 return done ? (
ssize_t) done : r;
87 vfs_log(
LOG_DEBUG,
"ext2_write(inode=%p, offset=%d, size=%d)\n", node, offset,
size);
90 struct ext2 *desc = node->
p;
122 char *block =
kmalloc(bs, &M_BUFFER, 0);
130 while (done <
size) {
131 size_t off = (offset + done) % bs;
132 size_t len =
MIN(bs - off,
size - done);
136 if ((off || len < bs) &&
140 if ((r =
iobuf_in(io, block + off, len)) <= 0)
151 if ((
size_t) r < len)
158 if (done <
size && (
size_t) offset +
size > old_size)
162 return done ? (
ssize_t) done : r;
178 if (!S_ISDIR(dir->
mode))
181 struct ext2 *desc = dir->
p;
187 if (!S_ISDIR(inode.
mode))
190 size_t bs = desc->
bs;
191 size_t blocks_nr = inode.
size / bs;
195 char *buf =
kmalloc(bs, &M_BUFFER, 0);
196 if (!buf)
return -ENOMEM;
202 for (
size_t i = 0; i < blocks_nr; ++i) {
211 while ((
char *) dentry +
sizeof(*dentry) <= buf + bs) {
215 if ((
char *) dentry->
name + dentry->
length > buf + bs)
225 dirent->d_ino = dentry->
ino;
227 dirent->d_name[dentry->
length] =
'\0';
254 if ((
size_t) len == inode->
size)
258 if ((
size_t) len > inode->
size) {
265 struct ext2 *desc = inode->
p;
267 size_t bs = desc->
bs;
278 char *buf =
kmalloc(bs, &M_BUFFER, 0);
284 memset(buf + len % bs, 0, bs - len % bs);
319 struct ext2 *desc = dir->
p;
418 struct ext2 *desc = dir->
p;
430 dirent->d_ino = inode_nr;
445 struct ext2 *desc = super->
p;
#define DEV(major, minor)
int ext2_bgd_table_rewrite(struct ext2 *desc)
Writes the block group descriptor table back to the disk, after a change to a group's counts.
int ext2_dentry_create(struct vnode *dir, const char *name, ino_t ino, mode_t mode)
Adds an entry to a directory.
int ext2_dentry_find(struct ext2 *desc, struct ext2_inode *inode, const char *name, ino_t *ino)
Finds the inode number of a name in a directory.
int ext2_inode_build(struct ext2 *desc, ino_t ino, struct vnode **ref_inode)
Gets the vnode of an inode: the cached one if it's in use, or a new one made from the inode on the di...
int ext2_inode_sync(struct vnode *inode)
Writes a vnode's attributes back to its inode on the disk.
ssize_t ext2_write(struct vnode *node, off_t offset, struct iobuf *io)
Writes to a file, which grows if the write goes past its end.
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.
ssize_t ext2_read(struct vnode *node, off_t offset, struct iobuf *io)
Reads from a file.
int ext2_inode_alloc(struct ext2 *desc, ino_t *ino)
Allocates a free inode: its bit is set in its group's inode bitmap, and the free counts go down.
int ext2_vget(struct vnode *super, ino_t ino, struct vnode **ref)
Gets the vnode of an inode, by its number.
int ext2_inode_read(struct ext2 *desc, ino_t inode, struct ext2_inode *ref)
Reads an inode from the disk.
int ext2_trunc(struct vnode *inode, off_t len)
Changes a file's length.
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_inode_block_read(struct ext2 *desc, struct ext2_inode *inode, size_t idx, void *buf)
Reads a block of a file.
#define EXT2_DENTRY_TYPE_DIR
int ext2_vmknod(struct vnode *dir, const char *fn, mode_t mode, dev_t dev, struct uio *uio, struct vnode **ref)
Makes a node in a directory: a new inode, and an entry for it.
int ext2_inode_write(struct ext2 *desc, ino_t inode, struct ext2_inode *i)
Writes an inode to the disk.
ssize_t ext2_readdir(struct vnode *dir, off_t offset, struct dirent *dirent)
Reads a directory's entry at an offset; unused entries don't count.
int ext2_setattr(struct vnode *vnode)
Stores a vnode's attributes (mode, owner, times) in its inode.
int ext2_finddir(struct vnode *dir, const char *fn, struct dirent *dirent)
Finds the entry of a directory that has a name.
int ext2_inode_block_write(struct ext2 *desc, struct ext2_inode *inode, uint32_t inode_nr, size_t idx, void *buf)
Writes a block of a file, allocating it and the indirect blocks above it if they don't exist yet.
ssize_t iobuf_out(struct iobuf *io, const void *src, size_t n)
Copies bytes from the kernel into the iobuf's buffer, and moves past them.
ssize_t iobuf_in(struct iobuf *io, void *dst, size_t n)
Copies bytes from the iobuf's buffer into the kernel, and moves past them.
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
void kfree(void *)
Frees an object that kmalloc() gave.
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
A device driver: its operations, any of which may be NULL.
A directory entry, as it is on the disk.
uint16_t size
entry total size
uint8_t name[0]
the name follows (not NUL-terminated)
uint8_t length
name length
uint16_t directories_count
An inode, as it is on the disk.
uint32_t direct_pointer[12]
uint32_t ginodes
number of inodes per block group
A mounted ext2 filesystem.
struct ext2_superblock * superblock
struct ext2_group * groups
int filetype
directory entries have their file's type (EXT2_INCOMPAT_FILETYPE); without it, the byte is 0
Who does a filesystem operation: where its paths start, and its credentials and umask.
A file in memory (a vnode): what its inode says, and the kernel's own state.
void * p
the filesystem's own data
int gettime(struct timespec *ts)
Reads the wall clock (CLOCK_REALTIME).