Skip to content

Commit

Permalink
Add ability to keep proc entry metadata
Browse files Browse the repository at this point in the history
entries and their metadirectories may sometimes need to keep some extra
data around for file operations in them, so this lets them define
functions that can refcount this.
  • Loading branch information
saagarjha committed Jan 23, 2022
1 parent d1460b0 commit 6ba6f5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 29 additions & 1 deletion fs/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static int proc_lookup(const char *path, struct proc_entry *entry) {
unsigned long index = 0;
struct proc_entry next_entry;
char entry_name[MAX_NAME];
if (entry->meta->ref) {
entry->meta->ref(entry);
}
while (proc_dir_read(entry, &index, &next_entry)) {
// tack on some dynamically generated attributes
if (next_entry.meta->parent == NULL)
Expand All @@ -30,8 +33,14 @@ static int proc_lookup(const char *path, struct proc_entry *entry) {
if (strcmp(entry_name, component) == 0)
goto found;
}
if (entry->meta->unref) {
entry->meta->unref(entry);
}
return _ENOENT;
found:
if (entry->meta->unref) {
entry->meta->unref(entry);
}
*entry = next_entry;
}
return err;
Expand All @@ -44,6 +53,9 @@ static struct fd *proc_open(struct mount *UNUSED(mount), const char *path, int U
int err = proc_lookup(path, &entry);
if (err < 0)
return ERR_PTR(err);
if (entry.meta->ref) {
entry.meta->ref(&entry);
}
struct fd *fd = fd_create(&procfs_fdops);
fd->proc.entry = entry;
fd->proc.data.data = NULL;
Expand Down Expand Up @@ -74,7 +86,14 @@ static int proc_stat(struct mount *UNUSED(mount), const char *path, struct statb
int err = proc_lookup(path, &entry);
if (err < 0)
return err;
return proc_entry_stat(&entry, stat);
if (entry.meta->ref) {
entry.meta->ref(&entry);
}
int ret = proc_entry_stat(&entry, stat);
if (entry.meta->unref) {
entry.meta->unref(&entry);
}
return ret;
}

static int proc_fstat(struct fd *fd, struct statbuf *stat) {
Expand Down Expand Up @@ -165,6 +184,9 @@ static int proc_readdir(struct fd *fd, struct dir_entry *entry) {
static int proc_close(struct fd *fd) {
if (fd->proc.data.data != NULL)
free(fd->proc.data.data);
if (fd->proc.entry.meta->unref) {
fd->proc.entry.meta->unref(&fd->proc.entry);
}
return 0;
}

Expand All @@ -184,7 +206,13 @@ static ssize_t proc_readlink(struct mount *UNUSED(mount), const char *path, char
return _EINVAL;

char target[MAX_PATH + 1];
if (entry.meta->ref) {
entry.meta->ref(&entry);
}
err = entry.meta->readlink(&entry, target);
if (entry.meta->unref) {
entry.meta->unref(&entry);
}
if (err < 0)
return err;
if (bufsize > strlen(target))
Expand Down
4 changes: 4 additions & 0 deletions fs/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct proc_dir_entry {
const char *name;
mode_t_ mode;

// files that need extra resource management
void (*ref)(struct proc_entry *entry);
void (*unref)(struct proc_entry *entry);

// file with dynamic name
void (*getname)(struct proc_entry *entry, char *buf);

Expand Down

0 comments on commit 6ba6f5b

Please sign in to comment.