minix/servers/vfs/link.c

456 lines
13 KiB
C
Raw Normal View History

/* This file handles the LINK and UNLINK system calls. It also deals with
* deallocating the storage used by a file when the last UNLINK is done to a
* file and the blocks must be returned to the free block pool.
*
* The entry points into this file are
* do_link: perform the LINK system call
* do_unlink: perform the UNLINK and RMDIR system calls
* do_rename: perform the RENAME system call
* do_truncate: perform the TRUNCATE system call
* do_ftruncate: perform the FTRUNCATE system call
* do_rdlink: perform the RDLNK system call
*/
#include "fs.h"
#include <sys/stat.h>
#include <string.h>
#include <minix/com.h>
#include <minix/callnr.h>
2012-02-13 16:28:04 +01:00
#include <minix/vfsif.h>
#include <dirent.h>
2012-02-13 16:28:04 +01:00
#include <assert.h>
#include "file.h"
#include "fproc.h"
2012-02-13 16:28:04 +01:00
#include "path.h"
#include "vnode.h"
2012-02-13 16:28:04 +01:00
#include "param.h"
/*===========================================================================*
* do_link *
*===========================================================================*/
PUBLIC int do_link()
{
/* Perform the link(name1, name2) system call. */
int r = OK;
2012-02-13 16:28:04 +01:00
struct vnode *vp = NULL, *dirp = NULL;
struct vmnt *vmp1 = NULL, *vmp2 = NULL;
char fullpath[PATH_MAX];
struct lookup resolve;
2012-02-13 16:28:04 +01:00
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp1, &vp);
resolve.l_vmnt_lock = VMNT_WRITE;
resolve.l_vnode_lock = VNODE_READ;
/* See if 'name1' (file to be linked to) exists. */
if (fetch_name(m_in.name1, m_in.name1_length, M1, fullpath) != OK)
return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
/* Does the final directory of 'name2' exist? */
2012-02-13 16:28:04 +01:00
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp2, &dirp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
if (fetch_name(m_in.name2, m_in.name2_length, M1, fullpath) != OK)
r = err_code;
else if ((dirp = last_dir(&resolve, fp)) == NULL)
r = err_code;
2012-02-13 16:28:04 +01:00
if (r != OK) {
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
unlock_vmnt(vmp1);
put_vnode(vp);
return(r);
2007-08-07 14:52:47 +02:00
}
/* Check for links across devices. */
2012-02-13 16:28:04 +01:00
if (vp->v_fs_e != dirp->v_fs_e)
r = EXDEV;
else
r = forbidden(fp, dirp, W_BIT | X_BIT);
if (r == OK)
2012-02-13 16:28:04 +01:00
r = req_link(vp->v_fs_e, dirp->v_inode_nr, fullpath,
vp->v_inode_nr);
2007-08-08 17:26:47 +02:00
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
unlock_vnode(dirp);
if (vmp2 != NULL) unlock_vmnt(vmp2);
unlock_vmnt(vmp1);
put_vnode(vp);
2012-02-13 16:28:04 +01:00
put_vnode(dirp);
return(r);
}
/*===========================================================================*
* do_unlink *
*===========================================================================*/
PUBLIC int do_unlink()
{
/* Perform the unlink(name) or rmdir(name) system call. The code for these two
* is almost the same. They differ only in some condition testing. Unlink()
* may be used by the superuser to do dangerous things; rmdir() may not.
*/
2012-02-13 16:28:04 +01:00
struct vnode *dirp, *vp;
struct vmnt *vmp, *vmp2;
int r;
2012-02-13 16:28:04 +01:00
char fullpath[PATH_MAX];
struct lookup resolve;
lookup_init(&resolve, fullpath, PATH_RET_SYMLINK, &vmp, &dirp);
resolve.l_vmnt_lock = VMNT_WRITE;
resolve.l_vnode_lock = VNODE_READ;
/* Get the last directory in the path. */
2012-02-13 16:28:04 +01:00
if (fetch_name(m_in.name, m_in.name_length, M3, fullpath) != OK)
return(err_code);
if ((dirp = last_dir(&resolve, fp)) == NULL) return(err_code);
assert(vmp != NULL);
2007-08-07 14:52:47 +02:00
/* Make sure that the object is a directory */
2012-02-13 16:28:04 +01:00
if ((dirp->v_mode & I_TYPE) != I_DIRECTORY) {
unlock_vnode(dirp);
unlock_vmnt(vmp);
put_vnode(dirp);
return(ENOTDIR);
}
2007-08-07 14:52:47 +02:00
/* The caller must have both search and execute permission */
2012-02-13 16:28:04 +01:00
if ((r = forbidden(fp, dirp, X_BIT | W_BIT)) != OK) {
unlock_vnode(dirp);
unlock_vmnt(vmp);
put_vnode(dirp);
return(r);
2007-08-07 14:52:47 +02:00
}
2012-02-13 16:28:04 +01:00
/* Also, if the sticky bit is set, only the owner of the file or a privileged
user is allowed to unlink */
2012-02-13 16:28:04 +01:00
if ((dirp->v_mode & S_ISVTX) == S_ISVTX) {
/* Look up inode of file to unlink to retrieve owner */
2012-02-13 16:28:04 +01:00
resolve.l_flags = PATH_RET_SYMLINK;
resolve.l_vmp = &vmp2; /* Shouldn't actually get locked */
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode = &vp;
resolve.l_vnode_lock = VNODE_READ;
vp = advance(dirp, &resolve, fp);
assert(vmp2 == NULL);
if (vp != NULL) {
2012-02-13 16:28:04 +01:00
if (vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID)
r = EPERM;
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
put_vnode(vp);
} else
r = err_code;
if (r != OK) {
2012-02-13 16:28:04 +01:00
unlock_vnode(dirp);
unlock_vmnt(vmp);
put_vnode(dirp);
return(r);
}
}
2012-02-13 16:28:04 +01:00
assert(vmp != NULL);
tll_upgrade(&vmp->m_lock);
if (call_nr == UNLINK)
r = req_unlink(dirp->v_fs_e, dirp->v_inode_nr, fullpath);
else
r = req_rmdir(dirp->v_fs_e, dirp->v_inode_nr, fullpath);
unlock_vnode(dirp);
unlock_vmnt(vmp);
put_vnode(dirp);
return(r);
}
/*===========================================================================*
* do_rename *
*===========================================================================*/
PUBLIC int do_rename()
{
/* Perform the rename(name1, name2) system call. */
int r = OK, r1;
struct vnode *old_dirp, *new_dirp = NULL, *vp;
2012-02-13 16:28:04 +01:00
struct vmnt *oldvmp, *newvmp, *vmp2;
char old_name[PATH_MAX];
2012-02-13 16:28:04 +01:00
char fullpath[PATH_MAX];
struct lookup resolve;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &oldvmp, &old_dirp);
/* Do not yet request exclusive lock on vmnt to prevent deadlocks later on */
resolve.l_vmnt_lock = VMNT_WRITE;
resolve.l_vnode_lock = VNODE_READ;
/* See if 'name1' (existing file) exists. Get dir and file inodes. */
2012-02-13 16:28:04 +01:00
if (fetch_name(m_in.name1, m_in.name1_length, M1, fullpath) != OK)
return(err_code);
if ((old_dirp = last_dir(&resolve, fp)) == NULL)
return(err_code);
/* If the sticky bit is set, only the owner of the file or a privileged
user is allowed to rename */
2012-02-13 16:28:04 +01:00
if ((old_dirp->v_mode & S_ISVTX) == S_ISVTX) {
/* Look up inode of file to unlink to retrieve owner */
2012-02-13 16:28:04 +01:00
lookup_init(&resolve, resolve.l_path, PATH_RET_SYMLINK, &vmp2, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
vp = advance(old_dirp, &resolve, fp);
assert(vmp2 == NULL);
if (vp != NULL) {
2012-02-13 16:28:04 +01:00
if(vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID)
r = EPERM;
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
put_vnode(vp);
} else
r = err_code;
2012-02-13 16:28:04 +01:00
if (r != OK) {
unlock_vnode(old_dirp);
unlock_vmnt(oldvmp);
put_vnode(old_dirp);
return(r);
}
2007-08-07 14:52:47 +02:00
}
2012-02-13 16:28:04 +01:00
2007-08-07 14:52:47 +02:00
/* Save the last component of the old name */
2012-02-13 16:28:04 +01:00
if(strlen(fullpath) >= sizeof(old_name)) {
unlock_vnode(old_dirp);
unlock_vmnt(oldvmp);
put_vnode(old_dirp);
return(ENAMETOOLONG);
2007-08-07 14:52:47 +02:00
}
2012-02-13 16:28:04 +01:00
strcpy(old_name, fullpath);
/* See if 'name2' (new name) exists. Get dir inode */
2012-02-13 16:28:04 +01:00
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &newvmp, &new_dirp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
if (fetch_name(m_in.name2, m_in.name2_length, M1, fullpath) != OK)
r = err_code;
else if ((new_dirp = last_dir(&resolve, fp)) == NULL)
r = err_code;
2012-02-13 16:28:04 +01:00
if (r != OK) {
2012-02-13 16:28:04 +01:00
unlock_vnode(old_dirp);
unlock_vmnt(oldvmp);
put_vnode(old_dirp);
return(r);
2007-08-07 14:52:47 +02:00
}
/* Both parent directories must be on the same device. */
2012-02-13 16:28:04 +01:00
if (old_dirp->v_fs_e != new_dirp->v_fs_e) r = EXDEV;
2007-08-07 14:52:47 +02:00
/* Parent dirs must be writable, searchable and on a writable device */
if ((r1 = forbidden(fp, old_dirp, W_BIT|X_BIT)) != OK ||
(r1 = forbidden(fp, new_dirp, W_BIT|X_BIT)) != OK) r = r1;
2012-02-13 16:28:04 +01:00
if (r == OK) {
tll_upgrade(&oldvmp->m_lock); /* Upgrade to exclusive access */
r = req_rename(old_dirp->v_fs_e, old_dirp->v_inode_nr, old_name,
new_dirp->v_inode_nr, fullpath);
}
unlock_vnode(old_dirp);
unlock_vnode(new_dirp);
unlock_vmnt(oldvmp);
if (newvmp) unlock_vmnt(newvmp);
put_vnode(old_dirp);
put_vnode(new_dirp);
2012-02-13 16:28:04 +01:00
return(r);
}
/*===========================================================================*
* do_truncate *
*===========================================================================*/
PUBLIC int do_truncate()
{
/* truncate_vnode() does the actual work of do_truncate() and do_ftruncate().
* do_truncate() and do_ftruncate() have to get hold of the inode, either
* by name or fd, do checks on it, and call truncate_inode() to do the
* work.
*/
struct vnode *vp;
2012-02-13 16:28:04 +01:00
struct vmnt *vmp;
int r;
2012-02-13 16:28:04 +01:00
char fullpath[PATH_MAX];
struct lookup resolve;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_EXCL;
resolve.l_vnode_lock = VNODE_WRITE;
2007-01-05 17:36:55 +01:00
if ((off_t) m_in.flength < 0) return(EINVAL);
/* Temporarily open file */
2012-02-13 16:28:04 +01:00
if (fetch_name(m_in.m2_p1, m_in.m2_i1, M1, fullpath) != OK) return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
/* Ask FS to truncate the file */
if ((r = forbidden(fp, vp, W_BIT)) == OK)
2012-02-13 16:28:04 +01:00
r = truncate_vnode(vp, m_in.flength);
unlock_vnode(vp);
unlock_vmnt(vmp);
2007-08-07 14:52:47 +02:00
put_vnode(vp);
return(r);
}
/*===========================================================================*
* do_ftruncate *
*===========================================================================*/
PUBLIC int do_ftruncate()
{
/* As with do_truncate(), truncate_vnode() does the actual work. */
struct filp *rfilp;
2012-02-13 16:28:04 +01:00
int r;
if ((off_t) m_in.flength < 0) return(EINVAL);
/* File is already opened; get a vnode pointer from filp */
2012-02-13 16:28:04 +01:00
if ((rfilp = get_filp(m_in.m2_i1, VNODE_WRITE)) == NULL) return(err_code);
if (!(rfilp->filp_mode & W_BIT))
r = EBADF;
else
r = truncate_vnode(rfilp->filp_vno, m_in.flength);
unlock_filp(rfilp);
return(r);
2007-01-05 17:36:55 +01:00
}
/*===========================================================================*
* truncate_vnode *
2007-01-05 17:36:55 +01:00
*===========================================================================*/
PUBLIC int truncate_vnode(vp, newsize)
2007-01-05 17:36:55 +01:00
struct vnode *vp;
off_t newsize;
{
2012-02-13 16:28:04 +01:00
/* Truncate a regular file or a pipe */
int r, file_type;
2007-01-05 17:36:55 +01:00
2012-02-13 16:28:04 +01:00
assert(tll_locked_by_me(&vp->v_lock));
file_type = vp->v_mode & I_TYPE;
if (file_type != I_REGULAR && file_type != I_NAMED_PIPE) return(EINVAL);
if ((r = req_ftrunc(vp->v_fs_e, vp->v_inode_nr, newsize, 0)) == OK)
vp->v_size = newsize;
return(r);
}
/*===========================================================================*
* do_slink *
*===========================================================================*/
PUBLIC int do_slink()
{
/* Perform the symlink(name1, name2) system call. */
int r;
2007-08-07 14:52:47 +02:00
struct vnode *vp;
2012-02-13 16:28:04 +01:00
struct vmnt *vmp;
char fullpath[PATH_MAX];
struct lookup resolve;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_WRITE;
resolve.l_vnode_lock = VNODE_READ;
if (m_in.name1_length <= 1) return(ENOENT);
if (m_in.name1_length >= SYMLINK_MAX) return(ENAMETOOLONG);
/* Get dir inode of 'name2' */
2012-02-13 16:28:04 +01:00
if (fetch_name(m_in.name2, m_in.name2_length, M1, fullpath) != OK)
return(err_code);
if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code);
if ((r = forbidden(fp, vp, W_BIT|X_BIT)) == OK) {
2012-02-13 16:28:04 +01:00
r = req_slink(vp->v_fs_e, vp->v_inode_nr, fullpath, who_e,
m_in.name1, m_in.name1_length - 1, fp->fp_effuid,
fp->fp_effgid);
2007-08-07 14:52:47 +02:00
}
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
unlock_vmnt(vmp);
2007-08-07 14:52:47 +02:00
put_vnode(vp);
2012-02-13 16:28:04 +01:00
return(r);
}
/*===========================================================================*
* rdlink_direct *
*===========================================================================*/
PUBLIC int rdlink_direct(orig_path, link_path, rfp)
char *orig_path;
2012-02-13 16:28:04 +01:00
char link_path[PATH_MAX]; /* should have length PATH_MAX */
struct fproc *rfp;
{
/* Perform a readlink()-like call from within the VFS */
int r;
struct vnode *vp;
2012-02-13 16:28:04 +01:00
struct vmnt *vmp;
struct lookup resolve;
2012-02-13 16:28:04 +01:00
lookup_init(&resolve, link_path, PATH_RET_SYMLINK, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
/* Temporarily open the file containing the symbolic link. Use link_path
* for temporary storage to keep orig_path untouched. */
strncpy(link_path, orig_path, PATH_MAX); /* PATH_MAX includes '\0' */
link_path[PATH_MAX - 1] = '\0';
if ((vp = eat_path(&resolve, rfp)) == NULL) return(err_code);
/* Make sure this is a symbolic link */
2012-02-13 16:28:04 +01:00
if ((vp->v_mode & I_TYPE) != I_SYMBOLIC_LINK)
r = EINVAL;
else
2012-02-13 16:28:04 +01:00
r = req_rdlink(vp->v_fs_e, vp->v_inode_nr, NONE, link_path,
PATH_MAX - 1, 1);
if (r > 0) link_path[r] = '\0'; /* Terminate string when succesful */
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
unlock_vmnt(vmp);
put_vnode(vp);
2012-02-13 16:28:04 +01:00
return r;
}
/*===========================================================================*
2012-02-13 16:28:04 +01:00
* do_rdlink *
*===========================================================================*/
PUBLIC int do_rdlink()
{
/* Perform the readlink(name, buf, bufsize) system call. */
2007-08-07 14:52:47 +02:00
int r, copylen;
struct vnode *vp;
2012-02-13 16:28:04 +01:00
struct vmnt *vmp;
char fullpath[PATH_MAX];
struct lookup resolve;
lookup_init(&resolve, fullpath, PATH_RET_SYMLINK, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
copylen = m_in.nbytes;
2012-02-13 16:28:04 +01:00
if (copylen < 0) return(EINVAL);
/* Temporarily open the file containing the symbolic link */
2012-02-13 16:28:04 +01:00
if (fetch_name(m_in.name1, m_in.name1_length, M1, fullpath) != OK)
return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
/* Make sure this is a symbolic link */
2012-02-13 16:28:04 +01:00
if ((vp->v_mode & I_TYPE) != I_SYMBOLIC_LINK)
r = EINVAL;
else
r = req_rdlink(vp->v_fs_e, vp->v_inode_nr, who_e, m_in.name2,
2012-02-13 16:28:04 +01:00
copylen, 0);
2007-08-07 14:52:47 +02:00
2012-02-13 16:28:04 +01:00
unlock_vnode(vp);
unlock_vmnt(vmp);
2007-08-07 14:52:47 +02:00
put_vnode(vp);
2012-02-13 16:28:04 +01:00
return(r);
}