2010-08-02 13:44:45 +02:00
|
|
|
/* Created (MFS based):
|
|
|
|
* February 2010 (Evgeniy Ivanov)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
2012-08-25 19:42:05 +02:00
|
|
|
#include <assert.h>
|
2010-08-02 13:44:45 +02:00
|
|
|
#include <minix/com.h>
|
|
|
|
#include "buf.h"
|
|
|
|
#include "inode.h"
|
|
|
|
#include "super.h"
|
|
|
|
#include <minix/vfsif.h>
|
|
|
|
|
2013-03-07 15:46:21 +01:00
|
|
|
static struct inode *new_node(struct inode *ldirp, char *string, pmode_t
|
2012-03-24 16:16:34 +01:00
|
|
|
bits, block_t z0);
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_create *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_create()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
phys_bytes len;
|
|
|
|
int r;
|
|
|
|
struct inode *ldirp;
|
|
|
|
struct inode *rip;
|
2014-04-28 18:25:26 +02:00
|
|
|
mode_t omode;
|
2010-08-02 13:44:45 +02:00
|
|
|
char lastc[NAME_MAX + 1];
|
|
|
|
|
|
|
|
/* Read request message */
|
2014-04-28 18:25:26 +02:00
|
|
|
omode = fs_m_in.m_vfs_fs_create.mode;
|
|
|
|
caller_uid = fs_m_in.m_vfs_fs_create.uid;
|
|
|
|
caller_gid = fs_m_in.m_vfs_fs_create.gid;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* Try to make the file. */
|
|
|
|
|
|
|
|
/* Copy the last component (i.e., file name) */
|
2014-04-28 18:25:26 +02:00
|
|
|
len = fs_m_in.m_vfs_fs_create.path_len; /* including trailing '\0' */
|
2010-08-02 13:44:45 +02:00
|
|
|
if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
|
|
|
|
return(ENAMETOOLONG);
|
|
|
|
|
2014-04-28 18:25:26 +02:00
|
|
|
err_code = sys_safecopyfrom(VFS_PROC_NR, fs_m_in.m_vfs_fs_create.grant,
|
2012-06-16 03:46:15 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) lastc, (size_t) len);
|
2010-08-02 13:44:45 +02:00
|
|
|
if (err_code != OK) return err_code;
|
|
|
|
NUL(lastc, len, sizeof(lastc));
|
|
|
|
|
|
|
|
/* Get last directory inode (i.e., directory that will hold the new inode) */
|
2014-04-28 18:25:26 +02:00
|
|
|
if ((ldirp = get_inode(fs_dev, fs_m_in.m_vfs_fs_create.inode)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(ENOENT);
|
|
|
|
|
|
|
|
/* Create a new inode by calling new_node(). */
|
|
|
|
rip = new_node(ldirp, lastc, omode, NO_BLOCK);
|
|
|
|
r = err_code;
|
|
|
|
|
|
|
|
/* If an error occurred, release inode. */
|
|
|
|
if (r != OK) {
|
|
|
|
put_inode(ldirp);
|
|
|
|
put_inode(rip);
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reply message */
|
2014-04-28 18:25:26 +02:00
|
|
|
fs_m_out.m_fs_vfs_create.inode = rip->i_num;
|
|
|
|
fs_m_out.m_fs_vfs_create.mode = rip->i_mode;
|
|
|
|
fs_m_out.m_fs_vfs_create.file_size = rip->i_size;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* This values are needed for the execution */
|
2014-04-28 18:25:26 +02:00
|
|
|
fs_m_out.m_fs_vfs_create.uid = rip->i_uid;
|
|
|
|
fs_m_out.m_fs_vfs_create.gid = rip->i_gid;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* Drop parent dir */
|
|
|
|
put_inode(ldirp);
|
|
|
|
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_mknod *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_mknod()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
struct inode *ip, *ldirp;
|
|
|
|
char lastc[NAME_MAX + 1];
|
|
|
|
phys_bytes len;
|
|
|
|
|
|
|
|
/* Copy the last component and set up caller's user and group id */
|
|
|
|
len = fs_m_in.REQ_PATH_LEN; /* including trailing '\0' */
|
|
|
|
if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
|
|
|
|
return(ENAMETOOLONG);
|
|
|
|
|
|
|
|
err_code = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_GRANT,
|
2012-06-16 03:46:15 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) lastc, (size_t) len);
|
2010-08-02 13:44:45 +02:00
|
|
|
if (err_code != OK) return err_code;
|
|
|
|
NUL(lastc, len, sizeof(lastc));
|
|
|
|
|
|
|
|
caller_uid = (uid_t) fs_m_in.REQ_UID;
|
|
|
|
caller_gid = (gid_t) fs_m_in.REQ_GID;
|
|
|
|
|
|
|
|
/* Get last directory inode */
|
2013-03-07 15:46:21 +01:00
|
|
|
if((ldirp = get_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(ENOENT);
|
|
|
|
|
|
|
|
/* Try to create the new node */
|
2013-03-07 15:46:21 +01:00
|
|
|
ip = new_node(ldirp, lastc, (pmode_t) fs_m_in.REQ_MODE,
|
2010-08-02 13:44:45 +02:00
|
|
|
(block_t) fs_m_in.REQ_DEV);
|
|
|
|
|
|
|
|
put_inode(ip);
|
|
|
|
put_inode(ldirp);
|
|
|
|
return(err_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_mkdir *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_mkdir()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
int r1, r2; /* status codes */
|
2013-03-07 15:46:21 +01:00
|
|
|
pino_t dot, dotdot; /* inode numbers for . and .. */
|
2010-08-02 13:44:45 +02:00
|
|
|
struct inode *rip, *ldirp;
|
|
|
|
char lastc[NAME_MAX + 1]; /* last component */
|
|
|
|
phys_bytes len;
|
|
|
|
|
|
|
|
/* Copy the last component and set up caller's user and group id */
|
|
|
|
len = fs_m_in.REQ_PATH_LEN; /* including trailing '\0' */
|
|
|
|
if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
|
|
|
|
return(ENAMETOOLONG);
|
|
|
|
|
|
|
|
err_code = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_GRANT,
|
2012-06-16 03:46:15 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) lastc, (phys_bytes) len);
|
2010-08-02 13:44:45 +02:00
|
|
|
if(err_code != OK) return(err_code);
|
|
|
|
NUL(lastc, len, sizeof(lastc));
|
|
|
|
|
|
|
|
caller_uid = (uid_t) fs_m_in.REQ_UID;
|
|
|
|
caller_gid = (gid_t) fs_m_in.REQ_GID;
|
|
|
|
|
|
|
|
/* Get last directory inode */
|
2013-03-07 15:46:21 +01:00
|
|
|
if((ldirp = get_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(ENOENT);
|
|
|
|
|
|
|
|
/* Next make the inode. If that fails, return error code. */
|
2013-03-07 15:46:21 +01:00
|
|
|
rip = new_node(ldirp, lastc, (pino_t) fs_m_in.REQ_MODE, (block_t) 0);
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
if(rip == NULL || err_code == EEXIST) {
|
|
|
|
put_inode(rip); /* can't make dir: it already exists */
|
|
|
|
put_inode(ldirp);
|
|
|
|
return(err_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the inode numbers for . and .. to enter in the directory. */
|
|
|
|
dotdot = ldirp->i_num; /* parent's inode number */
|
|
|
|
dot = rip->i_num; /* inode number of the new dir itself */
|
|
|
|
|
|
|
|
/* Now make dir entries for . and .. unless the disk is completely full. */
|
|
|
|
/* Use dot1 and dot2, so the mode of the directory isn't important. */
|
2013-03-07 15:46:21 +01:00
|
|
|
rip->i_mode = (pmode_t) fs_m_in.REQ_MODE; /* set mode */
|
2010-08-02 13:44:45 +02:00
|
|
|
/* enter . in the new dir*/
|
|
|
|
r1 = search_dir(rip, dot1, &dot, ENTER, IGN_PERM, I_DIRECTORY);
|
|
|
|
/* enter .. in the new dir */
|
|
|
|
r2 = search_dir(rip, dot2, &dotdot, ENTER, IGN_PERM, I_DIRECTORY);
|
|
|
|
|
|
|
|
/* If both . and .. were successfully entered, increment the link counts. */
|
|
|
|
if (r1 == OK && r2 == OK) {
|
|
|
|
/* Normal case. It was possible to enter . and .. in the new dir. */
|
|
|
|
rip->i_links_count++; /* this accounts for . */
|
|
|
|
ldirp->i_links_count++; /* this accounts for .. */
|
2012-10-16 17:40:39 +02:00
|
|
|
ldirp->i_dirt = IN_DIRTY; /* mark parent's inode as dirty */
|
2010-08-02 13:44:45 +02:00
|
|
|
} else {
|
|
|
|
/* It was not possible to enter . or .. probably disk was full -
|
|
|
|
* links counts haven't been touched. */
|
|
|
|
if (search_dir(ldirp, lastc, NULL, DELETE, IGN_PERM, 0) != OK)
|
2013-08-26 18:43:05 +02:00
|
|
|
panic("Dir disappeared: %d ", (int) rip->i_num);
|
2010-08-02 13:44:45 +02:00
|
|
|
rip->i_links_count--; /* undo the increment done in new_node() */
|
|
|
|
}
|
2012-10-16 17:40:39 +02:00
|
|
|
rip->i_dirt = IN_DIRTY; /* either way, i_links_count has changed */
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
put_inode(ldirp); /* return the inode of the parent dir */
|
|
|
|
put_inode(rip); /* return the inode of the newly made dir */
|
|
|
|
return(err_code); /* new_node() always sets 'err_code' */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_slink *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_slink()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
phys_bytes len;
|
|
|
|
struct inode *sip; /* inode containing symbolic link */
|
|
|
|
struct inode *ldirp; /* directory containing link */
|
|
|
|
register int r; /* error code */
|
|
|
|
char string[NAME_MAX]; /* last component of the new dir's path name */
|
2012-08-25 19:42:05 +02:00
|
|
|
char* link_target_buf = NULL; /* either sip->i_block or bp->b_data */
|
2010-08-02 13:44:45 +02:00
|
|
|
struct buf *bp = NULL; /* disk buffer for link */
|
|
|
|
|
2014-04-29 20:59:48 +02:00
|
|
|
caller_uid = fs_m_in.m_vfs_fs_slink.uid;
|
|
|
|
caller_gid = fs_m_in.m_vfs_fs_slink.gid;
|
2010-08-02 13:44:45 +02:00
|
|
|
|
|
|
|
/* Copy the link name's last component */
|
2014-04-29 20:59:48 +02:00
|
|
|
len = fs_m_in.m_vfs_fs_slink.path_len;
|
2010-08-02 13:44:45 +02:00
|
|
|
if (len > NAME_MAX || len > EXT2_NAME_MAX)
|
|
|
|
return(ENAMETOOLONG);
|
|
|
|
|
2014-04-29 20:59:48 +02:00
|
|
|
r = sys_safecopyfrom(VFS_PROC_NR, fs_m_in.m_vfs_fs_slink.grant_path,
|
2012-06-16 03:46:15 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) string, (size_t) len);
|
2010-08-02 13:44:45 +02:00
|
|
|
if (r != OK) return(r);
|
|
|
|
NUL(string, len, sizeof(string));
|
|
|
|
|
|
|
|
/* Temporarily open the dir. */
|
2014-04-29 20:59:48 +02:00
|
|
|
if( (ldirp = get_inode(fs_dev, fs_m_in.m_vfs_fs_slink.inode)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/* Create the inode for the symlink. */
|
2013-03-07 15:46:21 +01:00
|
|
|
sip = new_node(ldirp, string, (pmode_t) (I_SYMBOLIC_LINK | RWX_MODES),
|
2010-08-02 13:44:45 +02:00
|
|
|
(block_t) 0);
|
|
|
|
|
|
|
|
/* If we can then create fast symlink (store it in inode),
|
|
|
|
* Otherwise allocate a disk block for the contents of the symlink and
|
|
|
|
* copy contents of symlink (the name pointed to) into first disk block. */
|
|
|
|
if( (r = err_code) == OK) {
|
2014-04-29 20:59:48 +02:00
|
|
|
if ( (fs_m_in.m_vfs_fs_slink.mem_size + 1) > sip->i_sp->s_block_size) {
|
2010-08-02 13:44:45 +02:00
|
|
|
r = ENAMETOOLONG;
|
2014-04-29 20:59:48 +02:00
|
|
|
} else if ((fs_m_in.m_vfs_fs_slink.mem_size + 1) <= MAX_FAST_SYMLINK_LENGTH) {
|
2010-08-02 13:44:45 +02:00
|
|
|
r = sys_safecopyfrom(VFS_PROC_NR,
|
2014-04-29 20:59:48 +02:00
|
|
|
fs_m_in.m_vfs_fs_slink.grant_target,
|
2010-08-02 13:44:45 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) sip->i_block,
|
2014-04-29 20:59:48 +02:00
|
|
|
(vir_bytes) fs_m_in.m_vfs_fs_slink.mem_size);
|
2012-10-16 17:40:39 +02:00
|
|
|
sip->i_dirt = IN_DIRTY;
|
2010-08-02 13:44:45 +02:00
|
|
|
link_target_buf = (char*) sip->i_block;
|
|
|
|
} else {
|
2011-08-10 22:56:44 +02:00
|
|
|
if ((bp = new_block(sip, (off_t) 0)) != NULL) {
|
2010-08-02 13:44:45 +02:00
|
|
|
sys_safecopyfrom(VFS_PROC_NR,
|
2014-04-29 20:59:48 +02:00
|
|
|
fs_m_in.m_vfs_fs_slink.grant_target,
|
2012-10-16 17:40:39 +02:00
|
|
|
(vir_bytes) 0, (vir_bytes) b_data(bp),
|
2014-04-29 20:59:48 +02:00
|
|
|
(vir_bytes) fs_m_in.m_vfs_fs_slink.mem_size);
|
2012-10-16 17:40:39 +02:00
|
|
|
lmfs_markdirty(bp);
|
|
|
|
link_target_buf = b_data(bp);
|
2011-08-10 22:56:44 +02:00
|
|
|
} else {
|
|
|
|
r = err_code;
|
|
|
|
}
|
2010-08-02 13:44:45 +02:00
|
|
|
}
|
|
|
|
if (r == OK) {
|
2012-08-25 19:42:05 +02:00
|
|
|
assert(link_target_buf);
|
2014-04-29 20:59:48 +02:00
|
|
|
link_target_buf[fs_m_in.m_vfs_fs_slink.mem_size] = '\0';
|
2010-08-02 13:44:45 +02:00
|
|
|
sip->i_size = (off_t) strlen(link_target_buf);
|
2014-04-29 20:59:48 +02:00
|
|
|
if (sip->i_size != fs_m_in.m_vfs_fs_slink.mem_size) {
|
2010-08-02 13:44:45 +02:00
|
|
|
/* This can happen if the user provides a buffer
|
|
|
|
* with a \0 in it. This can cause a lot of trouble
|
|
|
|
* when the symlink is used later. We could just use
|
|
|
|
* the strlen() value, but we want to let the user
|
|
|
|
* know he did something wrong. ENAMETOOLONG doesn't
|
|
|
|
* exactly describe the error, but there is no
|
|
|
|
* ENAMETOOWRONG.
|
|
|
|
*/
|
|
|
|
r = ENAMETOOLONG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_block(bp, DIRECTORY_BLOCK); /* put_block() accepts NULL. */
|
|
|
|
|
|
|
|
if(r != OK) {
|
|
|
|
sip->i_links_count = NO_LINK;
|
|
|
|
if (search_dir(ldirp, string, NULL, DELETE, IGN_PERM, 0) != OK)
|
|
|
|
panic("Symbolic link vanished");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put_inode() accepts NULL as a noop, so the below are safe. */
|
|
|
|
put_inode(sip);
|
|
|
|
put_inode(ldirp);
|
|
|
|
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* new_node *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct inode *new_node(struct inode *ldirp,
|
2013-03-07 15:46:21 +01:00
|
|
|
char *string, pmode_t bits, block_t b0)
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
/* New_node() is called by fs_open(), fs_mknod(), and fs_mkdir().
|
|
|
|
* In all cases it allocates a new inode, makes a directory entry for it in
|
|
|
|
* the ldirp directory with string name, and initializes it.
|
|
|
|
* It returns a pointer to the inode if it can do this;
|
|
|
|
* otherwise it returns NULL. It always sets 'err_code'
|
|
|
|
* to an appropriate value (OK or an error code).
|
|
|
|
*/
|
|
|
|
|
|
|
|
register struct inode *rip;
|
|
|
|
register int r;
|
|
|
|
|
2010-09-01 11:07:18 +02:00
|
|
|
if (ldirp->i_links_count == NO_LINK) { /* Dir does not actually exist */
|
|
|
|
err_code = ENOENT;
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2010-08-02 13:44:45 +02:00
|
|
|
/* Get final component of the path. */
|
|
|
|
rip = advance(ldirp, string, IGN_PERM);
|
|
|
|
|
|
|
|
if (S_ISDIR(bits) && (ldirp->i_links_count >= USHRT_MAX ||
|
|
|
|
ldirp->i_links_count >= LINK_MAX)) {
|
|
|
|
/* New entry is a directory, alas we can't give it a ".." */
|
|
|
|
put_inode(rip);
|
|
|
|
err_code = EMLINK;
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rip == NULL && err_code == ENOENT) {
|
|
|
|
/* Last path component does not exist. Make new directory entry. */
|
|
|
|
if ( (rip = alloc_inode(ldirp, bits)) == NULL) {
|
|
|
|
/* Can't creat new inode: out of inodes. */
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force inode to the disk before making directory entry to make
|
|
|
|
* the system more robust in the face of a crash: an inode with
|
|
|
|
* no directory entry is much better than the opposite.
|
|
|
|
*/
|
|
|
|
rip->i_links_count++;
|
|
|
|
rip->i_block[0] = b0; /* major/minor device numbers */
|
|
|
|
rw_inode(rip, WRITING); /* force inode to disk now */
|
|
|
|
|
|
|
|
/* New inode acquired. Try to make directory entry. */
|
|
|
|
if ((r=search_dir(ldirp, string, &rip->i_num, ENTER, IGN_PERM,
|
|
|
|
rip->i_mode & I_TYPE)) != OK) {
|
|
|
|
rip->i_links_count--; /* pity, have to free disk inode */
|
2012-10-16 17:40:39 +02:00
|
|
|
rip->i_dirt = IN_DIRTY; /* dirty inodes are written out */
|
2010-08-02 13:44:45 +02:00
|
|
|
put_inode(rip); /* this call frees the inode */
|
|
|
|
err_code = r;
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (err_code == EENTERMOUNT || err_code == ELEAVEMOUNT) {
|
|
|
|
r = EEXIST;
|
|
|
|
} else {
|
|
|
|
/* Either last component exists, or there is some problem. */
|
|
|
|
if (rip != NULL)
|
|
|
|
r = EEXIST;
|
|
|
|
else
|
|
|
|
r = err_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The caller has to return the directory inode (*ldirp). */
|
|
|
|
err_code = r;
|
|
|
|
return(rip);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_inhibread *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int fs_inhibread()
|
2010-08-02 13:44:45 +02:00
|
|
|
{
|
|
|
|
struct inode *rip;
|
|
|
|
|
2013-03-07 15:46:21 +01:00
|
|
|
if((rip = find_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
2010-08-02 13:44:45 +02:00
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/* inhibit read ahead */
|
|
|
|
rip->i_seek = ISEEK;
|
|
|
|
|
|
|
|
return(OK);
|
|
|
|
}
|