printk("vfs_init() completed\n");
}
+/* FS's can provide another, if they want */
+int generic_dentry_hash(struct dentry *dentry, struct qstr *qstr)
+{
+ unsigned long hash = 5381;
+
+ for (int i = 0; i < qstr->len; i++) {
+ /* hash * 33 + c, djb2's technique */
+ hash = ((hash << 5) + hash) + qstr->name[i];
+ }
+ return hash;
+}
+
/* Builds / populates the qstr of a dentry based on its d_iname. If there is an
* l_name, (long), it will use that instead of the inline name. This will
* probably change a bit. */
void qstr_builder(struct dentry *dentry, char *l_name)
{
dentry->d_name.name = l_name ? l_name : dentry->d_iname;
- // TODO: pending what we actually do in d_hash
- //dentry->d_name.hash = dentry->d_op->d_hash(dentry, &dentry->d_name);
- dentry->d_name.hash = 0xcafebabe;
dentry->d_name.len = strnlen(dentry->d_name.name, MAX_FILENAME_SZ);
+ dentry->d_name.hash = dentry->d_op->d_hash(dentry, &dentry->d_name);
}
/* Useful little helper - return the string ptr for a given file */
int retval;
printd("Path lookup for %s\n", path);
/* we allow absolute lookups with no process context */
+ /* TODO: RCU read lock on pwd or kref_not_zero in a loop. concurrent chdir
+ * could decref nd->dentry before we get to incref it below. */
if (path[0] == '/') { /* absolute lookup */
if (!current)
nd->dentry = default_ns.root->mnt_root;
{
/* Build and init the first dentry / inode. The dentry ref is stored later
* by vfsmount's mnt_root. The parent is dealt with later. */
- struct dentry *d_root = get_dentry(sb, 0, "/"); /* probably right */
+ struct dentry *d_root = get_dentry_with_ops(sb, 0, "/", d_op);
if (!d_root)
panic("OOM! init_sb() can't fail yet!");
/* Dentry Functions */
-/* Helper to alloc and initialize a generic dentry. The following needs to be
- * set still: d_op (if no parent), d_fs_info (opt), d_inode, connect the inode
- * to the dentry (and up the d_kref again), maybe dcache_put(). The inode
- * stitching is done in get_inode() or lookup (depending on the FS).
- * The setting of the d_op might be problematic when dealing with mounts. Just
- * overwrite it.
- *
- * If the name is longer than the inline name, it will kmalloc a buffer, so
- * don't worry about the storage for *name after calling this. */
-struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
- char *name)
+static void dentry_set_name(struct dentry *dentry, char *name)
{
- assert(name);
size_t name_len = strnlen(name, MAX_FILENAME_SZ); /* not including \0! */
- struct dentry *dentry = kmem_cache_alloc(dentry_kcache, 0);
char *l_name = 0;
+ if (name_len < DNAME_INLINE_LEN) {
+ strncpy(dentry->d_iname, name, name_len);
+ dentry->d_iname[name_len] = '\0';
+ qstr_builder(dentry, 0);
+ } else {
+ l_name = kmalloc(name_len + 1, 0);
+ assert(l_name);
+ strncpy(l_name, name, name_len);
+ l_name[name_len] = '\0';
+ qstr_builder(dentry, l_name);
+ }
+}
+
+/* Gets a dentry. If there is no parent, use d_op. Only called directly by
+ * superblock init code. */
+struct dentry *get_dentry_with_ops(struct super_block *sb,
+ struct dentry *parent, char *name,
+ struct dentry_operations *d_op)
+{
+ assert(name);
+ struct dentry *dentry = kmem_cache_alloc(dentry_kcache, 0);
if (!dentry) {
set_errno(ENOMEM);
if (parent) { /* no parent for rootfs mount */
kref_get(&parent->d_kref, 1);
dentry->d_op = parent->d_op; /* d_op set in init_sb for parentless */
+ } else {
+ dentry->d_op = d_op;
}
dentry->d_parent = parent;
dentry->d_flags = DENTRY_USED;
dentry->d_fs_info = 0;
- if (name_len < DNAME_INLINE_LEN) {
- strncpy(dentry->d_iname, name, name_len);
- dentry->d_iname[name_len] = '\0';
- qstr_builder(dentry, 0);
- } else {
- l_name = kmalloc(name_len + 1, 0);
- assert(l_name);
- strncpy(l_name, name, name_len);
- l_name[name_len] = '\0';
- qstr_builder(dentry, l_name);
- }
+ dentry_set_name(dentry, name);
/* Catch bugs by aggressively zeroing this (o/w we use old stuff) */
dentry->d_inode = 0;
return dentry;
}
+/* Helper to alloc and initialize a generic dentry. The following needs to be
+ * set still: d_op (if no parent), d_fs_info (opt), d_inode, connect the inode
+ * to the dentry (and up the d_kref again), maybe dcache_put(). The inode
+ * stitching is done in get_inode() or lookup (depending on the FS).
+ * The setting of the d_op might be problematic when dealing with mounts. Just
+ * overwrite it.
+ *
+ * If the name is longer than the inline name, it will kmalloc a buffer, so
+ * don't worry about the storage for *name after calling this. */
+struct dentry *get_dentry(struct super_block *sb, struct dentry *parent,
+ char *name)
+{
+ return get_dentry_with_ops(sb, parent, name, 0);
+}
+
/* Called when the dentry is unreferenced (after kref == 0). This works closely
* with the resurrection in dcache_get().
*
spin_lock(&sb->s_lru_lock);
TAILQ_REMOVE(&sb->s_lru_d, old, d_lru);
spin_unlock(&sb->s_lru_lock);
+ /* TODO: this seems suspect. isn't this the same memory as key_val?
+ * in which case, we just adjust the flags (remove NEG) and reinsert? */
+ assert(old != key_val); // checking TODO comment
__dentry_free(old);
}
/* this returns 0 on failure (TODO: Fix this ghetto shit) */
* note we don't pass this an nd, like Linux does... */
static struct inode *create_inode(struct dentry *dentry, int mode)
{
+ uint64_t now = epoch_seconds();
/* note it is the i_ino that uniquely identifies a file in the specific
* filesystem. there's a diff between creating an inode (even for an in-use
* ino) and then filling it in, and vs creating a brand new one.
inode->i_nlink = 1;
inode->i_size = 0;
inode->i_blocks = 0;
- inode->i_atime.tv_sec = 0; /* TODO: now! */
- inode->i_ctime.tv_sec = 0;
- inode->i_mtime.tv_sec = 0;
- inode->i_atime.tv_nsec = 0; /* are these supposed to be the extra ns? */
+ inode->i_atime.tv_sec = now;
+ inode->i_ctime.tv_sec = now;
+ inode->i_mtime.tv_sec = now;
+ inode->i_atime.tv_nsec = 0;
inode->i_ctime.tv_nsec = 0;
inode->i_mtime.tv_nsec = 0;
inode->i_bdev = inode->i_sb->s_bdev;
/* Consider pushing some error checking higher in the VFS */
if (!count)
return 0;
- /* Extend the file. Should put more checks in here, and maybe do this per
- * page in the for loop below. */
- if (orig_off + count > file->f_dentry->d_inode->i_size)
- file->f_dentry->d_inode->i_size = orig_off + count;
+ if (file->f_flags & O_APPEND) {
+ spin_lock(&file->f_dentry->d_inode->i_lock);
+ orig_off = file->f_dentry->d_inode->i_size;
+ /* setting the filesize here, instead of during the extend-check, since
+ * we need to atomically reserve space and set our write position. */
+ file->f_dentry->d_inode->i_size += count;
+ spin_unlock(&file->f_dentry->d_inode->i_lock);
+ } else {
+ if (orig_off + count > file->f_dentry->d_inode->i_size) {
+ /* lock for writes to i_size. we allow lockless reads. recheck
+ * i_size in case of concurrent writers since our orig check. */
+ spin_lock(&file->f_dentry->d_inode->i_lock);
+ if (orig_off + count > file->f_dentry->d_inode->i_size)
+ file->f_dentry->d_inode->i_size = orig_off + count;
+ spin_unlock(&file->f_dentry->d_inode->i_lock);
+ }
+ }
page_off = orig_off & (PGSIZE - 1);
first_idx = orig_off >> PGSHIFT;
last_idx = (orig_off + count) >> PGSHIFT;
struct inode *parent_i;
struct nameidata nd_r = {0}, *nd = &nd_r;
int error;
+ unsigned long nr_pages;
/* The file might exist, lets try to just open it right away */
nd->intent = LOOKUP_OPEN;
error = path_lookup(path, LOOKUP_FOLLOW, nd);
if (!error) {
- /* Still need to make sure we didn't want to O_EXCL create */
+ /* If this is a directory, make sure we are opening with O_RDONLY.
+ * Unfortunately we can't just check for O_RDONLY directly because its
+ * value is 0x0. We instead have to make sure it's not O_WRONLY and
+ * not O_RDWR explicitly. */
+ if (S_ISDIR(nd->dentry->d_inode->i_mode) &&
+ ((flags & O_WRONLY) || (flags & O_RDWR))) {
+ set_errno(EISDIR);
+ goto out_path_only;
+ }
+ /* Also need to make sure we didn't want to O_EXCL create */
if ((flags & O_CREAT) && (flags & O_EXCL)) {
set_errno(EEXIST);
goto out_path_only;
kref_get(&file_d->d_kref, 1);
goto open_the_file;
}
+ if (!(flags & O_CREAT)) {
+ set_errno(-error);
+ goto out_path_only;
+ }
/* So it didn't already exist, release the path from the previous lookup,
* and then we try to create it. */
path_release(nd);
file_d = do_lookup(nd->dentry, nd->last.name);
if (!file_d) {
if (!(flags & O_CREAT)) {
+ warn("Extremely unlikely race, probably a bug");
set_errno(ENOENT);
goto out_path_only;
}
/* now open the file (freshly created or if it already existed). At this
* point, file_d is a refcnt'd dentry, regardless of which branch we took.*/
if (flags & O_TRUNC) {
+ spin_lock(&file_d->d_inode->i_lock);
+ nr_pages = ROUNDUP(file_d->d_inode->i_size, PGSIZE) >> PGSHIFT;
file_d->d_inode->i_size = 0;
- /* TODO: probably should remove the garbage pages from the page map */
+ spin_unlock(&file_d->d_inode->i_lock);
+ pm_remove_contig(file_d->d_inode->i_mapping, 0, nr_pages);
}
file = dentry_open(file_d, flags); /* sets errno */
/* Note the fall through to the exit paths. File is 0 by default and if
return -1;
}
+int do_rename(char *old_path, char *new_path)
+{
+ struct nameidata nd_old = {0}, *nd_o = &nd_old;
+ struct nameidata nd_new = {0}, *nd_n = &nd_new;
+ struct dentry *old_dir_d, *new_dir_d;
+ struct inode *old_dir_i, *new_dir_i;
+ struct dentry *old_d, *new_d, *unlink_d;
+ int error;
+ int retval = 0;
+ uint64_t now;
+
+ nd_o->intent = LOOKUP_ACCESS; /* maybe, might need another type */
+
+ /* get the parent, but don't follow links */
+ error = path_lookup(old_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_o);
+ if (error) {
+ set_errno(-error);
+ retval = -1;
+ goto out_old_path;
+ }
+ old_dir_d = nd_o->dentry;
+ old_dir_i = old_dir_d->d_inode;
+
+ old_d = do_lookup(old_dir_d, nd_o->last.name);
+ if (!old_d) {
+ set_errno(ENOENT);
+ retval = -1;
+ goto out_old_path;
+ }
+
+ nd_n->intent = LOOKUP_CREATE;
+ error = path_lookup(new_path, LOOKUP_PARENT | LOOKUP_DIRECTORY, nd_n);
+ if (error) {
+ set_errno(-error);
+ retval = -1;
+ goto out_paths_and_src;
+ }
+ new_dir_d = nd_n->dentry;
+ new_dir_i = new_dir_d->d_inode;
+ /* TODO if new_dir == old_dir, we might be able to simplify things */
+
+ if (new_dir_i->i_sb != old_dir_i->i_sb) {
+ set_errno(EXDEV);
+ retval = -1;
+ goto out_paths_and_src;
+ }
+ /* TODO: check_perms is lousy, want to just say "writable" here */
+ if (check_perms(old_dir_i, S_IWUSR) || check_perms(new_dir_i, S_IWUSR)) {
+ set_errno(EPERM);
+ retval = -1;
+ goto out_paths_and_src;
+ }
+ /* TODO: if we're doing a rename that moves a directory, we need to make
+ * sure the new_path doesn't include the old_path. it's not as simple as
+ * just checking, since there could be a concurrent rename that breaks the
+ * check later. e.g. what if new_dir's parent is being moved into a child
+ * of old_dir?
+ *
+ * linux has a per-fs rename mutex for these scenarios, so only one can
+ * proceed at a time. i don't see another way to deal with it either.
+ * maybe something like flagging all dentries on the new_path with "do not
+ * move". */
+
+ /* TODO: this is all very racy. right after we do a new_d lookup, someone
+ * else could create or unlink new_d. need to lock here, or else push this
+ * into the sub-FS.
+ *
+ * For any locking scheme, we probably need to lock both the old and new
+ * dirs. To prevent deadlock, we need a total ordering of all inodes (or
+ * dentries, if we locking them instead). inode number or struct inode*
+ * will work for this. */
+ new_d = do_lookup(new_dir_d, nd_n->last.name);
+ if (new_d) {
+ if (new_d->d_inode == old_d->d_inode)
+ goto out_paths_and_refs; /* rename does nothing */
+ /* TODO: Here's a bunch of other racy checks we need to do, maybe in the
+ * sub-FS:
+ *
+ * if src is a dir, dst must be an empty dir if it exists (RACYx2)
+ * racing on dst being created and it getting new entries
+ * if src is a file, dst must be a file if it exists (RACY)
+ * racing on dst being created and still being a file
+ * racing on dst being unlinked and a new one being added
+ */
+ /* TODO: we should allow empty dirs */
+ if (S_ISDIR(new_d->d_inode->i_mode)) {
+ set_errno(EISDIR);
+ retval = -1;
+ goto out_paths_and_refs;
+ }
+ /* TODO: need this to be atomic with rename */
+ error = new_dir_i->i_op->unlink(new_dir_i, new_d);
+ if (error) {
+ set_errno(-error);
+ retval = -1;
+ goto out_paths_and_refs;
+ }
+ new_d->d_flags |= DENTRY_DYING;
+ /* TODO: racy with other lookups on new_d */
+ dcache_remove(new_d->d_sb, new_d);
+ new_d->d_inode->i_nlink--; /* TODO: race here, esp with a decref */
+ kref_put(&new_d->d_kref);
+ }
+ /* new_d is just a vessel for the name. somewhat lousy. */
+ new_d = get_dentry(new_dir_d->d_sb, new_dir_d, nd_n->last.name);
+
+ /* TODO: more races. need to remove old_d from the dcache, since we're
+ * about to change its parentage. could be readded concurrently. */
+ dcache_remove(old_dir_d->d_sb, old_d);
+ error = new_dir_i->i_op->rename(old_dir_i, old_d, new_dir_i, new_d);
+ if (error) {
+ /* TODO: oh crap, we already unlinked! now we're screwed, and violated
+ * our atomicity requirements. */
+ printk("[kernel] rename failed, you might have lost data\n");
+ set_errno(-error);
+ retval = -1;
+ goto out_paths_and_refs;
+ }
+
+ /* old_dir loses old_d, new_dir gains old_d, renamed to new_d. this is
+ * particularly cumbersome since there are two levels here: the FS has its
+ * info about where things are, and the VFS has its dentry tree. and it's
+ * all racy (TODO). */
+ dentry_set_name(old_d, new_d->d_name.name);
+ old_d->d_parent = new_d->d_parent;
+ if (S_ISDIR(old_d->d_inode->i_mode)) {
+ TAILQ_REMOVE(&old_dir_d->d_subdirs, old_d, d_subdirs_link);
+ old_dir_i->i_nlink--; /* TODO: racy, etc */
+ TAILQ_INSERT_TAIL(&new_dir_d->d_subdirs, old_d, d_subdirs_link);
+ new_dir_i->i_nlink--; /* TODO: racy, etc */
+ }
+
+ /* and then the third level: dcache stuff. we could have old versions of
+ * old_d or negative versions of new_d sitting around. dcache_put should
+ * replace a potentially negative dentry for new_d (now called old_d) */
+ dcache_put(old_dir_d->d_sb, old_d);
+
+ /* TODO could have a helper for this, but it's going away soon */
+ now = epoch_seconds();
+ old_dir_i->i_ctime.tv_sec = now;
+ old_dir_i->i_mtime.tv_sec = now;
+ old_dir_i->i_ctime.tv_nsec = 0;
+ old_dir_i->i_mtime.tv_nsec = 0;
+ new_dir_i->i_ctime.tv_sec = now;
+ new_dir_i->i_mtime.tv_sec = now;
+ new_dir_i->i_ctime.tv_nsec = 0;
+ new_dir_i->i_mtime.tv_nsec = 0;
+
+ /* fall-through */
+out_paths_and_refs:
+ kref_put(&new_d->d_kref);
+out_paths_and_src:
+ kref_put(&old_d->d_kref);
+out_paths:
+ path_release(nd_n);
+out_old_path:
+ path_release(nd_o);
+ return retval;
+}
+
+int do_truncate(struct inode *inode, off64_t len)
+{
+ off64_t old_len;
+ uint64_t now;
+ if (len < 0) {
+ set_errno(EINVAL);
+ return -1;
+ }
+ if (len > PiB) {
+ printk("[kernel] truncate for > petabyte, probably a bug\n");
+ /* continuing, not too concerned. could set EINVAL or EFBIG */
+ }
+ spin_lock(&inode->i_lock);
+ old_len = inode->i_size;
+ if (old_len == len) {
+ spin_unlock(&inode->i_lock);
+ return 0;
+ }
+ inode->i_size = len;
+ /* truncate can't block, since we're holding the spinlock. but it can rely
+ * on that lock being held */
+ inode->i_op->truncate(inode);
+ spin_unlock(&inode->i_lock);
+
+ if (old_len < len) {
+ pm_remove_contig(inode->i_mapping, old_len >> PGSHIFT,
+ (len >> PGSHIFT) - (old_len >> PGSHIFT));
+ }
+ now = epoch_seconds();
+ inode->i_ctime.tv_sec = now;
+ inode->i_mtime.tv_sec = now;
+ inode->i_ctime.tv_nsec = 0;
+ inode->i_mtime.tv_nsec = 0;
+ return 0;
+}
+
struct file *alloc_file(void)
{
struct file *file = kmem_cache_alloc(file_kcache, 0);
}
/* Grow the vfs fd set */
-static int grow_fd_set(struct files_struct *open_files) {
+static int grow_fd_set(struct files_struct *open_files)
+{
int n;
struct file_desc *nfd, *ofd;
/* Grow the open_files->fd array in increments of NR_OPEN_FILES_DEFAULT */
n = open_files->max_files + NR_OPEN_FILES_DEFAULT;
if (n > NR_FILE_DESC_MAX)
- n = NR_FILE_DESC_MAX;
+ return -EMFILE;
nfd = kzmalloc(n * sizeof(struct file_desc), 0);
if (nfd == NULL)
- return -1;
+ return -ENOMEM;
/* Move the old array on top of the new one */
ofd = open_files->fd;
}
/* Free the vfs fd set if necessary */
-static void free_fd_set(struct files_struct *open_files) {
+static void free_fd_set(struct files_struct *open_files)
+{
+ void *free_me;
if (open_files->open_fds != (struct fd_set*)&open_files->open_fds_init) {
- kfree(open_files->open_fds);
assert(open_files->fd != open_files->fd_array);
- kfree(open_files->fd);
+ /* need to reset the pointers to the internal addrs, in case we take a
+ * look while debugging. 0 them out, since they have old data. our
+ * current versions should all be closed. */
+ memset(&open_files->open_fds_init, 0, sizeof(struct small_fd_set));
+ memset(&open_files->fd_array, 0, sizeof(open_files->fd_array));
+
+ free_me = open_files->open_fds;
+ open_files->open_fds = (struct fd_set*)&open_files->open_fds_init;
+ kfree(free_me);
+
+ free_me = open_files->fd;
+ open_files->fd = open_files->fd_array;
+ kfree(free_me);
}
}
static int __get_fd(struct files_struct *open_files, int low_fd)
{
int slot = -1;
+ int error;
if ((low_fd < 0) || (low_fd > NR_FILE_DESC_MAX))
return -EINVAL;
if (open_files->closed)
break;
}
if (slot == -1) {
- /* Expand the FD array and fd_set */
- if (grow_fd_set(open_files) == -1)
- return -ENOMEM;
- /* loop after growing */
+ if ((error = grow_fd_set(open_files)))
+ return error;
}
}
return slot;
}
-/* Gets and claims a free FD, used by 9ns. < 0 == error. */
-int get_fd(struct files_struct *open_files, int low_fd)
+/* Gets and claims a free FD, used by 9ns. < 0 == error. cloexec is tracked on
+ * the VFS FD. It's value will be O_CLOEXEC (not 1) or 0. */
+int get_fd(struct files_struct *open_files, int low_fd, int cloexec)
{
int slot;
spin_lock(&open_files->lock);
slot = __get_fd(open_files, low_fd);
+ if (cloexec && (slot >= 0))
+ open_files->fd[slot].fd_flags |= FD_CLOEXEC;
spin_unlock(&open_files->lock);
return slot;
}
static int __claim_fd(struct files_struct *open_files, int file_desc)
{
+ int error;
if ((file_desc < 0) || (file_desc > NR_FILE_DESC_MAX))
return -EINVAL;
if (open_files->closed)
/* Grow the open_files->fd_set until the file_desc can fit inside it */
while(file_desc >= open_files->max_files) {
- grow_fd_set(open_files);
+ if ((error = grow_fd_set(open_files)))
+ return error;
cpu_relax();
}
return -ENFILE; /* Should never really happen. Here to catch bugs. */
SET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
- assert(file_desc < open_files->max_files && open_files->fd[0].fd_file == 0);
+ assert(file_desc < open_files->max_files &&
+ open_files->fd[file_desc].fd_file == 0);
if (file_desc >= open_files->next_fd)
open_files->next_fd = file_desc + 1;
return 0;
}
-/* Claims a specific FD when duping FDs. used by 9ns. < 0 == error. */
+/* Claims a specific FD when duping FDs. used by 9ns. < 0 == error. No need
+ * for cloexec here, since it's not used during dup. */
int claim_fd(struct files_struct *open_files, int file_desc)
{
int ret;
}
/* Inserts the file in the files_struct, returning the corresponding new file
- * descriptor, or an error code. We start looking for open fds from low_fd. */
-int insert_file(struct files_struct *open_files, struct file *file, int low_fd)
+ * descriptor, or an error code. We start looking for open fds from low_fd.
+ *
+ * Passing cloexec is a bit cheap, since we might want to expand it to support
+ * more FD options in the future. */
+int insert_file(struct files_struct *open_files, struct file *file, int low_fd,
+ bool must, bool cloexec)
{
- int slot;
+ int slot, ret;
spin_lock(&open_files->lock);
- slot = __get_fd(open_files, low_fd);
+ if (must) {
+ ret = __claim_fd(open_files, low_fd);
+ if (ret < 0) {
+ spin_unlock(&open_files->lock);
+ return ret;
+ }
+ assert(!ret); /* issues with claim_fd returning status, not the fd */
+ slot = low_fd;
+ } else {
+ slot = __get_fd(open_files, low_fd);
+ }
+
if (slot < 0) {
spin_unlock(&open_files->lock);
return slot;
kref_get(&file->f_kref, 1);
open_files->fd[slot].fd_file = file;
open_files->fd[slot].fd_flags = 0;
+ if (cloexec)
+ open_files->fd[slot].fd_flags |= FD_CLOEXEC;
spin_unlock(&open_files->lock);
return slot;
}
/* Closes all open files. Mostly just a "put" for all files. If cloexec, it
- * will only close files that are opened with O_CLOEXEC. */
+ * will only close the FDs with FD_CLOEXEC (opened with O_CLOEXEC or fcntld). */
void close_all_files(struct files_struct *open_files, bool cloexec)
{
struct file *file;
/* no file == 9ns uses the FD. they will deal with it */
if (!file)
continue;
- if (cloexec && !(open_files->fd[i].fd_flags & O_CLOEXEC))
+ if (cloexec && !(open_files->fd[i].fd_flags & FD_CLOEXEC))
continue;
/* Actually close the file */
open_files->fd[i].fd_file = 0;
spin_unlock(&src->lock);
}
+static void __chpwd(struct fs_struct *fs_env, struct dentry *new_pwd)
+{
+ struct dentry *old_pwd;
+ kref_get(&new_pwd->d_kref, 1);
+ /* writer lock, make sure we replace pwd with ours. could also CAS.
+ * readers don't lock at all, so they need to either loop, or we need to
+ * delay releasing old_pwd til an RCU grace period. */
+ spin_lock(&fs_env->lock);
+ old_pwd = fs_env->pwd;
+ fs_env->pwd = new_pwd;
+ spin_unlock(&fs_env->lock);
+ kref_put(&old_pwd->d_kref);
+}
+
/* Change the working directory of the given fs env (one per process, at this
- * point). Returns 0 for success, -ERROR for whatever error. */
+ * point). Returns 0 for success, sets errno and returns -1 otherwise. */
int do_chdir(struct fs_struct *fs_env, char *path)
{
struct nameidata nd_r = {0}, *nd = &nd_r;
- int retval;
- retval = path_lookup(path, LOOKUP_DIRECTORY, nd);
- if (!retval) {
- /* nd->dentry is the place we want our PWD to be */
- kref_get(&nd->dentry->d_kref, 1);
- kref_put(&fs_env->pwd->d_kref);
- fs_env->pwd = nd->dentry;
+ int error;
+ error = path_lookup(path, LOOKUP_DIRECTORY, nd);
+ if (error) {
+ set_errno(-error);
+ path_release(nd);
+ return -1;
}
+ /* nd->dentry is the place we want our PWD to be */
+ __chpwd(fs_env, nd->dentry);
path_release(nd);
- return retval;
+ return 0;
+}
+
+int do_fchdir(struct fs_struct *fs_env, struct file *file)
+{
+ if ((file->f_dentry->d_inode->i_mode & __S_IFMT) != __S_IFDIR) {
+ set_errno(ENOTDIR);
+ return -1;
+ }
+ __chpwd(fs_env, file->f_dentry);
+ return 0;
}
/* Returns a null-terminated string of up to length cwd_l containing the